3 * Copyright (C) 2009-2024 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 com.google.gson.Gson;
23 import java.util.Arrays;
24 import java.util.LinkedHashMap;
25 import java.util.List;
28 import javax.annotation.Nullable;
29 import org.assertj.core.data.MapEntry;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.RegisterExtension;
32 import org.sonar.api.issue.impact.Severity;
33 import org.sonar.api.issue.impact.SoftwareQuality;
34 import org.sonar.api.rules.RuleType;
35 import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
36 import org.sonar.ce.task.projectanalysis.component.Component;
37 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
38 import org.sonar.ce.task.projectanalysis.measure.Measure;
39 import org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry;
40 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
41 import org.sonar.ce.task.projectanalysis.measure.ValueType;
42 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
43 import org.sonar.core.issue.DefaultIssue;
44 import org.sonar.db.rule.RuleTesting;
45 import org.sonar.server.measure.ImpactMeasureBuilder;
47 import static java.util.Arrays.stream;
48 import static org.assertj.core.api.Assertions.assertThat;
49 import static org.assertj.core.api.Assertions.entry;
50 import static org.mockito.ArgumentMatchers.any;
51 import static org.mockito.ArgumentMatchers.eq;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.when;
54 import static org.sonar.api.issue.Issue.RESOLUTION_FALSE_POSITIVE;
55 import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
56 import static org.sonar.api.issue.Issue.RESOLUTION_WONT_FIX;
57 import static org.sonar.api.issue.Issue.STATUS_CLOSED;
58 import static org.sonar.api.issue.Issue.STATUS_CONFIRMED;
59 import static org.sonar.api.issue.Issue.STATUS_OPEN;
60 import static org.sonar.api.issue.Issue.STATUS_RESOLVED;
61 import static org.sonar.api.issue.impact.Severity.HIGH;
62 import static org.sonar.api.issue.impact.Severity.INFO;
63 import static org.sonar.api.issue.impact.Severity.LOW;
64 import static org.sonar.api.issue.impact.Severity.MEDIUM;
65 import static org.sonar.api.measures.CoreMetrics.ACCEPTED_ISSUES;
66 import static org.sonar.api.measures.CoreMetrics.ACCEPTED_ISSUES_KEY;
67 import static org.sonar.api.measures.CoreMetrics.BLOCKER_VIOLATIONS;
68 import static org.sonar.api.measures.CoreMetrics.BLOCKER_VIOLATIONS_KEY;
69 import static org.sonar.api.measures.CoreMetrics.BUGS;
70 import static org.sonar.api.measures.CoreMetrics.BUGS_KEY;
71 import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS;
72 import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS_KEY;
73 import static org.sonar.api.measures.CoreMetrics.CONFIRMED_ISSUES;
74 import static org.sonar.api.measures.CoreMetrics.CONFIRMED_ISSUES_KEY;
75 import static org.sonar.api.measures.CoreMetrics.CRITICAL_VIOLATIONS;
76 import static org.sonar.api.measures.CoreMetrics.CRITICAL_VIOLATIONS_KEY;
77 import static org.sonar.api.measures.CoreMetrics.FALSE_POSITIVE_ISSUES;
78 import static org.sonar.api.measures.CoreMetrics.FALSE_POSITIVE_ISSUES_KEY;
79 import static org.sonar.api.measures.CoreMetrics.HIGH_IMPACT_ACCEPTED_ISSUES;
80 import static org.sonar.api.measures.CoreMetrics.HIGH_IMPACT_ACCEPTED_ISSUES_KEY;
81 import static org.sonar.api.measures.CoreMetrics.INFO_VIOLATIONS;
82 import static org.sonar.api.measures.CoreMetrics.MAINTAINABILITY_ISSUES;
83 import static org.sonar.api.measures.CoreMetrics.MAJOR_VIOLATIONS;
84 import static org.sonar.api.measures.CoreMetrics.MAJOR_VIOLATIONS_KEY;
85 import static org.sonar.api.measures.CoreMetrics.MINOR_VIOLATIONS;
86 import static org.sonar.api.measures.CoreMetrics.NEW_ACCEPTED_ISSUES;
87 import static org.sonar.api.measures.CoreMetrics.NEW_ACCEPTED_ISSUES_KEY;
88 import static org.sonar.api.measures.CoreMetrics.NEW_BLOCKER_VIOLATIONS;
89 import static org.sonar.api.measures.CoreMetrics.NEW_BLOCKER_VIOLATIONS_KEY;
90 import static org.sonar.api.measures.CoreMetrics.NEW_BUGS;
91 import static org.sonar.api.measures.CoreMetrics.NEW_BUGS_KEY;
92 import static org.sonar.api.measures.CoreMetrics.NEW_CODE_SMELLS;
93 import static org.sonar.api.measures.CoreMetrics.NEW_CODE_SMELLS_KEY;
94 import static org.sonar.api.measures.CoreMetrics.NEW_CRITICAL_VIOLATIONS;
95 import static org.sonar.api.measures.CoreMetrics.NEW_CRITICAL_VIOLATIONS_KEY;
96 import static org.sonar.api.measures.CoreMetrics.NEW_INFO_VIOLATIONS;
97 import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_ISSUES;
98 import static org.sonar.api.measures.CoreMetrics.NEW_MAJOR_VIOLATIONS;
99 import static org.sonar.api.measures.CoreMetrics.NEW_MAJOR_VIOLATIONS_KEY;
100 import static org.sonar.api.measures.CoreMetrics.NEW_MINOR_VIOLATIONS;
101 import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_ISSUES;
102 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS;
103 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_KEY;
104 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_ISSUES;
105 import static org.sonar.api.measures.CoreMetrics.NEW_VIOLATIONS;
106 import static org.sonar.api.measures.CoreMetrics.NEW_VIOLATIONS_KEY;
107 import static org.sonar.api.measures.CoreMetrics.NEW_VULNERABILITIES;
108 import static org.sonar.api.measures.CoreMetrics.NEW_VULNERABILITIES_KEY;
109 import static org.sonar.api.measures.CoreMetrics.OPEN_ISSUES;
110 import static org.sonar.api.measures.CoreMetrics.OPEN_ISSUES_KEY;
111 import static org.sonar.api.measures.CoreMetrics.RELIABILITY_ISSUES;
112 import static org.sonar.api.measures.CoreMetrics.REOPENED_ISSUES;
113 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS;
114 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
115 import static org.sonar.api.measures.CoreMetrics.SECURITY_ISSUES;
116 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS;
117 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS_KEY;
118 import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES;
119 import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES_KEY;
120 import static org.sonar.api.rule.Severity.CRITICAL;
121 import static org.sonar.api.rule.Severity.MAJOR;
122 import static org.sonar.api.rules.RuleType.BUG;
123 import static org.sonar.api.rules.RuleType.CODE_SMELL;
124 import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
125 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
126 import static org.sonar.ce.task.projectanalysis.issue.IssueCounter.IMPACT_TO_METRIC_KEY;
127 import static org.sonar.ce.task.projectanalysis.issue.IssueCounter.IMPACT_TO_NEW_METRIC_KEY;
128 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
129 import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
130 import static org.sonar.test.JsonAssert.assertJson;
132 class IssueCounterTest {
134 private static final Component FILE1 = builder(Component.Type.FILE, 1).build();
135 private static final Component FILE2 = builder(Component.Type.FILE, 2).build();
136 private static final Component FILE3 = builder(Component.Type.FILE, 3).build();
137 private static final Component PROJECT = builder(Component.Type.PROJECT, 4).addChildren(FILE1, FILE2, FILE3).build();
140 private final BatchReportReaderRule reportReader = new BatchReportReaderRule();
143 private final TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
146 private final MetricRepositoryRule metricRepository = new MetricRepositoryRule()
149 .add(REOPENED_ISSUES)
150 .add(CONFIRMED_ISSUES)
151 .add(BLOCKER_VIOLATIONS)
152 .add(CRITICAL_VIOLATIONS)
153 .add(MAJOR_VIOLATIONS)
154 .add(MINOR_VIOLATIONS)
155 .add(INFO_VIOLATIONS)
157 .add(NEW_BLOCKER_VIOLATIONS)
158 .add(NEW_CRITICAL_VIOLATIONS)
159 .add(NEW_MAJOR_VIOLATIONS)
160 .add(NEW_MINOR_VIOLATIONS)
161 .add(NEW_INFO_VIOLATIONS)
162 .add(FALSE_POSITIVE_ISSUES)
163 .add(ACCEPTED_ISSUES)
166 .add(VULNERABILITIES)
167 .add(SECURITY_HOTSPOTS)
168 .add(NEW_CODE_SMELLS)
170 .add(NEW_VULNERABILITIES)
171 .add(NEW_SECURITY_HOTSPOTS)
172 .add(NEW_ACCEPTED_ISSUES)
173 .add(HIGH_IMPACT_ACCEPTED_ISSUES)
174 .add(RELIABILITY_ISSUES)
175 .add(MAINTAINABILITY_ISSUES)
176 .add(SECURITY_ISSUES)
177 .add(NEW_RELIABILITY_ISSUES)
178 .add(NEW_MAINTAINABILITY_ISSUES)
179 .add(NEW_SECURITY_ISSUES);
182 private final MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
183 private final NewIssueClassifier newIssueClassifier = mock(NewIssueClassifier.class);
184 private final IssueCounter underTest = new IssueCounter(metricRepository, measureRepository, newIssueClassifier);
185 private static int issueCounter;
188 void count_issues_by_status() {
189 // bottom-up traversal -> from files to project
190 underTest.beforeComponent(FILE1);
191 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, org.sonar.api.rule.Severity.BLOCKER));
192 underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR));
193 underTest.onIssue(FILE1, createIssue(RESOLUTION_FALSE_POSITIVE, STATUS_RESOLVED, MAJOR));
194 underTest.afterComponent(FILE1);
196 underTest.beforeComponent(FILE2);
197 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, org.sonar.api.rule.Severity.BLOCKER));
198 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, MAJOR));
199 underTest.afterComponent(FILE2);
201 underTest.beforeComponent(FILE3);
202 // Security hotspot should be ignored
203 underTest.onIssue(FILE3, createSecurityHotspot().setStatus(STATUS_OPEN));
204 underTest.afterComponent(FILE3);
206 underTest.beforeComponent(PROJECT);
207 underTest.afterComponent(PROJECT);
209 assertMeasures(FILE1, entry(VIOLATIONS_KEY, 1), entry(OPEN_ISSUES_KEY, 1), entry(CONFIRMED_ISSUES_KEY, 0));
210 assertMeasures(FILE2, entry(VIOLATIONS_KEY, 2), entry(OPEN_ISSUES_KEY, 0), entry(CONFIRMED_ISSUES_KEY, 2));
211 assertMeasures(FILE3, entry(VIOLATIONS_KEY, 0));
212 assertMeasures(PROJECT, entry(VIOLATIONS_KEY, 3), entry(OPEN_ISSUES_KEY, 1), entry(CONFIRMED_ISSUES_KEY, 2));
216 void count_issues_by_resolution() {
217 // bottom-up traversal -> from files to project
218 underTest.beforeComponent(FILE1);
219 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, org.sonar.api.rule.Severity.BLOCKER));
220 underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR));
221 underTest.onIssue(FILE1, createIssue(RESOLUTION_FALSE_POSITIVE, STATUS_RESOLVED, MAJOR));
222 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, MAJOR));
223 underTest.afterComponent(FILE1);
225 underTest.beforeComponent(FILE2);
226 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, org.sonar.api.rule.Severity.BLOCKER));
227 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, MAJOR));
228 underTest.onIssue(FILE2, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, MAJOR));
229 underTest.afterComponent(FILE2);
231 underTest.beforeComponent(FILE3);
232 // Security hotspot should be ignored
233 underTest.onIssue(FILE3, createSecurityHotspot().setResolution(RESOLUTION_WONT_FIX));
234 underTest.afterComponent(FILE3);
236 underTest.beforeComponent(PROJECT);
237 underTest.afterComponent(PROJECT);
239 assertMeasures(FILE1, entry(VIOLATIONS_KEY, 1), entry(FALSE_POSITIVE_ISSUES_KEY, 1), entry(ACCEPTED_ISSUES_KEY, 1));
240 assertMeasures(FILE2, entry(VIOLATIONS_KEY, 2), entry(FALSE_POSITIVE_ISSUES_KEY, 0), entry(ACCEPTED_ISSUES_KEY, 1));
241 assertMeasures(FILE3, entry(VIOLATIONS_KEY, 0));
242 assertMeasures(PROJECT, entry(VIOLATIONS_KEY, 3), entry(FALSE_POSITIVE_ISSUES_KEY, 1), entry(ACCEPTED_ISSUES_KEY, 2));
246 void count_unresolved_issues_by_severity() {
247 // bottom-up traversal -> from files to project
248 underTest.beforeComponent(FILE1);
249 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, org.sonar.api.rule.Severity.BLOCKER));
250 // this resolved issue is ignored
251 underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR));
252 underTest.afterComponent(FILE1);
254 underTest.beforeComponent(FILE2);
255 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, org.sonar.api.rule.Severity.BLOCKER));
256 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, MAJOR));
257 underTest.afterComponent(FILE2);
259 underTest.beforeComponent(PROJECT);
260 // Security hotspot should be ignored
261 underTest.onIssue(FILE3, createSecurityHotspot().setSeverity(MAJOR));
262 underTest.afterComponent(PROJECT);
264 assertMeasures(FILE1, entry(BLOCKER_VIOLATIONS_KEY, 1), entry(CRITICAL_VIOLATIONS_KEY, 0), entry(MAJOR_VIOLATIONS_KEY, 0));
265 assertMeasures(FILE2, entry(BLOCKER_VIOLATIONS_KEY, 1), entry(CRITICAL_VIOLATIONS_KEY, 0), entry(MAJOR_VIOLATIONS_KEY, 1));
266 assertMeasures(PROJECT, entry(BLOCKER_VIOLATIONS_KEY, 2), entry(CRITICAL_VIOLATIONS_KEY, 0), entry(MAJOR_VIOLATIONS_KEY, 1));
270 void count_unresolved_issues_by_type() {
271 // bottom-up traversal -> from files to project
272 // file1 : one open code smell, one closed code smell (which will be excluded from metric)
273 underTest.beforeComponent(FILE1);
274 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, org.sonar.api.rule.Severity.BLOCKER).setType(CODE_SMELL));
275 underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR).setType(CODE_SMELL));
276 underTest.afterComponent(FILE1);
279 underTest.beforeComponent(FILE2);
280 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, org.sonar.api.rule.Severity.BLOCKER).setType(BUG));
281 underTest.afterComponent(FILE2);
283 // file3 : one unresolved security hotspot
284 underTest.beforeComponent(FILE3);
285 underTest.onIssue(FILE3, createSecurityHotspot());
286 underTest.onIssue(FILE3, createSecurityHotspot().setResolution(RESOLUTION_WONT_FIX).setStatus(STATUS_CLOSED));
287 underTest.afterComponent(FILE3);
289 underTest.beforeComponent(PROJECT);
290 underTest.afterComponent(PROJECT);
292 assertMeasures(FILE1, entry(CODE_SMELLS_KEY, 1), entry(BUGS_KEY, 0), entry(VULNERABILITIES_KEY, 0), entry(SECURITY_HOTSPOTS_KEY, 0));
293 assertMeasures(FILE2, entry(CODE_SMELLS_KEY, 0), entry(BUGS_KEY, 1), entry(VULNERABILITIES_KEY, 0), entry(SECURITY_HOTSPOTS_KEY, 0));
294 assertMeasures(FILE3, entry(CODE_SMELLS_KEY, 0), entry(BUGS_KEY, 0), entry(VULNERABILITIES_KEY, 0), entry(SECURITY_HOTSPOTS_KEY, 1));
295 assertMeasures(PROJECT, entry(CODE_SMELLS_KEY, 1), entry(BUGS_KEY, 1), entry(VULNERABILITIES_KEY, 0), entry(SECURITY_HOTSPOTS_KEY, 1));
299 void count_new_issues() {
300 when(newIssueClassifier.isEnabled()).thenReturn(true);
302 underTest.beforeComponent(FILE1);
303 // created before -> existing issues (so ignored)
304 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, org.sonar.api.rule.Severity.BLOCKER).setType(CODE_SMELL));
305 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, org.sonar.api.rule.Severity.BLOCKER).setType(BUG));
307 // created after -> 4 new issues but 1 is closed
308 underTest.onIssue(FILE1, createNewIssue(null, STATUS_OPEN, CRITICAL).setType(CODE_SMELL));
309 underTest.onIssue(FILE1, createNewIssue(null, STATUS_OPEN, CRITICAL).setType(BUG));
310 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR).setType(BUG));
311 underTest.onIssue(FILE1, createNewSecurityHotspot());
312 underTest.onIssue(FILE1, createNewSecurityHotspot().setResolution(RESOLUTION_WONT_FIX).setStatus(STATUS_CLOSED));
313 underTest.afterComponent(FILE1);
315 underTest.beforeComponent(FILE2);
316 underTest.afterComponent(FILE2);
318 underTest.beforeComponent(PROJECT);
319 underTest.afterComponent(PROJECT);
321 assertIntValue(FILE1, entry(NEW_VIOLATIONS_KEY, 2), entry(NEW_CRITICAL_VIOLATIONS_KEY, 2), entry(NEW_BLOCKER_VIOLATIONS_KEY, 0),
322 entry(NEW_MAJOR_VIOLATIONS_KEY, 0),
323 entry(NEW_CODE_SMELLS_KEY, 1), entry(NEW_BUGS_KEY, 1), entry(NEW_VULNERABILITIES_KEY, 0), entry(NEW_SECURITY_HOTSPOTS_KEY, 1));
324 assertIntValue(PROJECT, entry(NEW_VIOLATIONS_KEY, 2), entry(NEW_CRITICAL_VIOLATIONS_KEY, 2), entry(NEW_BLOCKER_VIOLATIONS_KEY, 0),
325 entry(NEW_MAJOR_VIOLATIONS_KEY, 0),
326 entry(NEW_CODE_SMELLS_KEY, 1), entry(NEW_BUGS_KEY, 1), entry(NEW_VULNERABILITIES_KEY, 0), entry(NEW_SECURITY_HOTSPOTS_KEY, 1));
330 void count_new_accepted_issues() {
331 when(newIssueClassifier.isEnabled()).thenReturn(true);
333 underTest.beforeComponent(FILE1);
334 // created before -> existing issues (so ignored)
335 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, CRITICAL));
336 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, CRITICAL));
338 // created after -> 2 accepted, 1 open, 1 hotspot
339 underTest.onIssue(FILE1, createNewIssue(null, STATUS_OPEN, CRITICAL));
340 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, CRITICAL));
341 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, CRITICAL));
342 underTest.onIssue(FILE1, createNewSecurityHotspot());
343 underTest.afterComponent(FILE1);
345 underTest.beforeComponent(PROJECT);
346 underTest.afterComponent(PROJECT);
348 assertIntValue(FILE1, entry(NEW_VIOLATIONS_KEY, 1), entry(NEW_ACCEPTED_ISSUES_KEY, 2), entry(NEW_SECURITY_HOTSPOTS_KEY, 1));
349 assertIntValue(PROJECT, entry(NEW_VIOLATIONS_KEY, 1), entry(NEW_ACCEPTED_ISSUES_KEY, 2), entry(NEW_SECURITY_HOTSPOTS_KEY, 1));
353 void onIssue_shouldCountOverallSoftwareQualitiesMeasures() {
354 when(newIssueClassifier.isEnabled()).thenReturn(true);
356 underTest.beforeComponent(FILE1);
357 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, HIGH));
358 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, MEDIUM));
360 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, HIGH));
361 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, SoftwareQuality.MAINTAINABILITY, HIGH));
362 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, MEDIUM));
364 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, HIGH));
365 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, MEDIUM));
367 underTest.onIssue(FILE1, createNewSecurityHotspot());
368 underTest.afterComponent(FILE1);
370 underTest.beforeComponent(PROJECT);
371 underTest.afterComponent(PROJECT);
373 Set<Map.Entry<String, Measure>> entries = measureRepository.getRawMeasures(FILE1).entrySet();
375 assertOverallSoftwareQualityMeasures(SoftwareQuality.MAINTAINABILITY, getImpactMeasure(4, 2, 2, 0, 0, 0), entries);
376 assertOverallSoftwareQualityMeasures(SoftwareQuality.SECURITY, getImpactMeasure(2, 1, 1, 0, 0, 0), entries);
377 assertOverallSoftwareQualityMeasures(SoftwareQuality.RELIABILITY, getImpactMeasure(0, 0, 0, 0, 0, 0), entries);
381 void onIssue_shouldCountNewSoftwareQualitiesMeasures() {
382 when(newIssueClassifier.isEnabled()).thenReturn(true);
384 underTest.beforeComponent(FILE1);
385 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, HIGH));
386 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, HIGH));
387 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, SoftwareQuality.MAINTAINABILITY, HIGH));
388 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, MEDIUM));
390 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.RELIABILITY, HIGH));
391 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.RELIABILITY, LOW));
392 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, SoftwareQuality.RELIABILITY, HIGH));
393 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.RELIABILITY, MEDIUM));
395 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, MEDIUM));
396 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, LOW));
397 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, HIGH));
398 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, HIGH));
399 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, SoftwareQuality.SECURITY, HIGH));
400 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, MEDIUM));
402 underTest.afterComponent(FILE1);
404 underTest.beforeComponent(PROJECT);
405 underTest.afterComponent(PROJECT);
407 Set<Map.Entry<String, Measure>> entries = measureRepository.getRawMeasures(FILE1).entrySet();
409 assertNewSoftwareQualityMeasures(SoftwareQuality.MAINTAINABILITY, getImpactMeasure(2, 1, 1, 0, 0, 0), entries);
410 assertNewSoftwareQualityMeasures(SoftwareQuality.RELIABILITY, getImpactMeasure(2, 0, 1, 1, 0, 0), entries);
411 assertNewSoftwareQualityMeasures(SoftwareQuality.SECURITY, getImpactMeasure(4, 2, 1, 1, 0, 0), entries);
414 private static Map<String, Long> getImpactMeasure(long total, long high, long medium, long low) {
415 Map<String, Long> map = new LinkedHashMap<>();
416 map.put(LOW.name(), low);
417 map.put(MEDIUM.name(), medium);
418 map.put(HIGH.name(), high);
419 map.put(ImpactMeasureBuilder.TOTAL_KEY, total);
423 private static Map<String, Long> getImpactMeasure(long total, long high, long medium, long low, long info, long blocker) {
424 Map<String, Long> map = getImpactMeasure(total, high, medium, low);
425 map.put(INFO.name(), info);
426 map.put(Severity.BLOCKER.name(), blocker);
430 private void assertOverallSoftwareQualityMeasures(SoftwareQuality softwareQuality, Map<? extends String, Long> expectedMap,
431 Set<Map.Entry<String, Measure>> actualRaw) {
432 assertSoftwareQualityMeasures(softwareQuality, expectedMap, actualRaw, IMPACT_TO_METRIC_KEY);
435 private void assertNewSoftwareQualityMeasures(SoftwareQuality softwareQuality, Map<? extends String, Long> expectedMap,
436 Set<Map.Entry<String, Measure>> actualRaw) {
437 assertSoftwareQualityMeasures(softwareQuality, expectedMap, actualRaw, IMPACT_TO_NEW_METRIC_KEY);
440 private void assertSoftwareQualityMeasures(SoftwareQuality softwareQuality, Map<? extends String, Long> expectedMap,
441 Set<Map.Entry<String, Measure>> actualRaw, Map<String, String> impactToMetricMap) {
443 Map.Entry<String, Measure> softwareQualityMap = actualRaw.stream()
444 .filter(e -> e.getKey().equals(impactToMetricMap.get(softwareQuality.name())))
448 assertJson(softwareQualityMap.getValue().getData()).isSimilarTo(new Gson().toJson(expectedMap));
452 void count_high_impact_accepted_issues() {
453 when(newIssueClassifier.isEnabled()).thenReturn(true);
455 underTest.beforeComponent(FILE1);
456 // created before -> existing issues with 2 high impact accepted (High and Blocker)
457 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, HIGH));
458 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, HIGH));
459 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, Severity.BLOCKER));
460 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, MEDIUM));
462 // created after -> 2 high impact accepted
463 underTest.onIssue(FILE1, createNewIssue(null, STATUS_OPEN, HIGH));
464 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, HIGH));
465 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, HIGH));
466 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, MEDIUM));
467 underTest.onIssue(FILE1, createNewSecurityHotspot());
468 underTest.afterComponent(FILE1);
470 underTest.beforeComponent(PROJECT);
471 underTest.afterComponent(PROJECT);
473 assertIntValue(FILE1, entry(VIOLATIONS_KEY, 2), entry(NEW_VIOLATIONS_KEY, 1), entry(NEW_ACCEPTED_ISSUES_KEY, 3),
474 entry(HIGH_IMPACT_ACCEPTED_ISSUES_KEY, 4));
475 assertIntValue(PROJECT, entry(VIOLATIONS_KEY, 2), entry(NEW_VIOLATIONS_KEY, 1), entry(NEW_ACCEPTED_ISSUES_KEY, 3),
476 entry(HIGH_IMPACT_ACCEPTED_ISSUES_KEY, 4));
480 void exclude_hotspots_from_issue_counts() {
481 // bottom-up traversal -> from files to project
482 underTest.beforeComponent(FILE1);
483 underTest.onIssue(FILE1, createSecurityHotspot());
484 underTest.onIssue(FILE1, createSecurityHotspot());
485 underTest.afterComponent(FILE1);
487 underTest.beforeComponent(FILE2);
488 underTest.onIssue(FILE2, createSecurityHotspot());
489 underTest.afterComponent(FILE2);
491 underTest.beforeComponent(FILE3);
492 underTest.afterComponent(FILE3);
494 underTest.beforeComponent(PROJECT);
495 underTest.afterComponent(PROJECT);
497 assertMeasures(FILE1, entry(VIOLATIONS_KEY, 0), entry(OPEN_ISSUES_KEY, 0), entry(CONFIRMED_ISSUES_KEY, 0));
498 assertMeasures(FILE2, entry(VIOLATIONS_KEY, 0), entry(OPEN_ISSUES_KEY, 0), entry(CONFIRMED_ISSUES_KEY, 0));
499 assertMeasures(FILE3, entry(VIOLATIONS_KEY, 0));
500 assertMeasures(PROJECT, entry(VIOLATIONS_KEY, 0), entry(OPEN_ISSUES_KEY, 0), entry(CONFIRMED_ISSUES_KEY, 0));
504 void exclude_new_hotspots_from_issue_counts() {
505 when(newIssueClassifier.isEnabled()).thenReturn(true);
507 underTest.beforeComponent(FILE1);
508 // created before -> existing issues (so ignored)
509 underTest.onIssue(FILE1, createSecurityHotspot());
510 underTest.onIssue(FILE1, createSecurityHotspot());
512 // created after, but closed
513 underTest.onIssue(FILE1, createNewSecurityHotspot().setStatus(STATUS_RESOLVED).setResolution(RESOLUTION_WONT_FIX));
515 for (String severity : Arrays.asList(CRITICAL, org.sonar.api.rule.Severity.BLOCKER, MAJOR)) {
516 DefaultIssue issue = createNewSecurityHotspot();
517 issue.setSeverity(severity);
518 underTest.onIssue(FILE1, issue);
520 underTest.afterComponent(FILE1);
522 underTest.beforeComponent(FILE2);
523 underTest.afterComponent(FILE2);
525 underTest.beforeComponent(PROJECT);
526 underTest.afterComponent(PROJECT);
528 assertIntValue(FILE1, entry(NEW_VIOLATIONS_KEY, 0), entry(NEW_CRITICAL_VIOLATIONS_KEY, 0), entry(NEW_BLOCKER_VIOLATIONS_KEY, 0),
529 entry(NEW_MAJOR_VIOLATIONS_KEY, 0),
530 entry(NEW_VULNERABILITIES_KEY, 0));
531 assertIntValue(PROJECT, entry(NEW_VIOLATIONS_KEY, 0), entry(NEW_CRITICAL_VIOLATIONS_KEY, 0), entry(NEW_BLOCKER_VIOLATIONS_KEY, 0),
532 entry(NEW_MAJOR_VIOLATIONS_KEY, 0),
533 entry(NEW_VULNERABILITIES_KEY, 0));
537 private void assertIntValue(Component componentRef, MapEntry<String, Integer>... entries) {
538 assertThat(measureRepository.getRawMeasures(componentRef).entrySet()
540 .filter(e -> e.getValue().getValueType() == ValueType.INT)
541 .map(e -> entry(e.getKey(), e.getValue().getIntValue())))
546 private void assertMeasures(Component componentRef, Map.Entry<String, Integer>... entries) {
547 List<MeasureRepoEntry> expected = stream(entries)
548 .map(e -> entryOf(e.getKey(), newMeasureBuilder().create(e.getValue())))
551 assertThat(measureRepository.getRawMeasures(componentRef).entrySet().stream().map(e -> entryOf(e.getKey(), e.getValue())))
552 .containsAll(expected);
555 private DefaultIssue createNewIssue(@Nullable String resolution, String status, String severity) {
556 return createNewIssue(resolution, status, severity, CODE_SMELL);
559 private DefaultIssue createNewIssue(@Nullable String resolution, String status, Severity impactSeverity) {
560 return createNewIssue(resolution, status, SoftwareQuality.MAINTAINABILITY, impactSeverity);
563 private DefaultIssue createNewIssue(@Nullable String resolution, String status, SoftwareQuality softwareQuality,
564 Severity impactSeverity) {
565 DefaultIssue issue = createNewIssue(resolution, status, MAJOR, CODE_SMELL);
566 issue.addImpact(softwareQuality, impactSeverity);
570 private DefaultIssue createNewIssue(@Nullable String resolution, String status, String severity, RuleType ruleType) {
571 DefaultIssue issue = createIssue(resolution, status, severity, ruleType);
572 when(newIssueClassifier.isNew(any(), eq(issue))).thenReturn(true);
576 private static DefaultIssue createIssue(@Nullable String resolution, String status, String severity) {
577 return createIssue(resolution, status, severity, CODE_SMELL);
580 private static DefaultIssue createIssue(@Nullable String resolution, String status, Severity impactSeverity) {
581 return createIssue(resolution, status, SoftwareQuality.MAINTAINABILITY, impactSeverity);
584 private static DefaultIssue createIssue(@Nullable String resolution, String status, SoftwareQuality softwareQuality,
585 Severity impactSeverity) {
586 DefaultIssue issue = createIssue(resolution, status, MAJOR, CODE_SMELL);
587 issue.addImpact(softwareQuality, impactSeverity);
591 private static DefaultIssue createIssue(@Nullable String resolution, String status, String severity, RuleType ruleType) {
592 return new DefaultIssue()
593 .setKey(String.valueOf(++issueCounter))
594 .setResolution(resolution).setStatus(status)
595 .setSeverity(severity).setRuleKey(RuleTesting.XOO_X1)
599 private static DefaultIssue createSecurityHotspot() {
600 return createIssue(null, STATUS_OPEN, "MAJOR", SECURITY_HOTSPOT);
603 private DefaultIssue createNewSecurityHotspot() {
604 return createNewIssue(null, STATUS_OPEN, "MAJOR", SECURITY_HOTSPOT);