3 * Copyright (C) 2009-2021 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 javax.annotation.Nullable;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
27 import org.sonar.ce.task.projectanalysis.analysis.Branch;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.component.BranchType;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.db.component.ComponentTesting;
32 import org.sonar.db.issue.IssueTesting;
33 import org.sonar.db.rule.RuleDefinitionDto;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
39 public class SiblingComponentsWithOpenIssuesTest {
41 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
44 public AnalysisMetadataHolderRule metadataHolder = new AnalysisMetadataHolderRule();
47 public DbTester db = DbTester.create();
49 private SiblingComponentsWithOpenIssues underTest;
51 private ComponentDto branch1;
52 private ComponentDto fileWithNoIssuesOnBranch1;
53 private ComponentDto fileWithOneOpenIssueOnBranch1Pr1;
54 private ComponentDto fileWithOneResolvedIssueOnBranch1Pr1;
55 private ComponentDto fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1;
56 private ComponentDto fileXWithOneResolvedIssueOnBranch1Pr1;
57 private ComponentDto fileXWithOneResolvedIssueOnBranch1Pr2;
59 private ComponentDto branch2;
60 private ComponentDto fileWithOneOpenIssueOnBranch2Pr1;
61 private ComponentDto fileWithOneResolvedIssueOnBranch2Pr1;
62 private ComponentDto branch1pr1;
66 ComponentDto project = db.components().insertPublicProject();
68 branch1 = db.components().insertProjectBranch(project, b -> b.setKey("branch1"), b -> b.setBranchType(BranchType.BRANCH));
69 branch1pr1 = db.components().insertProjectBranch(project,
70 b -> b.setKey("branch1pr1"),
71 b -> b.setBranchType(BranchType.PULL_REQUEST),
72 b -> b.setMergeBranchUuid(branch1.uuid()));
73 ComponentDto branch1pr2 = db.components().insertProjectBranch(project,
74 b -> b.setKey("branch1pr2"),
75 b -> b.setBranchType(BranchType.PULL_REQUEST),
76 b -> b.setMergeBranchUuid(branch1.uuid()));
78 fileWithNoIssuesOnBranch1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1, null));
80 RuleDefinitionDto rule = db.rules().insert();
82 fileWithOneOpenIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null));
83 db.issues().insert(rule, branch1pr1, fileWithOneOpenIssueOnBranch1Pr1);
85 fileWithOneResolvedIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null));
86 db.issues().insert(rule, branch1pr1, fileWithOneResolvedIssueOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
88 fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null));
89 db.issues().insert(rule, branch1pr1, fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1);
90 db.issues().insert(rule, branch1pr1, fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
92 String fileKey = "file-x";
93 fileXWithOneResolvedIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null)
94 .setDbKey(fileKey + ":BRANCH:branch1pr1"));
95 db.issues().insert(rule, branch1pr1, fileXWithOneResolvedIssueOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
97 fileXWithOneResolvedIssueOnBranch1Pr2 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr2, null)
98 .setDbKey(fileKey + ":BRANCH:branch1pr2"));
99 db.issues().insert(rule, branch1pr2, fileXWithOneResolvedIssueOnBranch1Pr2, i -> i.setStatus("RESOLVED"));
101 branch2 = db.components().insertProjectBranch(project, b -> b.setKey("branch2"), b -> b.setBranchType(BranchType.BRANCH));
102 ComponentDto branch2pr1 = db.components().insertProjectBranch(project,
103 b -> b.setKey("branch2pr1"),
104 b -> b.setBranchType(BranchType.PULL_REQUEST),
105 b -> b.setMergeBranchUuid(branch2.uuid()));
107 fileWithOneOpenIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1, null));
108 db.issues().insert(rule, branch2pr1, fileWithOneOpenIssueOnBranch2Pr1);
110 fileWithOneResolvedIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1, null));
111 db.issues().insert(rule, branch2pr1, fileWithOneResolvedIssueOnBranch2Pr1, i -> i.setStatus("RESOLVED"));
113 underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
117 public void should_find_sibling_components_with_open_issues_for_branch1() {
119 setBranch(BranchType.BRANCH);
121 assertThat(underTest.getUuids(fileWithNoIssuesOnBranch1.getKey())).isEmpty();
122 assertThat(underTest.getUuids(fileWithOneOpenIssueOnBranch1Pr1.getKey())).containsOnly(fileWithOneOpenIssueOnBranch1Pr1.uuid());
123 assertThat(underTest.getUuids(fileWithOneResolvedIssueOnBranch1Pr1.getKey())).containsOnly(fileWithOneResolvedIssueOnBranch1Pr1.uuid());
124 assertThat(underTest.getUuids(fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1.getKey())).containsOnly(fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1.uuid());
126 assertThat(fileXWithOneResolvedIssueOnBranch1Pr1.getKey()).isEqualTo(fileXWithOneResolvedIssueOnBranch1Pr2.getKey());
127 assertThat(underTest.getUuids(fileXWithOneResolvedIssueOnBranch1Pr1.getKey())).containsOnly(
128 fileXWithOneResolvedIssueOnBranch1Pr1.uuid(),
129 fileXWithOneResolvedIssueOnBranch1Pr2.uuid());
133 public void should_find_sibling_components_with_open_issues_for_pr1() {
135 setBranch(BranchType.PULL_REQUEST, branch1.uuid());
137 assertThat(underTest.getUuids(fileWithNoIssuesOnBranch1.getKey())).isEmpty();
138 assertThat(underTest.getUuids(fileWithOneOpenIssueOnBranch1Pr1.getKey())).isEmpty();
139 assertThat(underTest.getUuids(fileWithOneResolvedIssueOnBranch1Pr1.getKey())).isEmpty();
140 assertThat(underTest.getUuids(fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1.getKey())).isEmpty();
142 assertThat(underTest.getUuids(fileXWithOneResolvedIssueOnBranch1Pr1.getKey())).containsOnly(
143 fileXWithOneResolvedIssueOnBranch1Pr2.uuid());
147 public void should_find_sibling_components_with_open_issues_for_branch2() {
149 setBranch(BranchType.BRANCH);
151 underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
153 assertThat(underTest.getUuids(fileWithOneResolvedIssueOnBranch1Pr1.getKey())).isEmpty();
154 assertThat(underTest.getUuids(fileWithOneResolvedIssueOnBranch2Pr1.getKey())).containsOnly(fileWithOneResolvedIssueOnBranch2Pr1.uuid());
155 assertThat(underTest.getUuids(fileWithOneOpenIssueOnBranch2Pr1.getKey())).containsOnly(fileWithOneOpenIssueOnBranch2Pr1.uuid());
159 public void should_find_sibling_components_with_open_issues_from_pullrequest() {
160 ComponentDto project = db.components().insertPublicProject();
162 setBranch(BranchType.BRANCH);
164 ComponentDto pullRequest = db.components().insertProjectBranch(project,
165 b -> b.setBranchType(BranchType.PULL_REQUEST),
166 b -> b.setMergeBranchUuid(project.uuid()));
168 RuleDefinitionDto rule = db.rules().insert();
170 ComponentDto fileWithResolvedIssueOnPullrequest = db.components().insertComponent(ComponentTesting.newFileDto(pullRequest, null));
171 db.issues().insert(rule, pullRequest, fileWithResolvedIssueOnPullrequest, i -> i.setStatus("RESOLVED"));
172 underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
174 assertThat(underTest.getUuids(fileWithResolvedIssueOnPullrequest.getKey())).hasSize(1);
178 public void should_not_find_sibling_components_on_derived_branch() {
179 ComponentDto project = db.components().insertPublicProject();
181 setBranch(BranchType.BRANCH);
183 ComponentDto derivedBranch = db.components().insertProjectBranch(project,
184 b -> b.setBranchType(BranchType.BRANCH),
185 b -> b.setMergeBranchUuid(project.uuid()));
187 RuleDefinitionDto rule = db.rules().insert();
189 ComponentDto fileWithResolvedIssueOnDerivedBranch = db.components().insertComponent(ComponentTesting.newFileDto(derivedBranch, null));
190 db.issues().insert(rule, derivedBranch, fileWithResolvedIssueOnDerivedBranch, i -> i.setStatus("RESOLVED"));
192 underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
194 assertThat(underTest.getUuids(fileWithResolvedIssueOnDerivedBranch.getKey())).isEmpty();
197 private void setRoot(ComponentDto componentDto) {
198 Component root = mock(Component.class);
199 when(root.getUuid()).thenReturn(componentDto.uuid());
200 treeRootHolder.setRoot(root);
203 private void setBranch(BranchType currentBranchType) {
204 setBranch(currentBranchType, null);
207 private void setBranch(BranchType currentBranchType, @Nullable String mergeBranchUuid) {
208 Branch branch = mock(Branch.class);
209 when(branch.getType()).thenReturn(currentBranchType);
210 when(branch.getReferenceBranchUuid()).thenReturn(mergeBranchUuid);
211 metadataHolder.setBranch(branch);