]> source.dussan.org Git - sonarqube.git/blob
f47a6da5495fb475bfe74d4cc01107506fa061a6
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.gson.Gson;
23 import java.util.Arrays;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
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;
45
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;
130
131 class IssueCounterTest {
132
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();
137
138   @RegisterExtension
139   private final BatchReportReaderRule reportReader = new BatchReportReaderRule();
140
141   @RegisterExtension
142   private final TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
143
144   @RegisterExtension
145   private final MetricRepositoryRule metricRepository = new MetricRepositoryRule()
146     .add(VIOLATIONS)
147     .add(OPEN_ISSUES)
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)
155     .add(NEW_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)
163     .add(CODE_SMELLS)
164     .add(BUGS)
165     .add(VULNERABILITIES)
166     .add(SECURITY_HOTSPOTS)
167     .add(NEW_CODE_SMELLS)
168     .add(NEW_BUGS)
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);
179
180   @RegisterExtension
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;
185
186   @Test
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);
194
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);
199
200     underTest.beforeComponent(FILE3);
201     // Security hotspot should be ignored
202     underTest.onIssue(FILE3, createSecurityHotspot().setStatus(STATUS_OPEN));
203     underTest.afterComponent(FILE3);
204
205     underTest.beforeComponent(PROJECT);
206     underTest.afterComponent(PROJECT);
207
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));
212   }
213
214   @Test
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);
223
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);
229
230     underTest.beforeComponent(FILE3);
231     // Security hotspot should be ignored
232     underTest.onIssue(FILE3, createSecurityHotspot().setResolution(RESOLUTION_WONT_FIX));
233     underTest.afterComponent(FILE3);
234
235     underTest.beforeComponent(PROJECT);
236     underTest.afterComponent(PROJECT);
237
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));
242   }
243
244   @Test
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);
252
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);
257
258     underTest.beforeComponent(PROJECT);
259     // Security hotspot should be ignored
260     underTest.onIssue(FILE3, createSecurityHotspot().setSeverity(MAJOR));
261     underTest.afterComponent(PROJECT);
262
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));
266   }
267
268   @Test
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);
276
277     // file2 : one bug
278     underTest.beforeComponent(FILE2);
279     underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, BLOCKER).setType(BUG));
280     underTest.afterComponent(FILE2);
281
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);
287
288     underTest.beforeComponent(PROJECT);
289     underTest.afterComponent(PROJECT);
290
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));
295   }
296
297   @Test
298   void count_new_issues() {
299     when(newIssueClassifier.isEnabled()).thenReturn(true);
300
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));
305
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);
313
314     underTest.beforeComponent(FILE2);
315     underTest.afterComponent(FILE2);
316
317     underTest.beforeComponent(PROJECT);
318     underTest.afterComponent(PROJECT);
319
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));
326   }
327
328   @Test
329   void count_new_accepted_issues() {
330     when(newIssueClassifier.isEnabled()).thenReturn(true);
331
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));
336
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);
343
344     underTest.beforeComponent(PROJECT);
345     underTest.afterComponent(PROJECT);
346
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));
349   }
350
351   @Test
352   void onIssue_shouldCountOverallSoftwareQualitiesMeasures() {
353     when(newIssueClassifier.isEnabled()).thenReturn(true);
354
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));
358
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));
362
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));
365
366     underTest.onIssue(FILE1, createNewSecurityHotspot());
367     underTest.afterComponent(FILE1);
368
369     underTest.beforeComponent(PROJECT);
370     underTest.afterComponent(PROJECT);
371
372     Set<Map.Entry<String, Measure>> entries = measureRepository.getRawMeasures(FILE1).entrySet();
373
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);
377   }
378
379   @Test
380   void onIssue_shouldCountNewSoftwareQualitiesMeasures() {
381     when(newIssueClassifier.isEnabled()).thenReturn(true);
382
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));
388
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));
393
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));
400
401     underTest.afterComponent(FILE1);
402
403     underTest.beforeComponent(PROJECT);
404     underTest.afterComponent(PROJECT);
405
406     Set<Map.Entry<String, Measure>> entries = measureRepository.getRawMeasures(FILE1).entrySet();
407
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);
411   }
412
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);
419     return map;
420   }
421
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);
426     return map;
427   }
428
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);
432   }
433
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);
437   }
438
439   private void assertSoftwareQualityMeasures(SoftwareQuality softwareQuality, Map<? extends String, Long> expectedMap,
440     Set<Map.Entry<String, Measure>> actualRaw, Map<String, String> impactToMetricMap) {
441
442     Map.Entry<String, Measure> softwareQualityMap = actualRaw.stream()
443       .filter(e -> e.getKey().equals(impactToMetricMap.get(softwareQuality.name())))
444       .findFirst()
445       .get();
446
447     assertJson(softwareQualityMap.getValue().getData()).isSimilarTo(new Gson().toJson(expectedMap));
448   }
449
450   @Test
451   void count_high_impact_accepted_issues() {
452     when(newIssueClassifier.isEnabled()).thenReturn(true);
453
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));
459
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);
467
468     underTest.beforeComponent(PROJECT);
469     underTest.afterComponent(PROJECT);
470
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));
475   }
476
477   @Test
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);
484
485     underTest.beforeComponent(FILE2);
486     underTest.onIssue(FILE2, createSecurityHotspot());
487     underTest.afterComponent(FILE2);
488
489     underTest.beforeComponent(FILE3);
490     underTest.afterComponent(FILE3);
491
492     underTest.beforeComponent(PROJECT);
493     underTest.afterComponent(PROJECT);
494
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));
499   }
500
501   @Test
502   void exclude_new_hotspots_from_issue_counts() {
503     when(newIssueClassifier.isEnabled()).thenReturn(true);
504
505     underTest.beforeComponent(FILE1);
506     // created before -> existing issues (so ignored)
507     underTest.onIssue(FILE1, createSecurityHotspot());
508     underTest.onIssue(FILE1, createSecurityHotspot());
509
510     // created after, but closed
511     underTest.onIssue(FILE1, createNewSecurityHotspot().setStatus(STATUS_RESOLVED).setResolution(RESOLUTION_WONT_FIX));
512
513     for (String severity : Arrays.asList(CRITICAL, BLOCKER, MAJOR)) {
514       DefaultIssue issue = createNewSecurityHotspot();
515       issue.setSeverity(severity);
516       underTest.onIssue(FILE1, issue);
517     }
518     underTest.afterComponent(FILE1);
519
520     underTest.beforeComponent(FILE2);
521     underTest.afterComponent(FILE2);
522
523     underTest.beforeComponent(PROJECT);
524     underTest.afterComponent(PROJECT);
525
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));
532   }
533
534   @SafeVarargs
535   private void assertIntValue(Component componentRef, MapEntry<String, Integer>... entries) {
536     assertThat(measureRepository.getRawMeasures(componentRef).entrySet()
537       .stream()
538       .filter(e -> e.getValue().getValueType() == Measure.ValueType.INT)
539       .map(e -> entry(e.getKey(), e.getValue().getIntValue())))
540       .contains(entries);
541   }
542
543   @SafeVarargs
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())))
547       .toList();
548
549     assertThat(measureRepository.getRawMeasures(componentRef).entrySet().stream().map(e -> entryOf(e.getKey(), e.getValue())))
550       .containsAll(expected);
551   }
552
553   private DefaultIssue createNewIssue(@Nullable String resolution, String status, String severity) {
554     return createNewIssue(resolution, status, severity, CODE_SMELL);
555   }
556
557   private DefaultIssue createNewIssue(@Nullable String resolution, String status, Severity impactSeverity) {
558     return createNewIssue(resolution, status, SoftwareQuality.MAINTAINABILITY, impactSeverity);
559   }
560
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);
565     return issue;
566   }
567
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);
571     return issue;
572   }
573
574   private static DefaultIssue createIssue(@Nullable String resolution, String status, String severity) {
575     return createIssue(resolution, status, severity, CODE_SMELL);
576   }
577
578   private static DefaultIssue createIssue(@Nullable String resolution, String status, Severity impactSeverity) {
579     return createIssue(resolution, status, SoftwareQuality.MAINTAINABILITY, impactSeverity);
580   }
581
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);
586     return issue;
587   }
588
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)
594       .setType(ruleType);
595   }
596
597   private static DefaultIssue createSecurityHotspot() {
598     return createIssue(null, STATUS_OPEN, "MAJOR", SECURITY_HOTSPOT);
599   }
600
601   private DefaultIssue createNewSecurityHotspot() {
602     return createNewIssue(null, STATUS_OPEN, "MAJOR", SECURITY_HOTSPOT);
603   }
604 }