]> source.dussan.org Git - sonarqube.git/blob
98d4c7880bc98f834ea7d0da85f587574b220e1f
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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 SiblingComponentsWithOpenIssuesIT {
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().getMainBranchComponent();
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));
81
82     RuleDto rule = db.rules().insert();
83
84     fileWithOneOpenIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1));
85     db.issues().insert(rule, branch1pr1, fileWithOneOpenIssueOnBranch1Pr1);
86
87     fileWithOneResolvedIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1));
88     db.issues().insert(rule, branch1pr1, fileWithOneResolvedIssueOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
89
90     fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1));
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)
96       .setKey(fileKey));
97     db.issues().insert(rule, branch1pr1, fileXWithOneResolvedIssueOnBranch1Pr1, i -> i.setStatus("RESOLVED"));
98
99     fileXWithOneResolvedIssueOnBranch1Pr2 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr2)
100       .setKey(fileKey));
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));
110     db.issues().insert(rule, branch2pr1, fileWithOneOpenIssueOnBranch2Pr1);
111
112     fileWithOneResolvedIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1));
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(fileXWithOneResolvedIssueOnBranch1Pr2.uuid());
145   }
146
147   @Test
148   public void should_find_sibling_components_with_open_issues_for_branch2() {
149     setRoot(branch2);
150     setBranch(BranchType.BRANCH);
151
152     underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
153
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());
157   }
158
159   @Test
160   public void should_find_sibling_components_with_open_issues_from_pullrequest() {
161     ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
162     setRoot(project);
163     setBranch(BranchType.BRANCH);
164
165     ComponentDto pullRequest = db.components().insertProjectBranch(project,
166       b -> b.setBranchType(BranchType.PULL_REQUEST),
167       b -> b.setMergeBranchUuid(project.uuid()));
168
169     RuleDto rule = db.rules().insert();
170
171     ComponentDto fileWithResolvedIssueOnPullrequest = db.components().insertComponent(ComponentTesting.newFileDto(pullRequest));
172     db.issues().insert(rule, pullRequest, fileWithResolvedIssueOnPullrequest, i -> i.setStatus("RESOLVED"));
173     underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
174
175     assertThat(underTest.getUuids(fileWithResolvedIssueOnPullrequest.getKey())).hasSize(1);
176   }
177
178   @Test
179   public void should_not_find_sibling_components_on_derived_branch() {
180     ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
181     setRoot(project);
182     setBranch(BranchType.BRANCH);
183
184     ComponentDto derivedBranch = db.components().insertProjectBranch(project,
185       b -> b.setBranchType(BranchType.BRANCH),
186       b -> b.setMergeBranchUuid(project.uuid()));
187
188     RuleDto rule = db.rules().insert();
189
190     ComponentDto fileWithResolvedIssueOnDerivedBranch = db.components().insertComponent(ComponentTesting.newFileDto(derivedBranch));
191     db.issues().insert(rule, derivedBranch, fileWithResolvedIssueOnDerivedBranch, i -> i.setStatus("RESOLVED"));
192
193     underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
194
195     assertThat(underTest.getUuids(fileWithResolvedIssueOnDerivedBranch.getKey())).isEmpty();
196   }
197
198   private void setRoot(ComponentDto componentDto) {
199     Component root = mock(Component.class);
200     when(root.getUuid()).thenReturn(componentDto.uuid());
201     treeRootHolder.setRoot(root);
202   }
203
204   private void setBranch(BranchType currentBranchType) {
205     setBranch(currentBranchType, null);
206   }
207
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);
213   }
214 }