]> source.dussan.org Git - sonarqube.git/blob
c3d0c03d4df22c7e228a2908eed50c1855d6ecce
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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 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;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
39
40 public class SiblingComponentsWithOpenIssuesTest {
41   @Rule
42   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
43
44   @Rule
45   public AnalysisMetadataHolderRule metadataHolder = new AnalysisMetadataHolderRule();
46
47   @Rule
48   public DbTester db = DbTester.create();
49
50   private SiblingComponentsWithOpenIssues underTest;
51
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;
59
60   private ComponentDto branch2;
61   private ComponentDto fileWithOneOpenIssueOnBranch2Pr1;
62   private ComponentDto fileWithOneResolvedIssueOnBranch2Pr1;
63   private ComponentDto branch1pr1;
64
65   @Before
66   public void setUp() {
67     ComponentDto project = db.components().insertPublicProject();
68     metadataHolder.setProject(new Project(project.uuid(), project.getKey(), project.name(), project.description(), Collections.emptyList()));
69
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()));
79
80     fileWithNoIssuesOnBranch1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1, null));
81
82     RuleDto rule = db.rules().insert();
83
84     fileWithOneOpenIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null));
85     db.issues().insert(rule, branch1pr1, fileWithOneOpenIssueOnBranch1Pr1);
86
87     fileWithOneResolvedIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null));
88     db.issues().insert(rule, branch1pr1, fileWithOneResolvedIssueOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
89
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"));
93
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"));
98
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"));
102
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()));
108
109     fileWithOneOpenIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1, null));
110     db.issues().insert(rule, branch2pr1, fileWithOneOpenIssueOnBranch2Pr1);
111
112     fileWithOneResolvedIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1, null));
113     db.issues().insert(rule, branch2pr1, fileWithOneResolvedIssueOnBranch2Pr1, i -> i.setStatus("RESOLVED"));
114     setRoot(branch1);
115     underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
116   }
117
118   @Test
119   public void should_find_sibling_components_with_open_issues_for_branch1() {
120     setRoot(branch1);
121     setBranch(BranchType.BRANCH);
122
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());
127
128     assertThat(fileXWithOneResolvedIssueOnBranch1Pr1.getKey()).isEqualTo(fileXWithOneResolvedIssueOnBranch1Pr2.getKey());
129     assertThat(underTest.getUuids(fileXWithOneResolvedIssueOnBranch1Pr1.getKey())).containsOnly(
130       fileXWithOneResolvedIssueOnBranch1Pr1.uuid(),
131       fileXWithOneResolvedIssueOnBranch1Pr2.uuid());
132   }
133
134   @Test
135   public void should_find_sibling_components_with_open_issues_for_pr1() {
136     setRoot(branch1pr1);
137     setBranch(BranchType.PULL_REQUEST, branch1.uuid());
138
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();
143
144     assertThat(underTest.getUuids(fileXWithOneResolvedIssueOnBranch1Pr1.getKey())).containsOnly(
145       fileXWithOneResolvedIssueOnBranch1Pr2.uuid());
146   }
147
148   @Test
149   public void should_find_sibling_components_with_open_issues_for_branch2() {
150     setRoot(branch2);
151     setBranch(BranchType.BRANCH);
152
153     underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
154
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());
158   }
159
160   @Test
161   public void should_find_sibling_components_with_open_issues_from_pullrequest() {
162     ComponentDto project = db.components().insertPublicProject();
163     setRoot(project);
164     setBranch(BranchType.BRANCH);
165
166     ComponentDto pullRequest = db.components().insertProjectBranch(project,
167       b -> b.setBranchType(BranchType.PULL_REQUEST),
168       b -> b.setMergeBranchUuid(project.uuid()));
169
170     RuleDto rule = db.rules().insert();
171
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());
175
176     assertThat(underTest.getUuids(fileWithResolvedIssueOnPullrequest.getKey())).hasSize(1);
177   }
178
179   @Test
180   public void should_not_find_sibling_components_on_derived_branch() {
181     ComponentDto project = db.components().insertPublicProject();
182     setRoot(project);
183     setBranch(BranchType.BRANCH);
184
185     ComponentDto derivedBranch = db.components().insertProjectBranch(project,
186       b -> b.setBranchType(BranchType.BRANCH),
187       b -> b.setMergeBranchUuid(project.uuid()));
188
189     RuleDto rule = db.rules().insert();
190
191     ComponentDto fileWithResolvedIssueOnDerivedBranch = db.components().insertComponent(ComponentTesting.newFileDto(derivedBranch, null));
192     db.issues().insert(rule, derivedBranch, fileWithResolvedIssueOnDerivedBranch, i -> i.setStatus("RESOLVED"));
193
194     underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
195
196     assertThat(underTest.getUuids(fileWithResolvedIssueOnDerivedBranch.getKey())).isEmpty();
197   }
198
199   private void setRoot(ComponentDto componentDto) {
200     Component root = mock(Component.class);
201     when(root.getUuid()).thenReturn(componentDto.uuid());
202     treeRootHolder.setRoot(root);
203   }
204
205   private void setBranch(BranchType currentBranchType) {
206     setBranch(currentBranchType, null);
207   }
208
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);
214   }
215 }