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.component;
22 import java.util.Collections;
23 import javax.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
28 import org.sonar.ce.task.projectanalysis.analysis.Branch;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.component.BranchType;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.component.ComponentTesting;
33 import org.sonar.db.rule.RuleDto;
34 import org.sonar.server.project.Project;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
40 public class SiblingComponentsWithOpenIssuesIT {
42 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
45 public AnalysisMetadataHolderRule metadataHolder = new AnalysisMetadataHolderRule();
48 public DbTester db = DbTester.create();
50 private SiblingComponentsWithOpenIssues underTest;
52 private ComponentDto branch1;
53 private ComponentDto fileWithNoIssuesOnBranch1;
54 private ComponentDto fileWithOneOpenIssueOnBranch1Pr1;
55 private ComponentDto fileWithOneResolvedIssueOnBranch1Pr1;
56 private ComponentDto fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1;
57 private ComponentDto fileXWithOneResolvedIssueOnBranch1Pr1;
58 private ComponentDto fileXWithOneResolvedIssueOnBranch1Pr2;
60 private ComponentDto branch2;
61 private ComponentDto fileWithOneOpenIssueOnBranch2Pr1;
62 private ComponentDto fileWithOneResolvedIssueOnBranch2Pr1;
63 private ComponentDto branch1pr1;
67 ComponentDto project = db.components().insertPublicProject();
68 metadataHolder.setProject(new Project(project.uuid(), project.getKey(), project.name(), project.description(), Collections.emptyList()));
70 branch1 = db.components().insertProjectBranch(project, b -> b.setKey("branch1"), b -> b.setBranchType(BranchType.BRANCH));
71 branch1pr1 = db.components().insertProjectBranch(project,
72 b -> b.setKey("branch1pr1"),
73 b -> b.setBranchType(BranchType.PULL_REQUEST),
74 b -> b.setMergeBranchUuid(branch1.uuid()));
75 ComponentDto branch1pr2 = db.components().insertProjectBranch(project,
76 b -> b.setKey("branch1pr2"),
77 b -> b.setBranchType(BranchType.PULL_REQUEST),
78 b -> b.setMergeBranchUuid(branch1.uuid()));
80 fileWithNoIssuesOnBranch1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1, null));
82 RuleDto rule = db.rules().insert();
84 fileWithOneOpenIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null));
85 db.issues().insert(rule, branch1pr1, fileWithOneOpenIssueOnBranch1Pr1);
87 fileWithOneResolvedIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null));
88 db.issues().insert(rule, branch1pr1, fileWithOneResolvedIssueOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
90 fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null));
91 db.issues().insert(rule, branch1pr1, fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1);
92 db.issues().insert(rule, branch1pr1, fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
94 String fileKey = "file-x";
95 fileXWithOneResolvedIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null)
97 db.issues().insert(rule, branch1pr1, fileXWithOneResolvedIssueOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
99 fileXWithOneResolvedIssueOnBranch1Pr2 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr2, null)
101 db.issues().insert(rule, branch1pr2, fileXWithOneResolvedIssueOnBranch1Pr2, i -> i.setStatus("RESOLVED"));
103 branch2 = db.components().insertProjectBranch(project, b -> b.setKey("branch2"), b -> b.setBranchType(BranchType.BRANCH));
104 ComponentDto branch2pr1 = db.components().insertProjectBranch(project,
105 b -> b.setKey("branch2pr1"),
106 b -> b.setBranchType(BranchType.PULL_REQUEST),
107 b -> b.setMergeBranchUuid(branch2.uuid()));
109 fileWithOneOpenIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1, null));
110 db.issues().insert(rule, branch2pr1, fileWithOneOpenIssueOnBranch2Pr1);
112 fileWithOneResolvedIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1, null));
113 db.issues().insert(rule, branch2pr1, fileWithOneResolvedIssueOnBranch2Pr1, i -> i.setStatus("RESOLVED"));
115 underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
119 public void should_find_sibling_components_with_open_issues_for_branch1() {
121 setBranch(BranchType.BRANCH);
123 assertThat(underTest.getUuids(fileWithNoIssuesOnBranch1.getKey())).isEmpty();
124 assertThat(underTest.getUuids(fileWithOneOpenIssueOnBranch1Pr1.getKey())).containsOnly(fileWithOneOpenIssueOnBranch1Pr1.uuid());
125 assertThat(underTest.getUuids(fileWithOneResolvedIssueOnBranch1Pr1.getKey())).containsOnly(fileWithOneResolvedIssueOnBranch1Pr1.uuid());
126 assertThat(underTest.getUuids(fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1.getKey())).containsOnly(fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1.uuid());
128 assertThat(fileXWithOneResolvedIssueOnBranch1Pr1.getKey()).isEqualTo(fileXWithOneResolvedIssueOnBranch1Pr2.getKey());
129 assertThat(underTest.getUuids(fileXWithOneResolvedIssueOnBranch1Pr1.getKey())).containsOnly(
130 fileXWithOneResolvedIssueOnBranch1Pr1.uuid(),
131 fileXWithOneResolvedIssueOnBranch1Pr2.uuid());
135 public void should_find_sibling_components_with_open_issues_for_pr1() {
137 setBranch(BranchType.PULL_REQUEST, branch1.uuid());
139 assertThat(underTest.getUuids(fileWithNoIssuesOnBranch1.getKey())).isEmpty();
140 assertThat(underTest.getUuids(fileWithOneOpenIssueOnBranch1Pr1.getKey())).isEmpty();
141 assertThat(underTest.getUuids(fileWithOneResolvedIssueOnBranch1Pr1.getKey())).isEmpty();
142 assertThat(underTest.getUuids(fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1.getKey())).isEmpty();
144 assertThat(underTest.getUuids(fileXWithOneResolvedIssueOnBranch1Pr1.getKey())).containsOnly(fileXWithOneResolvedIssueOnBranch1Pr2.uuid());
148 public void should_find_sibling_components_with_open_issues_for_branch2() {
150 setBranch(BranchType.BRANCH);
152 underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
154 assertThat(underTest.getUuids(fileWithOneResolvedIssueOnBranch1Pr1.getKey())).isEmpty();
155 assertThat(underTest.getUuids(fileWithOneResolvedIssueOnBranch2Pr1.getKey())).containsOnly(fileWithOneResolvedIssueOnBranch2Pr1.uuid());
156 assertThat(underTest.getUuids(fileWithOneOpenIssueOnBranch2Pr1.getKey())).containsOnly(fileWithOneOpenIssueOnBranch2Pr1.uuid());
160 public void should_find_sibling_components_with_open_issues_from_pullrequest() {
161 ComponentDto project = db.components().insertPublicProject();
163 setBranch(BranchType.BRANCH);
165 ComponentDto pullRequest = db.components().insertProjectBranch(project,
166 b -> b.setBranchType(BranchType.PULL_REQUEST),
167 b -> b.setMergeBranchUuid(project.uuid()));
169 RuleDto rule = db.rules().insert();
171 ComponentDto fileWithResolvedIssueOnPullrequest = db.components().insertComponent(ComponentTesting.newFileDto(pullRequest, null));
172 db.issues().insert(rule, pullRequest, fileWithResolvedIssueOnPullrequest, i -> i.setStatus("RESOLVED"));
173 underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
175 assertThat(underTest.getUuids(fileWithResolvedIssueOnPullrequest.getKey())).hasSize(1);
179 public void should_not_find_sibling_components_on_derived_branch() {
180 ComponentDto project = db.components().insertPublicProject();
182 setBranch(BranchType.BRANCH);
184 ComponentDto derivedBranch = db.components().insertProjectBranch(project,
185 b -> b.setBranchType(BranchType.BRANCH),
186 b -> b.setMergeBranchUuid(project.uuid()));
188 RuleDto rule = db.rules().insert();
190 ComponentDto fileWithResolvedIssueOnDerivedBranch = db.components().insertComponent(ComponentTesting.newFileDto(derivedBranch, null));
191 db.issues().insert(rule, derivedBranch, fileWithResolvedIssueOnDerivedBranch, i -> i.setStatus("RESOLVED"));
193 underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
195 assertThat(underTest.getUuids(fileWithResolvedIssueOnDerivedBranch.getKey())).isEmpty();
198 private void setRoot(ComponentDto componentDto) {
199 Component root = mock(Component.class);
200 when(root.getUuid()).thenReturn(componentDto.uuid());
201 treeRootHolder.setRoot(root);
204 private void setBranch(BranchType currentBranchType) {
205 setBranch(currentBranchType, null);
208 private void setBranch(BranchType currentBranchType, @Nullable String mergeBranchUuid) {
209 Branch branch = mock(Branch.class);
210 when(branch.getType()).thenReturn(currentBranchType);
211 when(branch.getReferenceBranchUuid()).thenReturn(mergeBranchUuid);
212 metadataHolder.setBranch(branch);