3 * Copyright (C) 2009-2023 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.issue;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Optional;
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.FileStatuses;
40 import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
41 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
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.SourceLinesRepository;
53 import org.sonar.core.issue.DefaultIssue;
54 import org.sonar.core.issue.FieldDiffs;
55 import org.sonar.core.issue.IssueChangeContext;
56 import org.sonar.core.issue.tracking.Tracker;
57 import org.sonar.db.DbClient;
58 import org.sonar.db.DbTester;
59 import org.sonar.db.component.BranchType;
60 import org.sonar.db.component.ComponentDto;
61 import org.sonar.db.component.ComponentTesting;
62 import org.sonar.db.issue.IssueDto;
63 import org.sonar.db.issue.IssueTesting;
64 import org.sonar.db.rule.RuleDto;
65 import org.sonar.db.rule.RuleTesting;
66 import org.sonar.scanner.protocol.Constants;
67 import org.sonar.scanner.protocol.output.ScannerReport;
68 import org.sonar.server.issue.IssueFieldsSetter;
69 import org.sonar.server.issue.workflow.IssueWorkflow;
71 import static com.google.common.collect.Lists.newArrayList;
72 import static java.util.Collections.singletonList;
73 import static org.assertj.core.api.Assertions.assertThat;
74 import static org.assertj.core.api.Assertions.entry;
75 import static org.mockito.ArgumentMatchers.any;
76 import static org.mockito.ArgumentMatchers.eq;
77 import static org.mockito.Mockito.mock;
78 import static org.mockito.Mockito.verify;
79 import static org.mockito.Mockito.when;
81 public class IntegrateIssuesVisitorTest {
83 private static final String FILE_UUID = "FILE_UUID";
84 private static final String FILE_UUID_ON_BRANCH = "FILE_UUID_BRANCH";
85 private static final String FILE_KEY = "FILE_KEY";
86 private static final int FILE_REF = 2;
88 private static final Component FILE = ReportComponent.builder(Component.Type.FILE, FILE_REF)
93 private static final String PROJECT_KEY = "PROJECT_KEY";
94 private static final String PROJECT_UUID = "PROJECT_UUID";
95 private static final String PROJECT_UUID_ON_BRANCH = "PROJECT_UUID_BRANCH";
96 private static final int PROJECT_REF = 1;
97 private static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, PROJECT_REF)
99 .setUuid(PROJECT_UUID)
104 public TemporaryFolder temp = new TemporaryFolder();
106 public DbTester dbTester = DbTester.create(System2.INSTANCE);
108 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
110 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
112 public ActiveRulesHolderRule activeRulesHolderRule = new ActiveRulesHolderRule();
114 public RuleRepositoryRule ruleRepositoryRule = new RuleRepositoryRule();
116 private final AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
117 private final IssueFilter issueFilter = mock(IssueFilter.class);
118 private final MovedFilesRepository movedFilesRepository = mock(MovedFilesRepository.class);
119 private final IssueChangeContext issueChangeContext = mock(IssueChangeContext.class);
120 private final IssueLifecycle issueLifecycle = new IssueLifecycle(analysisMetadataHolder, issueChangeContext, mock(IssueWorkflow.class), new IssueFieldsSetter(),
121 mock(DebtCalculator.class), ruleRepositoryRule);
122 private final IssueVisitor issueVisitor = mock(IssueVisitor.class);
123 private final ReferenceBranchComponentUuids mergeBranchComponentsUuids = mock(ReferenceBranchComponentUuids.class);
124 private final SiblingsIssueMerger issueStatusCopier = mock(SiblingsIssueMerger.class);
125 private final ReferenceBranchComponentUuids referenceBranchComponentUuids = mock(ReferenceBranchComponentUuids.class);
126 private final SourceLinesHashRepository sourceLinesHash = mock(SourceLinesHashRepository.class);
127 private final NewLinesRepository newLinesRepository = mock(NewLinesRepository.class);
128 private final TargetBranchComponentUuids targetBranchComponentUuids = mock(TargetBranchComponentUuids.class);
129 private final SourceLinesRepository sourceLinesRepository = mock(SourceLinesRepository.class);
130 private final FileStatuses fileStatuses = mock(FileStatuses.class);
131 private ArgumentCaptor<DefaultIssue> defaultIssueCaptor;
133 private final ComponentIssuesLoader issuesLoader = new ComponentIssuesLoader(dbTester.getDbClient(), ruleRepositoryRule, activeRulesHolderRule, new MapSettings().asConfig(),
134 System2.INSTANCE, mock(IssueChangesToDeleteRepository.class));
135 private IssueTrackingDelegator trackingDelegator;
136 private TrackerExecution tracker;
137 private PullRequestTrackerExecution prBranchTracker;
138 private ReferenceBranchTrackerExecution mergeBranchTracker;
139 private final ActiveRulesHolder activeRulesHolder = new AlwaysActiveRulesHolderImpl();
140 private ProtoIssueCache protoIssueCache;
142 private TypeAwareVisitor underTest;
145 public void setUp() throws Exception {
146 IssueVisitors issueVisitors = new IssueVisitors(new IssueVisitor[] {issueVisitor});
148 defaultIssueCaptor = ArgumentCaptor.forClass(DefaultIssue.class);
149 when(movedFilesRepository.getOriginalFile(any(Component.class))).thenReturn(Optional.empty());
151 DbClient dbClient = dbTester.getDbClient();
152 TrackerRawInputFactory rawInputFactory = new TrackerRawInputFactory(treeRootHolder, reportReader, sourceLinesHash, new CommonRuleEngineImpl(), issueFilter,
153 ruleRepositoryRule, activeRulesHolder);
154 TrackerBaseInputFactory baseInputFactory = new TrackerBaseInputFactory(issuesLoader, dbClient, movedFilesRepository);
155 TrackerTargetBranchInputFactory targetInputFactory = new TrackerTargetBranchInputFactory(issuesLoader, targetBranchComponentUuids, dbClient, movedFilesRepository);
156 TrackerReferenceBranchInputFactory mergeInputFactory = new TrackerReferenceBranchInputFactory(issuesLoader, mergeBranchComponentsUuids, dbClient);
157 ClosedIssuesInputFactory closedIssuesInputFactory = new ClosedIssuesInputFactory(issuesLoader, dbClient, movedFilesRepository);
158 tracker = new TrackerExecution(baseInputFactory, closedIssuesInputFactory, new Tracker<>(), issuesLoader, analysisMetadataHolder);
159 mergeBranchTracker = new ReferenceBranchTrackerExecution(mergeInputFactory, new Tracker<>());
160 prBranchTracker = new PullRequestTrackerExecution(baseInputFactory, targetInputFactory, new Tracker<>(), newLinesRepository);
161 trackingDelegator = new IssueTrackingDelegator(prBranchTracker, mergeBranchTracker, tracker, analysisMetadataHolder);
162 treeRootHolder.setRoot(PROJECT);
163 protoIssueCache = new ProtoIssueCache(temp.newFile(), System2.INSTANCE);
164 when(issueFilter.accept(any(DefaultIssue.class), eq(FILE))).thenReturn(true);
165 when(issueChangeContext.date()).thenReturn(new Date());
166 underTest = new IntegrateIssuesVisitor(protoIssueCache, rawInputFactory, baseInputFactory, issueLifecycle, issueVisitors, trackingDelegator, issueStatusCopier,
167 referenceBranchComponentUuids, mock(PullRequestSourceBranchMerger.class), fileStatuses);
171 public void process_new_issue() {
172 ruleRepositoryRule.add(RuleKey.of("xoo", "S001"));
173 when(analysisMetadataHolder.isBranch()).thenReturn(true);
174 ScannerReport.Issue reportIssue = ScannerReport.Issue.newBuilder()
175 .setMsg("the message")
176 .setRuleRepository("xoo")
178 .setSeverity(Constants.Severity.BLOCKER)
180 reportReader.putIssues(FILE_REF, singletonList(reportIssue));
182 underTest.visitAny(FILE);
184 assertThat(newArrayList(protoIssueCache.traverse())).hasSize(1);
188 public void process_existing_issue() {
189 RuleKey ruleKey = RuleTesting.XOO_X1;
190 // Issue from db has severity major
191 addBaseIssue(ruleKey);
193 // Issue from report has severity blocker
194 ScannerReport.Issue reportIssue = ScannerReport.Issue.newBuilder()
195 .setMsg("new message")
196 .setRuleRepository(ruleKey.repository())
197 .setRuleKey(ruleKey.rule())
198 .setSeverity(Constants.Severity.BLOCKER)
200 reportReader.putIssues(FILE_REF, singletonList(reportIssue));
202 underTest.visitAny(FILE);
204 List<DefaultIssue> issues = newArrayList(protoIssueCache.traverse());
205 assertThat(issues).hasSize(1);
206 assertThat(issues.get(0).severity()).isEqualTo(Severity.BLOCKER);
210 public void dont_cache_existing_issue_if_unmodified() {
211 RuleKey ruleKey = RuleTesting.XOO_X1;
212 // Issue from db has severity major
213 addBaseIssue(ruleKey);
215 // Issue from report has severity blocker
216 ScannerReport.Issue reportIssue = ScannerReport.Issue.newBuilder()
217 .setMsg("the message")
218 .setRuleRepository(ruleKey.repository())
219 .setRuleKey(ruleKey.rule())
220 .setSeverity(Constants.Severity.BLOCKER)
222 reportReader.putIssues(FILE_REF, singletonList(reportIssue));
224 underTest.visitAny(FILE);
226 List<DefaultIssue> issues = newArrayList(protoIssueCache.traverse());
227 assertThat(issues).hasSize(1);
228 assertThat(issues.get(0).severity()).isEqualTo(Severity.BLOCKER);
232 public void execute_issue_visitors() {
233 ruleRepositoryRule.add(RuleKey.of("xoo", "S001"));
234 ScannerReport.Issue reportIssue = ScannerReport.Issue.newBuilder()
235 .setMsg("the message")
236 .setRuleRepository("xoo")
238 .setSeverity(Constants.Severity.BLOCKER)
240 reportReader.putIssues(FILE_REF, singletonList(reportIssue));
242 underTest.visitAny(FILE);
244 verify(issueVisitor).beforeComponent(FILE);
245 verify(issueVisitor).afterComponent(FILE);
246 verify(issueVisitor).onIssue(eq(FILE), defaultIssueCaptor.capture());
247 assertThat(defaultIssueCaptor.getValue().ruleKey().rule()).isEqualTo("S001");
251 public void close_unmatched_base_issue() {
252 RuleKey ruleKey = RuleTesting.XOO_X1;
253 addBaseIssue(ruleKey);
255 // No issue in the report
256 underTest.visitAny(FILE);
258 List<DefaultIssue> issues = newArrayList(protoIssueCache.traverse());
259 assertThat(issues).isEmpty();
263 public void remove_uuid_of_original_file_from_componentsWithUnprocessedIssues_if_component_has_one() {
264 String originalFileUuid = "original file uuid";
265 when(movedFilesRepository.getOriginalFile(FILE))
266 .thenReturn(Optional.of(new MovedFilesRepository.OriginalFile(originalFileUuid, "original file key")));
268 underTest.visitAny(FILE);
272 public void reuse_issues_when_data_unchanged() {
273 RuleKey ruleKey = RuleTesting.XOO_X1;
274 // Issue from db has severity major
275 addBaseIssue(ruleKey);
277 // Issue from report has severity blocker
278 ScannerReport.Issue reportIssue = ScannerReport.Issue.newBuilder()
279 .setMsg("new message")
280 .setRuleRepository(ruleKey.repository())
281 .setRuleKey(ruleKey.rule())
282 .setSeverity(Constants.Severity.BLOCKER)
284 reportReader.putIssues(FILE_REF, singletonList(reportIssue));
285 when(fileStatuses.isDataUnchanged(FILE)).thenReturn(true);
287 underTest.visitAny(FILE);
289 // visitors get called, so measures created from issues should be calculated taking these issues into account
290 verify(issueVisitor).onIssue(eq(FILE), defaultIssueCaptor.capture());
291 assertThat(defaultIssueCaptor.getValue().ruleKey().rule()).isEqualTo(ruleKey.rule());
293 // most issues won't go to the cache since they aren't changed and don't need to be persisted
294 // In this test they are being closed but the workflows aren't working (we mock them) so nothing is changed on the issue is not cached.
295 assertThat(newArrayList(protoIssueCache.traverse())).isEmpty();
299 public void copy_issues_when_creating_new_non_main_branch() {
300 when(mergeBranchComponentsUuids.getComponentUuid(FILE_KEY)).thenReturn(FILE_UUID_ON_BRANCH);
301 when(referenceBranchComponentUuids.getReferenceBranchName()).thenReturn("master");
303 when(analysisMetadataHolder.isBranch()).thenReturn(true);
304 when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(true);
305 Branch branch = mock(Branch.class);
306 when(branch.isMain()).thenReturn(false);
307 when(branch.getType()).thenReturn(BranchType.BRANCH);
308 when(analysisMetadataHolder.getBranch()).thenReturn(branch);
310 RuleKey ruleKey = RuleTesting.XOO_X1;
311 // Issue from main branch has severity major
312 addBaseIssueOnBranch(ruleKey);
314 // Issue from report has severity blocker
315 ScannerReport.Issue reportIssue = ScannerReport.Issue.newBuilder()
316 .setMsg("the message")
317 .setRuleRepository(ruleKey.repository())
318 .setRuleKey(ruleKey.rule())
319 .setSeverity(Constants.Severity.BLOCKER)
321 reportReader.putIssues(FILE_REF, singletonList(reportIssue));
323 underTest.visitAny(FILE);
325 List<DefaultIssue> issues = newArrayList(protoIssueCache.traverse());
326 assertThat(issues).hasSize(1);
327 assertThat(issues.get(0).severity()).isEqualTo(Severity.BLOCKER);
328 assertThat(issues.get(0).isNew()).isFalse();
329 assertThat(issues.get(0).isCopied()).isTrue();
330 assertThat(issues.get(0).changes()).hasSize(1);
331 assertThat(issues.get(0).changes().get(0).diffs()).contains(entry(IssueFieldsSetter.FROM_BRANCH, new FieldDiffs.Diff<>("master", null)));
334 private void addBaseIssue(RuleKey ruleKey) {
335 ComponentDto project = ComponentTesting.newPrivateProjectDto(PROJECT_UUID).setKey(PROJECT_KEY);
336 ComponentDto file = ComponentTesting.newFileDto(project, null, FILE_UUID).setKey(FILE_KEY);
337 dbTester.components().insertComponents(project, file);
339 RuleDto ruleDto = RuleTesting.newDto(ruleKey);
340 dbTester.rules().insert(ruleDto);
341 ruleRepositoryRule.add(ruleKey);
343 IssueDto issue = IssueTesting.newDto(ruleDto, file, project)
345 .setStatus(Issue.STATUS_OPEN)
346 .setSeverity(Severity.MAJOR);
347 dbTester.getDbClient().issueDao().insert(dbTester.getSession(), issue);
348 dbTester.getSession().commit();
351 private void addBaseIssueOnBranch(RuleKey ruleKey) {
352 ComponentDto project = ComponentTesting.newPrivateProjectDto(PROJECT_UUID_ON_BRANCH).setKey(PROJECT_KEY);
353 ComponentDto file = ComponentTesting.newFileDto(project, null, FILE_UUID_ON_BRANCH).setKey(FILE_KEY);
354 dbTester.components().insertComponents(project, file);
356 RuleDto ruleDto = RuleTesting.newDto(ruleKey);
357 dbTester.rules().insert(ruleDto);
358 ruleRepositoryRule.add(ruleKey);
360 IssueDto issue = IssueTesting.newDto(ruleDto, file, project)
362 .setStatus(Issue.STATUS_OPEN)
363 .setSeverity(Severity.MAJOR);
364 dbTester.getDbClient().issueDao().insert(dbTester.getSession(), issue);
365 dbTester.getSession().commit();