]> source.dussan.org Git - sonarqube.git/blob
4c4ba86943e99ec340c77642499bd192974e293d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.issue;
21
22 import com.google.common.base.Optional;
23 import java.util.Collections;
24 import java.util.List;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.TemporaryFolder;
29 import org.mockito.ArgumentCaptor;
30 import org.sonar.api.config.internal.MapSettings;
31 import org.sonar.api.issue.Issue;
32 import org.sonar.api.rule.RuleKey;
33 import org.sonar.api.rule.Severity;
34 import org.sonar.api.utils.System2;
35 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
36 import org.sonar.ce.task.projectanalysis.analysis.Branch;
37 import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
38 import org.sonar.ce.task.projectanalysis.component.Component;
39 import org.sonar.ce.task.projectanalysis.component.MergeAndTargetBranchComponentUuids;
40 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
41 import org.sonar.ce.task.projectanalysis.component.ReportModulesPath;
42 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
43 import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitor;
44 import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository;
45 import org.sonar.ce.task.projectanalysis.issue.commonrule.CommonRuleEngineImpl;
46 import org.sonar.ce.task.projectanalysis.issue.filter.IssueFilter;
47 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolder;
48 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolderRule;
49 import org.sonar.ce.task.projectanalysis.qualityprofile.AlwaysActiveRulesHolderImpl;
50 import org.sonar.ce.task.projectanalysis.source.NewLinesRepository;
51 import org.sonar.ce.task.projectanalysis.source.SourceLinesHashRepository;
52 import org.sonar.ce.task.projectanalysis.source.SourceLinesRepositoryRule;
53 import org.sonar.core.issue.DefaultIssue;
54 import org.sonar.core.issue.tracking.Tracker;
55 import org.sonar.db.DbClient;
56 import org.sonar.db.DbTester;
57 import org.sonar.db.component.BranchType;
58 import org.sonar.db.component.ComponentDto;
59 import org.sonar.db.component.ComponentTesting;
60 import org.sonar.db.issue.IssueDto;
61 import org.sonar.db.issue.IssueTesting;
62 import org.sonar.db.rule.RuleDto;
63 import org.sonar.db.rule.RuleTesting;
64 import org.sonar.scanner.protocol.Constants;
65 import org.sonar.scanner.protocol.output.ScannerReport;
66 import org.sonar.server.issue.IssueFieldsSetter;
67
68 import static com.google.common.collect.Lists.newArrayList;
69 import static java.util.Arrays.asList;
70 import static org.assertj.core.api.Assertions.assertThat;
71 import static org.mockito.ArgumentMatchers.any;
72 import static org.mockito.ArgumentMatchers.eq;
73 import static org.mockito.Mockito.mock;
74 import static org.mockito.Mockito.verify;
75 import static org.mockito.Mockito.when;
76
77 public class IntegrateIssuesVisitorTest {
78
79   private static final String FILE_UUID = "FILE_UUID";
80   private static final String FILE_UUID_ON_BRANCH = "FILE_UUID_BRANCH";
81   private static final String FILE_KEY = "FILE_KEY";
82   private static final int FILE_REF = 2;
83
84   private static final Component FILE = ReportComponent.builder(Component.Type.FILE, FILE_REF)
85     .setKey(FILE_KEY)
86     .setUuid(FILE_UUID)
87     .build();
88
89   private static final String PROJECT_KEY = "PROJECT_KEY";
90   private static final String PROJECT_UUID = "PROJECT_UUID";
91   private static final String PROJECT_UUID_ON_BRANCH = "PROJECT_UUID_BRANCH";
92   private static final int PROJECT_REF = 1;
93   private static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, PROJECT_REF)
94     .setKey(PROJECT_KEY)
95     .setUuid(PROJECT_UUID)
96     .addChildren(FILE)
97     .build();
98
99   @Rule
100   public TemporaryFolder temp = new TemporaryFolder();
101   @Rule
102   public DbTester dbTester = DbTester.create(System2.INSTANCE);
103   @Rule
104   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
105   @Rule
106   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
107   @Rule
108   public ActiveRulesHolderRule activeRulesHolderRule = new ActiveRulesHolderRule();
109   @Rule
110   public RuleRepositoryRule ruleRepositoryRule = new RuleRepositoryRule();
111   @Rule
112   public SourceLinesRepositoryRule fileSourceRepository = new SourceLinesRepositoryRule();
113
114   private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
115   private IssueFilter issueFilter = mock(IssueFilter.class);
116   private MovedFilesRepository movedFilesRepository = mock(MovedFilesRepository.class);
117   private IssueLifecycle issueLifecycle = mock(IssueLifecycle.class);
118   private IssueVisitor issueVisitor = mock(IssueVisitor.class);
119   private MergeAndTargetBranchComponentUuids mergeBranchComponentsUuids = mock(MergeAndTargetBranchComponentUuids.class);
120   private SiblingsIssueMerger issueStatusCopier = mock(SiblingsIssueMerger.class);
121   private MergeAndTargetBranchComponentUuids mergeAndTargetBranchComponentUuids = mock(MergeAndTargetBranchComponentUuids.class);
122   private SourceLinesHashRepository sourceLinesHash = mock(SourceLinesHashRepository.class);
123   private NewLinesRepository newLinesRepository = mock(NewLinesRepository.class);
124
125   private ArgumentCaptor<DefaultIssue> defaultIssueCaptor;
126
127   private ComponentIssuesLoader issuesLoader = new ComponentIssuesLoader(dbTester.getDbClient(), ruleRepositoryRule, activeRulesHolderRule, new MapSettings().asConfig(), System2.INSTANCE);
128   private IssueTrackingDelegator trackingDelegator;
129   private TrackerExecution tracker;
130   private ShortBranchOrPullRequestTrackerExecution shortBranchTracker;
131   private MergeBranchTrackerExecution mergeBranchTracker;
132   private ActiveRulesHolder activeRulesHolder = new AlwaysActiveRulesHolderImpl();
133   private IssueCache issueCache;
134
135   private TypeAwareVisitor underTest;
136
137   @Before
138   public void setUp() throws Exception {
139     IssueVisitors issueVisitors = new IssueVisitors(new IssueVisitor[] {issueVisitor});
140
141     defaultIssueCaptor = ArgumentCaptor.forClass(DefaultIssue.class);
142     when(movedFilesRepository.getOriginalFile(any(Component.class))).thenReturn(Optional.absent());
143
144     DbClient dbClient = dbTester.getDbClient();
145     TrackerRawInputFactory rawInputFactory = new TrackerRawInputFactory(treeRootHolder, reportReader, sourceLinesHash, new CommonRuleEngineImpl(),
146       issueFilter, ruleRepositoryRule, activeRulesHolder);
147     TrackerBaseInputFactory baseInputFactory = new TrackerBaseInputFactory(issuesLoader, dbClient, movedFilesRepository, mock(ReportModulesPath.class), analysisMetadataHolder, new IssueFieldsSetter(), mock(ComponentsWithUnprocessedIssues.class));
148     TrackerMergeOrTargetBranchInputFactory mergeInputFactory = new TrackerMergeOrTargetBranchInputFactory(issuesLoader, mergeBranchComponentsUuids, dbClient);
149     ClosedIssuesInputFactory closedIssuesInputFactory = new ClosedIssuesInputFactory(issuesLoader, dbClient, movedFilesRepository);
150     tracker = new TrackerExecution(baseInputFactory, rawInputFactory, closedIssuesInputFactory, new Tracker<>(), issuesLoader, analysisMetadataHolder);
151     shortBranchTracker = new ShortBranchOrPullRequestTrackerExecution(baseInputFactory, rawInputFactory, mergeInputFactory, new Tracker<>(), newLinesRepository);
152     mergeBranchTracker = new MergeBranchTrackerExecution(rawInputFactory, mergeInputFactory, new Tracker<>());
153
154     trackingDelegator = new IssueTrackingDelegator(shortBranchTracker, mergeBranchTracker, tracker, analysisMetadataHolder);
155     treeRootHolder.setRoot(PROJECT);
156     issueCache = new IssueCache(temp.newFile(), System2.INSTANCE);
157     when(issueFilter.accept(any(DefaultIssue.class), eq(FILE))).thenReturn(true);
158     underTest = new IntegrateIssuesVisitor(issueCache, issueLifecycle, issueVisitors, trackingDelegator, issueStatusCopier, mergeAndTargetBranchComponentUuids);
159   }
160
161   @Test
162   public void process_new_issue() {
163     when(analysisMetadataHolder.isLongLivingBranch()).thenReturn(true);
164     ScannerReport.Issue reportIssue = ScannerReport.Issue.newBuilder()
165       .setMsg("the message")
166       .setRuleRepository("xoo")
167       .setRuleKey("S001")
168       .setSeverity(Constants.Severity.BLOCKER)
169       .build();
170     reportReader.putIssues(FILE_REF, asList(reportIssue));
171     fileSourceRepository.addLine(FILE_REF, "line1");
172
173     underTest.visitAny(FILE);
174
175     verify(issueLifecycle).initNewOpenIssue(defaultIssueCaptor.capture());
176     DefaultIssue capturedIssue = defaultIssueCaptor.getValue();
177     assertThat(capturedIssue.ruleKey().rule()).isEqualTo("S001");
178
179     verify(issueStatusCopier).tryMerge(FILE, Collections.singletonList(capturedIssue));
180
181     verify(issueLifecycle).doAutomaticTransition(capturedIssue);
182
183     assertThat(newArrayList(issueCache.traverse())).hasSize(1);
184   }
185
186   @Test
187   public void process_existing_issue() {
188
189     RuleKey ruleKey = RuleTesting.XOO_X1;
190     // Issue from db has severity major
191     addBaseIssue(ruleKey);
192
193     // Issue from report has severity blocker
194     ScannerReport.Issue reportIssue = ScannerReport.Issue.newBuilder()
195       .setMsg("the message")
196       .setRuleRepository(ruleKey.repository())
197       .setRuleKey(ruleKey.rule())
198       .setSeverity(Constants.Severity.BLOCKER)
199       .build();
200     reportReader.putIssues(FILE_REF, asList(reportIssue));
201     fileSourceRepository.addLine(FILE_REF, "line1");
202
203     underTest.visitAny(FILE);
204
205     ArgumentCaptor<DefaultIssue> rawIssueCaptor = ArgumentCaptor.forClass(DefaultIssue.class);
206     ArgumentCaptor<DefaultIssue> baseIssueCaptor = ArgumentCaptor.forClass(DefaultIssue.class);
207     verify(issueLifecycle).mergeExistingOpenIssue(rawIssueCaptor.capture(), baseIssueCaptor.capture());
208     assertThat(rawIssueCaptor.getValue().severity()).isEqualTo(Severity.BLOCKER);
209     assertThat(baseIssueCaptor.getValue().severity()).isEqualTo(Severity.MAJOR);
210
211     verify(issueLifecycle).doAutomaticTransition(defaultIssueCaptor.capture());
212     assertThat(defaultIssueCaptor.getValue().ruleKey()).isEqualTo(ruleKey);
213     List<DefaultIssue> issues = newArrayList(issueCache.traverse());
214     assertThat(issues).hasSize(1);
215     assertThat(issues.get(0).severity()).isEqualTo(Severity.BLOCKER);
216
217   }
218
219   @Test
220   public void execute_issue_visitors() {
221     ScannerReport.Issue reportIssue = ScannerReport.Issue.newBuilder()
222       .setMsg("the message")
223       .setRuleRepository("xoo")
224       .setRuleKey("S001")
225       .setSeverity(Constants.Severity.BLOCKER)
226       .build();
227     reportReader.putIssues(FILE_REF, asList(reportIssue));
228     fileSourceRepository.addLine(FILE_REF, "line1");
229
230     underTest.visitAny(FILE);
231
232     verify(issueVisitor).beforeComponent(FILE);
233     verify(issueVisitor).afterComponent(FILE);
234     verify(issueVisitor).onIssue(eq(FILE), defaultIssueCaptor.capture());
235     assertThat(defaultIssueCaptor.getValue().ruleKey().rule()).isEqualTo("S001");
236   }
237
238   @Test
239   public void close_unmatched_base_issue() {
240     RuleKey ruleKey = RuleTesting.XOO_X1;
241     addBaseIssue(ruleKey);
242
243     // No issue in the report
244
245     underTest.visitAny(FILE);
246
247     verify(issueLifecycle).doAutomaticTransition(defaultIssueCaptor.capture());
248     assertThat(defaultIssueCaptor.getValue().isBeingClosed()).isTrue();
249     List<DefaultIssue> issues = newArrayList(issueCache.traverse());
250     assertThat(issues).hasSize(1);
251   }
252
253   @Test
254   public void remove_uuid_of_original_file_from_componentsWithUnprocessedIssues_if_component_has_one() {
255     String originalFileUuid = "original file uuid";
256     when(movedFilesRepository.getOriginalFile(FILE))
257       .thenReturn(Optional.of(new MovedFilesRepository.OriginalFile(4851, originalFileUuid, "original file key")));
258
259     underTest.visitAny(FILE);
260   }
261
262   @Test
263   public void copy_issues_when_creating_new_long_living_branch() {
264
265     when(mergeBranchComponentsUuids.getMergeBranchComponentUuid(FILE_KEY)).thenReturn(FILE_UUID_ON_BRANCH);
266     when(mergeAndTargetBranchComponentUuids.getMergeBranchName()).thenReturn("master");
267
268     when(analysisMetadataHolder.isLongLivingBranch()).thenReturn(true);
269     when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(true);
270     Branch branch = mock(Branch.class);
271     when(branch.isMain()).thenReturn(false);
272     when(branch.getType()).thenReturn(BranchType.LONG);
273     when(analysisMetadataHolder.getBranch()).thenReturn(branch);
274
275     RuleKey ruleKey = RuleTesting.XOO_X1;
276     // Issue from main branch has severity major
277     addBaseIssueOnBranch(ruleKey);
278
279     // Issue from report has severity blocker
280     ScannerReport.Issue reportIssue = ScannerReport.Issue.newBuilder()
281       .setMsg("the message")
282       .setRuleRepository(ruleKey.repository())
283       .setRuleKey(ruleKey.rule())
284       .setSeverity(Constants.Severity.BLOCKER)
285       .build();
286     reportReader.putIssues(FILE_REF, asList(reportIssue));
287     fileSourceRepository.addLine(FILE_REF, "line1");
288
289     underTest.visitAny(FILE);
290
291     ArgumentCaptor<DefaultIssue> rawIssueCaptor = ArgumentCaptor.forClass(DefaultIssue.class);
292     ArgumentCaptor<DefaultIssue> baseIssueCaptor = ArgumentCaptor.forClass(DefaultIssue.class);
293     verify(issueLifecycle).copyExistingOpenIssueFromLongLivingBranch(rawIssueCaptor.capture(), baseIssueCaptor.capture(), eq("master"));
294     assertThat(rawIssueCaptor.getValue().severity()).isEqualTo(Severity.BLOCKER);
295     assertThat(baseIssueCaptor.getValue().severity()).isEqualTo(Severity.MAJOR);
296
297     verify(issueLifecycle).doAutomaticTransition(defaultIssueCaptor.capture());
298     assertThat(defaultIssueCaptor.getValue().ruleKey()).isEqualTo(ruleKey);
299     List<DefaultIssue> issues = newArrayList(issueCache.traverse());
300     assertThat(issues).hasSize(1);
301     assertThat(issues.get(0).severity()).isEqualTo(Severity.BLOCKER);
302   }
303
304   private void addBaseIssue(RuleKey ruleKey) {
305     ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), PROJECT_UUID).setDbKey(PROJECT_KEY);
306     ComponentDto file = ComponentTesting.newFileDto(project, null, FILE_UUID).setDbKey(FILE_KEY);
307     dbTester.getDbClient().componentDao().insert(dbTester.getSession(), project, file);
308
309     RuleDto ruleDto = RuleTesting.newDto(ruleKey);
310     dbTester.rules().insertRule(ruleDto);
311     ruleRepositoryRule.add(ruleKey);
312
313     IssueDto issue = IssueTesting.newDto(ruleDto, file, project)
314       .setKee("ISSUE")
315       .setStatus(Issue.STATUS_OPEN)
316       .setSeverity(Severity.MAJOR);
317     dbTester.getDbClient().issueDao().insert(dbTester.getSession(), issue);
318     dbTester.getSession().commit();
319   }
320
321   private void addBaseIssueOnBranch(RuleKey ruleKey) {
322     ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), PROJECT_UUID_ON_BRANCH).setDbKey(PROJECT_KEY);
323     ComponentDto file = ComponentTesting.newFileDto(project, null, FILE_UUID_ON_BRANCH).setDbKey(FILE_KEY);
324     dbTester.getDbClient().componentDao().insert(dbTester.getSession(), project, file);
325
326     RuleDto ruleDto = RuleTesting.newDto(ruleKey);
327     dbTester.rules().insertRule(ruleDto);
328     ruleRepositoryRule.add(ruleKey);
329
330     IssueDto issue = IssueTesting.newDto(ruleDto, file, project)
331       .setKee("ISSUE")
332       .setStatus(Issue.STATUS_OPEN)
333       .setSeverity(Severity.MAJOR);
334     dbTester.getDbClient().issueDao().insert(dbTester.getSession(), issue);
335     dbTester.getSession().commit();
336   }
337 }