]> source.dussan.org Git - sonarqube.git/blob
313cd38286141c1a4d6e72f103c3c85e364b5db1
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.component;
21
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;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38
39 public class SiblingComponentsWithOpenIssuesTest {
40   @Rule
41   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
42
43   @Rule
44   public AnalysisMetadataHolderRule metadataHolder = new AnalysisMetadataHolderRule();
45
46   @Rule
47   public DbTester db = DbTester.create();
48
49   private SiblingComponentsWithOpenIssues underTest;
50
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;
58
59   private ComponentDto branch2;
60   private ComponentDto fileWithOneOpenIssueOnBranch2Pr1;
61   private ComponentDto fileWithOneResolvedIssueOnBranch2Pr1;
62   private ComponentDto branch1pr1;
63
64   @Before
65   public void setUp() {
66     ComponentDto project = db.components().insertPublicProject();
67
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()));
77
78     fileWithNoIssuesOnBranch1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1, null));
79
80     RuleDefinitionDto rule = db.rules().insert();
81
82     fileWithOneOpenIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null));
83     db.issues().insert(rule, branch1pr1, fileWithOneOpenIssueOnBranch1Pr1);
84
85     fileWithOneResolvedIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null));
86     db.issues().insert(rule, branch1pr1, fileWithOneResolvedIssueOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
87
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"));
91
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"));
96
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"));
100
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()));
106
107     fileWithOneOpenIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1, null));
108     db.issues().insert(rule, branch2pr1, fileWithOneOpenIssueOnBranch2Pr1);
109
110     fileWithOneResolvedIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1, null));
111     db.issues().insert(rule, branch2pr1, fileWithOneResolvedIssueOnBranch2Pr1, i -> i.setStatus("RESOLVED"));
112     setRoot(branch1);
113     underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
114   }
115
116   @Test
117   public void should_find_sibling_components_with_open_issues_for_branch1() {
118     setRoot(branch1);
119     setBranch(BranchType.BRANCH);
120
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());
125
126     assertThat(fileXWithOneResolvedIssueOnBranch1Pr1.getKey()).isEqualTo(fileXWithOneResolvedIssueOnBranch1Pr2.getKey());
127     assertThat(underTest.getUuids(fileXWithOneResolvedIssueOnBranch1Pr1.getKey())).containsOnly(
128       fileXWithOneResolvedIssueOnBranch1Pr1.uuid(),
129       fileXWithOneResolvedIssueOnBranch1Pr2.uuid());
130   }
131
132   @Test
133   public void should_find_sibling_components_with_open_issues_for_pr1() {
134     setRoot(branch1pr1);
135     setBranch(BranchType.PULL_REQUEST, branch1.uuid());
136
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();
141
142     assertThat(underTest.getUuids(fileXWithOneResolvedIssueOnBranch1Pr1.getKey())).containsOnly(
143       fileXWithOneResolvedIssueOnBranch1Pr2.uuid());
144   }
145
146   @Test
147   public void should_find_sibling_components_with_open_issues_for_branch2() {
148     setRoot(branch2);
149     setBranch(BranchType.BRANCH);
150
151     underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
152
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());
156   }
157
158   @Test
159   public void should_find_sibling_components_with_open_issues_from_pullrequest() {
160     ComponentDto project = db.components().insertPublicProject();
161     setRoot(project);
162     setBranch(BranchType.BRANCH);
163
164     ComponentDto pullRequest = db.components().insertProjectBranch(project,
165       b -> b.setBranchType(BranchType.PULL_REQUEST),
166       b -> b.setMergeBranchUuid(project.uuid()));
167
168     RuleDefinitionDto rule = db.rules().insert();
169
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());
173
174     assertThat(underTest.getUuids(fileWithResolvedIssueOnPullrequest.getKey())).hasSize(1);
175   }
176
177   @Test
178   public void should_not_find_sibling_components_on_derived_branch() {
179     ComponentDto project = db.components().insertPublicProject();
180     setRoot(project);
181     setBranch(BranchType.BRANCH);
182
183     ComponentDto derivedBranch = db.components().insertProjectBranch(project,
184       b -> b.setBranchType(BranchType.BRANCH),
185       b -> b.setMergeBranchUuid(project.uuid()));
186
187     RuleDefinitionDto rule = db.rules().insert();
188
189     ComponentDto fileWithResolvedIssueOnDerivedBranch = db.components().insertComponent(ComponentTesting.newFileDto(derivedBranch, null));
190     db.issues().insert(rule, derivedBranch, fileWithResolvedIssueOnDerivedBranch, i -> i.setStatus("RESOLVED"));
191
192     underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
193
194     assertThat(underTest.getUuids(fileWithResolvedIssueOnDerivedBranch.getKey())).isEmpty();
195   }
196
197   private void setRoot(ComponentDto componentDto) {
198     Component root = mock(Component.class);
199     when(root.getUuid()).thenReturn(componentDto.uuid());
200     treeRootHolder.setRoot(root);
201   }
202
203   private void setBranch(BranchType currentBranchType) {
204     setBranch(currentBranchType, null);
205   }
206
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);
212   }
213 }