3 * Copyright (C) 2009-2022 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.RuleDefinitionDto;
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 SiblingComponentsWithOpenIssuesTest {
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 RuleDefinitionDto 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)
96 .setDbKey(fileKey + ":BRANCH:branch1pr1"));
97 db.issues().insert(rule, branch1pr1, fileXWithOneResolvedIssueOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
99 fileXWithOneResolvedIssueOnBranch1Pr2 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr2, null)
100 .setDbKey(fileKey + ":BRANCH:branch1pr2"));
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(
145 fileXWithOneResolvedIssueOnBranch1Pr2.uuid());
149 public void should_find_sibling_components_with_open_issues_for_branch2() {
151 setBranch(BranchType.BRANCH);
153 underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
155 assertThat(underTest.getUuids(fileWithOneResolvedIssueOnBranch1Pr1.getKey())).isEmpty();
156 assertThat(underTest.getUuids(fileWithOneResolvedIssueOnBranch2Pr1.getKey())).containsOnly(fileWithOneResolvedIssueOnBranch2Pr1.uuid());
157 assertThat(underTest.getUuids(fileWithOneOpenIssueOnBranch2Pr1.getKey())).containsOnly(fileWithOneOpenIssueOnBranch2Pr1.uuid());
161 public void should_find_sibling_components_with_open_issues_from_pullrequest() {
162 ComponentDto project = db.components().insertPublicProject();
164 setBranch(BranchType.BRANCH);
166 ComponentDto pullRequest = db.components().insertProjectBranch(project,
167 b -> b.setBranchType(BranchType.PULL_REQUEST),
168 b -> b.setMergeBranchUuid(project.uuid()));
170 RuleDefinitionDto rule = db.rules().insert();
172 ComponentDto fileWithResolvedIssueOnPullrequest = db.components().insertComponent(ComponentTesting.newFileDto(pullRequest, null));
173 db.issues().insert(rule, pullRequest, fileWithResolvedIssueOnPullrequest, i -> i.setStatus("RESOLVED"));
174 underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
176 assertThat(underTest.getUuids(fileWithResolvedIssueOnPullrequest.getKey())).hasSize(1);
180 public void should_not_find_sibling_components_on_derived_branch() {
181 ComponentDto project = db.components().insertPublicProject();
183 setBranch(BranchType.BRANCH);
185 ComponentDto derivedBranch = db.components().insertProjectBranch(project,
186 b -> b.setBranchType(BranchType.BRANCH),
187 b -> b.setMergeBranchUuid(project.uuid()));
189 RuleDefinitionDto rule = db.rules().insert();
191 ComponentDto fileWithResolvedIssueOnDerivedBranch = db.components().insertComponent(ComponentTesting.newFileDto(derivedBranch, null));
192 db.issues().insert(rule, derivedBranch, fileWithResolvedIssueOnDerivedBranch, i -> i.setStatus("RESOLVED"));
194 underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
196 assertThat(underTest.getUuids(fileWithResolvedIssueOnDerivedBranch.getKey())).isEmpty();
199 private void setRoot(ComponentDto componentDto) {
200 Component root = mock(Component.class);
201 when(root.getUuid()).thenReturn(componentDto.uuid());
202 treeRootHolder.setRoot(root);
205 private void setBranch(BranchType currentBranchType) {
206 setBranch(currentBranchType, null);
209 private void setBranch(BranchType currentBranchType, @Nullable String mergeBranchUuid) {
210 Branch branch = mock(Branch.class);
211 when(branch.getType()).thenReturn(currentBranchType);
212 when(branch.getReferenceBranchUuid()).thenReturn(mergeBranchUuid);
213 metadataHolder.setBranch(branch);