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.metric.MetricRepositoryRule;
42 import org.sonar.core.issue.DefaultIssue;
43 import org.sonar.db.rule.RuleTesting;
44 import org.sonar.server.measure.ImpactMeasureBuilder;
46 import static java.util.Arrays.stream;
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.assertj.core.api.Assertions.entry;
49 import static org.mockito.ArgumentMatchers.any;
50 import static org.mockito.ArgumentMatchers.eq;
51 import static org.mockito.Mockito.mock;
52 import static org.mockito.Mockito.when;
53 import static org.sonar.api.issue.Issue.RESOLUTION_FALSE_POSITIVE;
54 import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
55 import static org.sonar.api.issue.Issue.RESOLUTION_WONT_FIX;
56 import static org.sonar.api.issue.Issue.STATUS_CLOSED;
57 import static org.sonar.api.issue.Issue.STATUS_CONFIRMED;
58 import static org.sonar.api.issue.Issue.STATUS_OPEN;
59 import static org.sonar.api.issue.Issue.STATUS_RESOLVED;
60 import static org.sonar.api.issue.impact.Severity.HIGH;
61 import static org.sonar.api.issue.impact.Severity.LOW;
62 import static org.sonar.api.issue.impact.Severity.MEDIUM;
63 import static org.sonar.api.measures.CoreMetrics.ACCEPTED_ISSUES;
64 import static org.sonar.api.measures.CoreMetrics.ACCEPTED_ISSUES_KEY;
65 import static org.sonar.api.measures.CoreMetrics.BLOCKER_VIOLATIONS;
66 import static org.sonar.api.measures.CoreMetrics.BLOCKER_VIOLATIONS_KEY;
67 import static org.sonar.api.measures.CoreMetrics.BUGS;
68 import static org.sonar.api.measures.CoreMetrics.BUGS_KEY;
69 import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS;
70 import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS_KEY;
71 import static org.sonar.api.measures.CoreMetrics.CONFIRMED_ISSUES;
72 import static org.sonar.api.measures.CoreMetrics.CONFIRMED_ISSUES_KEY;
73 import static org.sonar.api.measures.CoreMetrics.CRITICAL_VIOLATIONS;
74 import static org.sonar.api.measures.CoreMetrics.CRITICAL_VIOLATIONS_KEY;
75 import static org.sonar.api.measures.CoreMetrics.FALSE_POSITIVE_ISSUES;
76 import static org.sonar.api.measures.CoreMetrics.FALSE_POSITIVE_ISSUES_KEY;
77 import static org.sonar.api.measures.CoreMetrics.HIGH_IMPACT_ACCEPTED_ISSUES;
78 import static org.sonar.api.measures.CoreMetrics.HIGH_IMPACT_ACCEPTED_ISSUES_KEY;
79 import static org.sonar.api.measures.CoreMetrics.INFO_VIOLATIONS;
80 import static org.sonar.api.measures.CoreMetrics.MAINTAINABILITY_ISSUES;
81 import static org.sonar.api.measures.CoreMetrics.MAJOR_VIOLATIONS;
82 import static org.sonar.api.measures.CoreMetrics.MAJOR_VIOLATIONS_KEY;
83 import static org.sonar.api.measures.CoreMetrics.MINOR_VIOLATIONS;
84 import static org.sonar.api.measures.CoreMetrics.NEW_ACCEPTED_ISSUES;
85 import static org.sonar.api.measures.CoreMetrics.NEW_ACCEPTED_ISSUES_KEY;
86 import static org.sonar.api.measures.CoreMetrics.NEW_BLOCKER_VIOLATIONS;
87 import static org.sonar.api.measures.CoreMetrics.NEW_BLOCKER_VIOLATIONS_KEY;
88 import static org.sonar.api.measures.CoreMetrics.NEW_BUGS;
89 import static org.sonar.api.measures.CoreMetrics.NEW_BUGS_KEY;
90 import static org.sonar.api.measures.CoreMetrics.NEW_CODE_SMELLS;
91 import static org.sonar.api.measures.CoreMetrics.NEW_CODE_SMELLS_KEY;
92 import static org.sonar.api.measures.CoreMetrics.NEW_CRITICAL_VIOLATIONS;
93 import static org.sonar.api.measures.CoreMetrics.NEW_CRITICAL_VIOLATIONS_KEY;
94 import static org.sonar.api.measures.CoreMetrics.NEW_INFO_VIOLATIONS;
95 import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_ISSUES;
96 import static org.sonar.api.measures.CoreMetrics.NEW_MAJOR_VIOLATIONS;
97 import static org.sonar.api.measures.CoreMetrics.NEW_MAJOR_VIOLATIONS_KEY;
98 import static org.sonar.api.measures.CoreMetrics.NEW_MINOR_VIOLATIONS;
99 import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_ISSUES;
100 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS;
101 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_KEY;
102 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_ISSUES;
103 import static org.sonar.api.measures.CoreMetrics.NEW_VIOLATIONS;
104 import static org.sonar.api.measures.CoreMetrics.NEW_VIOLATIONS_KEY;
105 import static org.sonar.api.measures.CoreMetrics.NEW_VULNERABILITIES;
106 import static org.sonar.api.measures.CoreMetrics.NEW_VULNERABILITIES_KEY;
107 import static org.sonar.api.measures.CoreMetrics.OPEN_ISSUES;
108 import static org.sonar.api.measures.CoreMetrics.OPEN_ISSUES_KEY;
109 import static org.sonar.api.measures.CoreMetrics.RELIABILITY_ISSUES;
110 import static org.sonar.api.measures.CoreMetrics.REOPENED_ISSUES;
111 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS;
112 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
113 import static org.sonar.api.measures.CoreMetrics.SECURITY_ISSUES;
114 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS;
115 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS_KEY;
116 import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES;
117 import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES_KEY;
118 import static org.sonar.api.rule.Severity.BLOCKER;
119 import static org.sonar.api.rule.Severity.CRITICAL;
120 import static org.sonar.api.rule.Severity.MAJOR;
121 import static org.sonar.api.rules.RuleType.BUG;
122 import static org.sonar.api.rules.RuleType.CODE_SMELL;
123 import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
124 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
125 import static org.sonar.ce.task.projectanalysis.issue.IssueCounter.IMPACT_TO_METRIC_KEY;
126 import static org.sonar.ce.task.projectanalysis.issue.IssueCounter.IMPACT_TO_NEW_METRIC_KEY;
127 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
128 import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
129 import static org.sonar.test.JsonAssert.assertJson;
131 class IssueCounterTest {
133 private static final Component FILE1 = builder(Component.Type.FILE, 1).build();
134 private static final Component FILE2 = builder(Component.Type.FILE, 2).build();
135 private static final Component FILE3 = builder(Component.Type.FILE, 3).build();
136 private static final Component PROJECT = builder(Component.Type.PROJECT, 4).addChildren(FILE1, FILE2, FILE3).build();
139 private final BatchReportReaderRule reportReader = new BatchReportReaderRule();
142 private final TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
145 private final MetricRepositoryRule metricRepository = new MetricRepositoryRule()
148 .add(REOPENED_ISSUES)
149 .add(CONFIRMED_ISSUES)
150 .add(BLOCKER_VIOLATIONS)
151 .add(CRITICAL_VIOLATIONS)
152 .add(MAJOR_VIOLATIONS)
153 .add(MINOR_VIOLATIONS)
154 .add(INFO_VIOLATIONS)
156 .add(NEW_BLOCKER_VIOLATIONS)
157 .add(NEW_CRITICAL_VIOLATIONS)
158 .add(NEW_MAJOR_VIOLATIONS)
159 .add(NEW_MINOR_VIOLATIONS)
160 .add(NEW_INFO_VIOLATIONS)
161 .add(FALSE_POSITIVE_ISSUES)
162 .add(ACCEPTED_ISSUES)
165 .add(VULNERABILITIES)
166 .add(SECURITY_HOTSPOTS)
167 .add(NEW_CODE_SMELLS)
169 .add(NEW_VULNERABILITIES)
170 .add(NEW_SECURITY_HOTSPOTS)
171 .add(NEW_ACCEPTED_ISSUES)
172 .add(HIGH_IMPACT_ACCEPTED_ISSUES)
173 .add(RELIABILITY_ISSUES)
174 .add(MAINTAINABILITY_ISSUES)
175 .add(SECURITY_ISSUES)
176 .add(NEW_RELIABILITY_ISSUES)
177 .add(NEW_MAINTAINABILITY_ISSUES)
178 .add(NEW_SECURITY_ISSUES);
181 private final MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
182 private final NewIssueClassifier newIssueClassifier = mock(NewIssueClassifier.class);
183 private final IssueCounter underTest = new IssueCounter(metricRepository, measureRepository, newIssueClassifier);
184 private static int issueCounter;
187 void count_issues_by_status() {
188 // bottom-up traversal -> from files to project
189 underTest.beforeComponent(FILE1);
190 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER));
191 underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR));
192 underTest.onIssue(FILE1, createIssue(RESOLUTION_FALSE_POSITIVE, STATUS_RESOLVED, MAJOR));
193 underTest.afterComponent(FILE1);
195 underTest.beforeComponent(FILE2);
196 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, BLOCKER));
197 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, MAJOR));
198 underTest.afterComponent(FILE2);
200 underTest.beforeComponent(FILE3);
201 // Security hotspot should be ignored
202 underTest.onIssue(FILE3, createSecurityHotspot().setStatus(STATUS_OPEN));
203 underTest.afterComponent(FILE3);
205 underTest.beforeComponent(PROJECT);
206 underTest.afterComponent(PROJECT);
208 assertMeasures(FILE1, entry(VIOLATIONS_KEY, 1), entry(OPEN_ISSUES_KEY, 1), entry(CONFIRMED_ISSUES_KEY, 0));
209 assertMeasures(FILE2, entry(VIOLATIONS_KEY, 2), entry(OPEN_ISSUES_KEY, 0), entry(CONFIRMED_ISSUES_KEY, 2));
210 assertMeasures(FILE3, entry(VIOLATIONS_KEY, 0));
211 assertMeasures(PROJECT, entry(VIOLATIONS_KEY, 3), entry(OPEN_ISSUES_KEY, 1), entry(CONFIRMED_ISSUES_KEY, 2));
215 void count_issues_by_resolution() {
216 // bottom-up traversal -> from files to project
217 underTest.beforeComponent(FILE1);
218 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER));
219 underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR));
220 underTest.onIssue(FILE1, createIssue(RESOLUTION_FALSE_POSITIVE, STATUS_RESOLVED, MAJOR));
221 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, MAJOR));
222 underTest.afterComponent(FILE1);
224 underTest.beforeComponent(FILE2);
225 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, BLOCKER));
226 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, MAJOR));
227 underTest.onIssue(FILE2, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, MAJOR));
228 underTest.afterComponent(FILE2);
230 underTest.beforeComponent(FILE3);
231 // Security hotspot should be ignored
232 underTest.onIssue(FILE3, createSecurityHotspot().setResolution(RESOLUTION_WONT_FIX));
233 underTest.afterComponent(FILE3);
235 underTest.beforeComponent(PROJECT);
236 underTest.afterComponent(PROJECT);
238 assertMeasures(FILE1, entry(VIOLATIONS_KEY, 1), entry(FALSE_POSITIVE_ISSUES_KEY, 1), entry(ACCEPTED_ISSUES_KEY, 1));
239 assertMeasures(FILE2, entry(VIOLATIONS_KEY, 2), entry(FALSE_POSITIVE_ISSUES_KEY, 0), entry(ACCEPTED_ISSUES_KEY, 1));
240 assertMeasures(FILE3, entry(VIOLATIONS_KEY, 0));
241 assertMeasures(PROJECT, entry(VIOLATIONS_KEY, 3), entry(FALSE_POSITIVE_ISSUES_KEY, 1), entry(ACCEPTED_ISSUES_KEY, 2));
245 void count_unresolved_issues_by_severity() {
246 // bottom-up traversal -> from files to project
247 underTest.beforeComponent(FILE1);
248 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER));
249 // this resolved issue is ignored
250 underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR));
251 underTest.afterComponent(FILE1);
253 underTest.beforeComponent(FILE2);
254 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, BLOCKER));
255 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, MAJOR));
256 underTest.afterComponent(FILE2);
258 underTest.beforeComponent(PROJECT);
259 // Security hotspot should be ignored
260 underTest.onIssue(FILE3, createSecurityHotspot().setSeverity(MAJOR));
261 underTest.afterComponent(PROJECT);
263 assertMeasures(FILE1, entry(BLOCKER_VIOLATIONS_KEY, 1), entry(CRITICAL_VIOLATIONS_KEY, 0), entry(MAJOR_VIOLATIONS_KEY, 0));
264 assertMeasures(FILE2, entry(BLOCKER_VIOLATIONS_KEY, 1), entry(CRITICAL_VIOLATIONS_KEY, 0), entry(MAJOR_VIOLATIONS_KEY, 1));
265 assertMeasures(PROJECT, entry(BLOCKER_VIOLATIONS_KEY, 2), entry(CRITICAL_VIOLATIONS_KEY, 0), entry(MAJOR_VIOLATIONS_KEY, 1));
269 void count_unresolved_issues_by_type() {
270 // bottom-up traversal -> from files to project
271 // file1 : one open code smell, one closed code smell (which will be excluded from metric)
272 underTest.beforeComponent(FILE1);
273 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER).setType(CODE_SMELL));
274 underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR).setType(CODE_SMELL));
275 underTest.afterComponent(FILE1);
278 underTest.beforeComponent(FILE2);
279 underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, BLOCKER).setType(BUG));
280 underTest.afterComponent(FILE2);
282 // file3 : one unresolved security hotspot
283 underTest.beforeComponent(FILE3);
284 underTest.onIssue(FILE3, createSecurityHotspot());
285 underTest.onIssue(FILE3, createSecurityHotspot().setResolution(RESOLUTION_WONT_FIX).setStatus(STATUS_CLOSED));
286 underTest.afterComponent(FILE3);
288 underTest.beforeComponent(PROJECT);
289 underTest.afterComponent(PROJECT);
291 assertMeasures(FILE1, entry(CODE_SMELLS_KEY, 1), entry(BUGS_KEY, 0), entry(VULNERABILITIES_KEY, 0), entry(SECURITY_HOTSPOTS_KEY, 0));
292 assertMeasures(FILE2, entry(CODE_SMELLS_KEY, 0), entry(BUGS_KEY, 1), entry(VULNERABILITIES_KEY, 0), entry(SECURITY_HOTSPOTS_KEY, 0));
293 assertMeasures(FILE3, entry(CODE_SMELLS_KEY, 0), entry(BUGS_KEY, 0), entry(VULNERABILITIES_KEY, 0), entry(SECURITY_HOTSPOTS_KEY, 1));
294 assertMeasures(PROJECT, entry(CODE_SMELLS_KEY, 1), entry(BUGS_KEY, 1), entry(VULNERABILITIES_KEY, 0), entry(SECURITY_HOTSPOTS_KEY, 1));
298 void count_new_issues() {
299 when(newIssueClassifier.isEnabled()).thenReturn(true);
301 underTest.beforeComponent(FILE1);
302 // created before -> existing issues (so ignored)
303 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER).setType(CODE_SMELL));
304 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER).setType(BUG));
306 // created after -> 4 new issues but 1 is closed
307 underTest.onIssue(FILE1, createNewIssue(null, STATUS_OPEN, CRITICAL).setType(CODE_SMELL));
308 underTest.onIssue(FILE1, createNewIssue(null, STATUS_OPEN, CRITICAL).setType(BUG));
309 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR).setType(BUG));
310 underTest.onIssue(FILE1, createNewSecurityHotspot());
311 underTest.onIssue(FILE1, createNewSecurityHotspot().setResolution(RESOLUTION_WONT_FIX).setStatus(STATUS_CLOSED));
312 underTest.afterComponent(FILE1);
314 underTest.beforeComponent(FILE2);
315 underTest.afterComponent(FILE2);
317 underTest.beforeComponent(PROJECT);
318 underTest.afterComponent(PROJECT);
320 assertIntValue(FILE1, entry(NEW_VIOLATIONS_KEY, 2), entry(NEW_CRITICAL_VIOLATIONS_KEY, 2), entry(NEW_BLOCKER_VIOLATIONS_KEY, 0),
321 entry(NEW_MAJOR_VIOLATIONS_KEY, 0),
322 entry(NEW_CODE_SMELLS_KEY, 1), entry(NEW_BUGS_KEY, 1), entry(NEW_VULNERABILITIES_KEY, 0), entry(NEW_SECURITY_HOTSPOTS_KEY, 1));
323 assertIntValue(PROJECT, entry(NEW_VIOLATIONS_KEY, 2), entry(NEW_CRITICAL_VIOLATIONS_KEY, 2), entry(NEW_BLOCKER_VIOLATIONS_KEY, 0),
324 entry(NEW_MAJOR_VIOLATIONS_KEY, 0),
325 entry(NEW_CODE_SMELLS_KEY, 1), entry(NEW_BUGS_KEY, 1), entry(NEW_VULNERABILITIES_KEY, 0), entry(NEW_SECURITY_HOTSPOTS_KEY, 1));
329 void count_new_accepted_issues() {
330 when(newIssueClassifier.isEnabled()).thenReturn(true);
332 underTest.beforeComponent(FILE1);
333 // created before -> existing issues (so ignored)
334 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, CRITICAL));
335 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, CRITICAL));
337 // created after -> 2 accepted, 1 open, 1 hotspot
338 underTest.onIssue(FILE1, createNewIssue(null, STATUS_OPEN, CRITICAL));
339 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, CRITICAL));
340 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, CRITICAL));
341 underTest.onIssue(FILE1, createNewSecurityHotspot());
342 underTest.afterComponent(FILE1);
344 underTest.beforeComponent(PROJECT);
345 underTest.afterComponent(PROJECT);
347 assertIntValue(FILE1, entry(NEW_VIOLATIONS_KEY, 1), entry(NEW_ACCEPTED_ISSUES_KEY, 2), entry(NEW_SECURITY_HOTSPOTS_KEY, 1));
348 assertIntValue(PROJECT, entry(NEW_VIOLATIONS_KEY, 1), entry(NEW_ACCEPTED_ISSUES_KEY, 2), entry(NEW_SECURITY_HOTSPOTS_KEY, 1));
352 void onIssue_shouldCountOverallSoftwareQualitiesMeasures() {
353 when(newIssueClassifier.isEnabled()).thenReturn(true);
355 underTest.beforeComponent(FILE1);
356 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, HIGH));
357 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, MEDIUM));
359 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, HIGH));
360 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, SoftwareQuality.MAINTAINABILITY, HIGH));
361 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, MEDIUM));
363 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, HIGH));
364 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, MEDIUM));
366 underTest.onIssue(FILE1, createNewSecurityHotspot());
367 underTest.afterComponent(FILE1);
369 underTest.beforeComponent(PROJECT);
370 underTest.afterComponent(PROJECT);
372 Set<Map.Entry<String, Measure>> entries = measureRepository.getRawMeasures(FILE1).entrySet();
374 assertOverallSoftwareQualityMeasures(SoftwareQuality.MAINTAINABILITY, getImpactMeasure(4, 2, 2, 0, 0, 0), entries);
375 assertOverallSoftwareQualityMeasures(SoftwareQuality.SECURITY, getImpactMeasure(2, 1, 1, 0, 0, 0), entries);
376 assertOverallSoftwareQualityMeasures(SoftwareQuality.RELIABILITY, getImpactMeasure(0, 0, 0, 0, 0, 0), entries);
380 void onIssue_shouldCountNewSoftwareQualitiesMeasures() {
381 when(newIssueClassifier.isEnabled()).thenReturn(true);
383 underTest.beforeComponent(FILE1);
384 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, HIGH));
385 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, HIGH));
386 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, SoftwareQuality.MAINTAINABILITY, HIGH));
387 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, MEDIUM));
389 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.RELIABILITY, HIGH));
390 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.RELIABILITY, LOW));
391 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, SoftwareQuality.RELIABILITY, HIGH));
392 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.RELIABILITY, MEDIUM));
394 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, MEDIUM));
395 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, LOW));
396 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, HIGH));
397 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, HIGH));
398 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, SoftwareQuality.SECURITY, HIGH));
399 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, MEDIUM));
401 underTest.afterComponent(FILE1);
403 underTest.beforeComponent(PROJECT);
404 underTest.afterComponent(PROJECT);
406 Set<Map.Entry<String, Measure>> entries = measureRepository.getRawMeasures(FILE1).entrySet();
408 assertNewSoftwareQualityMeasures(SoftwareQuality.MAINTAINABILITY, getImpactMeasure(2, 1, 1, 0, 0, 0), entries);
409 assertNewSoftwareQualityMeasures(SoftwareQuality.RELIABILITY, getImpactMeasure(2, 0, 1, 1, 0, 0), entries);
410 assertNewSoftwareQualityMeasures(SoftwareQuality.SECURITY, getImpactMeasure(4, 2, 1, 1, 0, 0), entries);
413 private static Map<String, Long> getImpactMeasure(long total, long high, long medium, long low) {
414 Map<String, Long> map = new LinkedHashMap<>();
415 map.put(LOW.name(), low);
416 map.put(MEDIUM.name(), medium);
417 map.put(HIGH.name(), high);
418 map.put(ImpactMeasureBuilder.TOTAL_KEY, total);
422 private static Map<String, Long> getImpactMeasure(long total, long high, long medium, long low, long info, long blocker) {
423 Map<String, Long> map = getImpactMeasure(total, high, medium, low);
424 map.put(Severity.INFO.name(), info);
425 map.put(Severity.BLOCKER.name(), blocker);
429 private void assertOverallSoftwareQualityMeasures(SoftwareQuality softwareQuality, Map<? extends String, Long> expectedMap,
430 Set<Map.Entry<String, Measure>> actualRaw) {
431 assertSoftwareQualityMeasures(softwareQuality, expectedMap, actualRaw, IMPACT_TO_METRIC_KEY);
434 private void assertNewSoftwareQualityMeasures(SoftwareQuality softwareQuality, Map<? extends String, Long> expectedMap,
435 Set<Map.Entry<String, Measure>> actualRaw) {
436 assertSoftwareQualityMeasures(softwareQuality, expectedMap, actualRaw, IMPACT_TO_NEW_METRIC_KEY);
439 private void assertSoftwareQualityMeasures(SoftwareQuality softwareQuality, Map<? extends String, Long> expectedMap,
440 Set<Map.Entry<String, Measure>> actualRaw, Map<String, String> impactToMetricMap) {
442 Map.Entry<String, Measure> softwareQualityMap = actualRaw.stream()
443 .filter(e -> e.getKey().equals(impactToMetricMap.get(softwareQuality.name())))
447 assertJson(softwareQualityMap.getValue().getData()).isSimilarTo(new Gson().toJson(expectedMap));
451 void count_high_impact_accepted_issues() {
452 when(newIssueClassifier.isEnabled()).thenReturn(true);
454 underTest.beforeComponent(FILE1);
455 // created before -> existing issues with 1 high impact accepted
456 underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, HIGH));
457 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, HIGH));
458 underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, MEDIUM));
460 // created after -> 2 high impact accepted
461 underTest.onIssue(FILE1, createNewIssue(null, STATUS_OPEN, HIGH));
462 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, HIGH));
463 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, HIGH));
464 underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, MEDIUM));
465 underTest.onIssue(FILE1, createNewSecurityHotspot());
466 underTest.afterComponent(FILE1);
468 underTest.beforeComponent(PROJECT);
469 underTest.afterComponent(PROJECT);
471 assertIntValue(FILE1, entry(VIOLATIONS_KEY, 2), entry(NEW_VIOLATIONS_KEY, 1), entry(NEW_ACCEPTED_ISSUES_KEY, 3),
472 entry(HIGH_IMPACT_ACCEPTED_ISSUES_KEY, 3));
473 assertIntValue(PROJECT, entry(VIOLATIONS_KEY, 2), entry(NEW_VIOLATIONS_KEY, 1), entry(NEW_ACCEPTED_ISSUES_KEY, 3),
474 entry(HIGH_IMPACT_ACCEPTED_ISSUES_KEY, 3));
478 void exclude_hotspots_from_issue_counts() {
479 // bottom-up traversal -> from files to project
480 underTest.beforeComponent(FILE1);
481 underTest.onIssue(FILE1, createSecurityHotspot());
482 underTest.onIssue(FILE1, createSecurityHotspot());
483 underTest.afterComponent(FILE1);
485 underTest.beforeComponent(FILE2);
486 underTest.onIssue(FILE2, createSecurityHotspot());
487 underTest.afterComponent(FILE2);
489 underTest.beforeComponent(FILE3);
490 underTest.afterComponent(FILE3);
492 underTest.beforeComponent(PROJECT);
493 underTest.afterComponent(PROJECT);
495 assertMeasures(FILE1, entry(VIOLATIONS_KEY, 0), entry(OPEN_ISSUES_KEY, 0), entry(CONFIRMED_ISSUES_KEY, 0));
496 assertMeasures(FILE2, entry(VIOLATIONS_KEY, 0), entry(OPEN_ISSUES_KEY, 0), entry(CONFIRMED_ISSUES_KEY, 0));
497 assertMeasures(FILE3, entry(VIOLATIONS_KEY, 0));
498 assertMeasures(PROJECT, entry(VIOLATIONS_KEY, 0), entry(OPEN_ISSUES_KEY, 0), entry(CONFIRMED_ISSUES_KEY, 0));
502 void exclude_new_hotspots_from_issue_counts() {
503 when(newIssueClassifier.isEnabled()).thenReturn(true);
505 underTest.beforeComponent(FILE1);
506 // created before -> existing issues (so ignored)
507 underTest.onIssue(FILE1, createSecurityHotspot());
508 underTest.onIssue(FILE1, createSecurityHotspot());
510 // created after, but closed
511 underTest.onIssue(FILE1, createNewSecurityHotspot().setStatus(STATUS_RESOLVED).setResolution(RESOLUTION_WONT_FIX));
513 for (String severity : Arrays.asList(CRITICAL, BLOCKER, MAJOR)) {
514 DefaultIssue issue = createNewSecurityHotspot();
515 issue.setSeverity(severity);
516 underTest.onIssue(FILE1, issue);
518 underTest.afterComponent(FILE1);
520 underTest.beforeComponent(FILE2);
521 underTest.afterComponent(FILE2);
523 underTest.beforeComponent(PROJECT);
524 underTest.afterComponent(PROJECT);
526 assertIntValue(FILE1, entry(NEW_VIOLATIONS_KEY, 0), entry(NEW_CRITICAL_VIOLATIONS_KEY, 0), entry(NEW_BLOCKER_VIOLATIONS_KEY, 0),
527 entry(NEW_MAJOR_VIOLATIONS_KEY, 0),
528 entry(NEW_VULNERABILITIES_KEY, 0));
529 assertIntValue(PROJECT, entry(NEW_VIOLATIONS_KEY, 0), entry(NEW_CRITICAL_VIOLATIONS_KEY, 0), entry(NEW_BLOCKER_VIOLATIONS_KEY, 0),
530 entry(NEW_MAJOR_VIOLATIONS_KEY, 0),
531 entry(NEW_VULNERABILITIES_KEY, 0));
535 private void assertIntValue(Component componentRef, MapEntry<String, Integer>... entries) {
536 assertThat(measureRepository.getRawMeasures(componentRef).entrySet()
538 .filter(e -> e.getValue().getValueType() == Measure.ValueType.INT)
539 .map(e -> entry(e.getKey(), e.getValue().getIntValue())))
544 private void assertMeasures(Component componentRef, Map.Entry<String, Integer>... entries) {
545 List<MeasureRepoEntry> expected = stream(entries)
546 .map(e -> entryOf(e.getKey(), newMeasureBuilder().create(e.getValue())))
549 assertThat(measureRepository.getRawMeasures(componentRef).entrySet().stream().map(e -> entryOf(e.getKey(), e.getValue())))
550 .containsAll(expected);
553 private DefaultIssue createNewIssue(@Nullable String resolution, String status, String severity) {
554 return createNewIssue(resolution, status, severity, CODE_SMELL);
557 private DefaultIssue createNewIssue(@Nullable String resolution, String status, Severity impactSeverity) {
558 return createNewIssue(resolution, status, SoftwareQuality.MAINTAINABILITY, impactSeverity);
561 private DefaultIssue createNewIssue(@Nullable String resolution, String status, SoftwareQuality softwareQuality,
562 Severity impactSeverity) {
563 DefaultIssue issue = createNewIssue(resolution, status, MAJOR, CODE_SMELL);
564 issue.addImpact(softwareQuality, impactSeverity);
568 private DefaultIssue createNewIssue(@Nullable String resolution, String status, String severity, RuleType ruleType) {
569 DefaultIssue issue = createIssue(resolution, status, severity, ruleType);
570 when(newIssueClassifier.isNew(any(), eq(issue))).thenReturn(true);
574 private static DefaultIssue createIssue(@Nullable String resolution, String status, String severity) {
575 return createIssue(resolution, status, severity, CODE_SMELL);
578 private static DefaultIssue createIssue(@Nullable String resolution, String status, Severity impactSeverity) {
579 return createIssue(resolution, status, SoftwareQuality.MAINTAINABILITY, impactSeverity);
582 private static DefaultIssue createIssue(@Nullable String resolution, String status, SoftwareQuality softwareQuality,
583 Severity impactSeverity) {
584 DefaultIssue issue = createIssue(resolution, status, MAJOR, CODE_SMELL);
585 issue.addImpact(softwareQuality, impactSeverity);
589 private static DefaultIssue createIssue(@Nullable String resolution, String status, String severity, RuleType ruleType) {
590 return new DefaultIssue()
591 .setKey(String.valueOf(++issueCounter))
592 .setResolution(resolution).setStatus(status)
593 .setSeverity(severity).setRuleKey(RuleTesting.XOO_X1)
597 private static DefaultIssue createSecurityHotspot() {
598 return createIssue(null, STATUS_OPEN, "MAJOR", SECURITY_HOTSPOT);
601 private DefaultIssue createNewSecurityHotspot() {
602 return createNewIssue(null, STATUS_OPEN, "MAJOR", SECURITY_HOTSPOT);