3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.step;
22 import org.assertj.core.api.Assertions;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.utils.System2;
27 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
28 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
29 import org.sonar.ce.task.projectanalysis.issue.fixedissues.PullRequestFixedIssueRepositoryImpl;
30 import org.sonar.ce.task.step.TestComputationStepContext;
31 import org.sonar.core.issue.DefaultIssue;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.DbTester;
34 import org.sonar.db.component.ComponentDto;
35 import org.sonar.db.component.ProjectData;
36 import org.sonar.db.issue.IssueFixedDto;
38 import static org.assertj.core.groups.Tuple.tuple;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
42 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
44 public class PersistPullRequestFixedIssueStepIT {
46 public DbTester db = DbTester.create(System2.INSTANCE);
49 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
50 private PullRequestFixedIssueRepositoryImpl fixedIssueRepository;
51 public PersistPullRequestFixedIssueStep underTest;
53 private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
56 public void before() {
57 fixedIssueRepository = new PullRequestFixedIssueRepositoryImpl();
58 underTest = new PersistPullRequestFixedIssueStep(analysisMetadataHolder, db.getDbClient(), treeRootHolder, fixedIssueRepository);
59 when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
63 public void execute_shouldPersistFixedIssues() {
64 ProjectData projectData = db.components().insertPublicProject();
65 ComponentDto pullRequest = db.components().insertProjectBranch(projectData.getMainBranchComponent());
67 fixedIssueRepository.addFixedIssue(new DefaultIssue().setKey("key1"));
68 fixedIssueRepository.addFixedIssue(new DefaultIssue().setKey("key2"));
69 treeRootHolder.setRoot(builder(PROJECT, 1)
70 .setUuid(pullRequest.uuid())
73 underTest.execute(new TestComputationStepContext());
75 Assertions.assertThat(db.getDbClient().issueFixedDao().selectByPullRequest(db.getSession(), pullRequest.uuid()))
76 .extracting(IssueFixedDto::issueKey, IssueFixedDto::pullRequestUuid)
77 .containsExactly(tuple("key1", pullRequest.uuid()), tuple("key2", pullRequest.uuid()));
81 public void execute_whenFixedIssuesAlreadyExists_shouldKeepExistingFixedIssues() {
82 ProjectData projectData = db.components().insertPublicProject();
83 ComponentDto pullRequest = db.components().insertProjectBranch(projectData.getMainBranchComponent());
85 DbSession session = db.getSession();
86 db.getDbClient().issueFixedDao().insert(session, new IssueFixedDto(pullRequest.uuid(), "key1"));
87 db.getDbClient().issueFixedDao().insert(session, new IssueFixedDto(pullRequest.uuid(), "key2"));
90 fixedIssueRepository.addFixedIssue(new DefaultIssue().setKey("key1"));
91 treeRootHolder.setRoot(builder(PROJECT, 1)
92 .setUuid(pullRequest.uuid())
95 underTest.execute(new TestComputationStepContext());
97 Assertions.assertThat(db.getDbClient().issueFixedDao().selectByPullRequest(session, pullRequest.uuid()))
98 .extracting(IssueFixedDto::issueKey, IssueFixedDto::pullRequestUuid)
99 .containsExactly(tuple("key1", pullRequest.uuid()));
103 public void execute_whenRepositoryIsEmpty_shouldNotPersistAnyFixedIssues() {
104 ProjectData projectData = db.components().insertPublicProject();
105 ComponentDto pullRequest = db.components().insertProjectBranch(projectData.getMainBranchComponent());
107 DbSession session = db.getSession();
108 db.getDbClient().issueFixedDao().insert(session, new IssueFixedDto(pullRequest.uuid(), "key1"));
111 treeRootHolder.setRoot(builder(PROJECT, 1)
112 .setUuid(pullRequest.uuid())
115 underTest.execute(new TestComputationStepContext());
117 Assertions.assertThat(db.getDbClient().issueFixedDao().selectByPullRequest(db.getSession(), pullRequest.uuid()))