]> source.dussan.org Git - sonarqube.git/blob
7ad66ed9b4089475415a74510ad14624bfaba938
[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
130 class IssueCounterTest {
131
132   private static final Component FILE1 = builder(Component.Type.FILE, 1).build();
133   private static final Component FILE2 = builder(Component.Type.FILE, 2).build();
134   private static final Component FILE3 = builder(Component.Type.FILE, 3).build();
135   private static final Component PROJECT = builder(Component.Type.PROJECT, 4).addChildren(FILE1, FILE2, FILE3).build();
136
137   @RegisterExtension
138   private final BatchReportReaderRule reportReader = new BatchReportReaderRule();
139
140   @RegisterExtension
141   private final TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
142
143   @RegisterExtension
144   private final MetricRepositoryRule metricRepository = new MetricRepositoryRule()
145     .add(VIOLATIONS)
146     .add(OPEN_ISSUES)
147     .add(REOPENED_ISSUES)
148     .add(CONFIRMED_ISSUES)
149     .add(BLOCKER_VIOLATIONS)
150     .add(CRITICAL_VIOLATIONS)
151     .add(MAJOR_VIOLATIONS)
152     .add(MINOR_VIOLATIONS)
153     .add(INFO_VIOLATIONS)
154     .add(NEW_VIOLATIONS)
155     .add(NEW_BLOCKER_VIOLATIONS)
156     .add(NEW_CRITICAL_VIOLATIONS)
157     .add(NEW_MAJOR_VIOLATIONS)
158     .add(NEW_MINOR_VIOLATIONS)
159     .add(NEW_INFO_VIOLATIONS)
160     .add(FALSE_POSITIVE_ISSUES)
161     .add(ACCEPTED_ISSUES)
162     .add(CODE_SMELLS)
163     .add(BUGS)
164     .add(VULNERABILITIES)
165     .add(SECURITY_HOTSPOTS)
166     .add(NEW_CODE_SMELLS)
167     .add(NEW_BUGS)
168     .add(NEW_VULNERABILITIES)
169     .add(NEW_SECURITY_HOTSPOTS)
170     .add(NEW_ACCEPTED_ISSUES)
171     .add(HIGH_IMPACT_ACCEPTED_ISSUES)
172     .add(RELIABILITY_ISSUES)
173     .add(MAINTAINABILITY_ISSUES)
174     .add(SECURITY_ISSUES)
175     .add(NEW_RELIABILITY_ISSUES)
176     .add(NEW_MAINTAINABILITY_ISSUES)
177     .add(NEW_SECURITY_ISSUES);
178
179   @RegisterExtension
180   private final MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
181   private final NewIssueClassifier newIssueClassifier = mock(NewIssueClassifier.class);
182   private final IssueCounter underTest = new IssueCounter(metricRepository, measureRepository, newIssueClassifier);
183   private static int issueCounter;
184
185   @Test
186   void count_issues_by_status() {
187     // bottom-up traversal -> from files to project
188     underTest.beforeComponent(FILE1);
189     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER));
190     underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR));
191     underTest.onIssue(FILE1, createIssue(RESOLUTION_FALSE_POSITIVE, STATUS_RESOLVED, MAJOR));
192     underTest.afterComponent(FILE1);
193
194     underTest.beforeComponent(FILE2);
195     underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, BLOCKER));
196     underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, MAJOR));
197     underTest.afterComponent(FILE2);
198
199     underTest.beforeComponent(FILE3);
200     // Security hotspot should be ignored
201     underTest.onIssue(FILE3, createSecurityHotspot().setStatus(STATUS_OPEN));
202     underTest.afterComponent(FILE3);
203
204     underTest.beforeComponent(PROJECT);
205     underTest.afterComponent(PROJECT);
206
207     assertMeasures(FILE1, entry(VIOLATIONS_KEY, 1), entry(OPEN_ISSUES_KEY, 1), entry(CONFIRMED_ISSUES_KEY, 0));
208     assertMeasures(FILE2, entry(VIOLATIONS_KEY, 2), entry(OPEN_ISSUES_KEY, 0), entry(CONFIRMED_ISSUES_KEY, 2));
209     assertMeasures(FILE3, entry(VIOLATIONS_KEY, 0));
210     assertMeasures(PROJECT, entry(VIOLATIONS_KEY, 3), entry(OPEN_ISSUES_KEY, 1), entry(CONFIRMED_ISSUES_KEY, 2));
211   }
212
213   @Test
214   void count_issues_by_resolution() {
215     // bottom-up traversal -> from files to project
216     underTest.beforeComponent(FILE1);
217     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER));
218     underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR));
219     underTest.onIssue(FILE1, createIssue(RESOLUTION_FALSE_POSITIVE, STATUS_RESOLVED, MAJOR));
220     underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, MAJOR));
221     underTest.afterComponent(FILE1);
222
223     underTest.beforeComponent(FILE2);
224     underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, BLOCKER));
225     underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, MAJOR));
226     underTest.onIssue(FILE2, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, MAJOR));
227     underTest.afterComponent(FILE2);
228
229     underTest.beforeComponent(FILE3);
230     // Security hotspot should be ignored
231     underTest.onIssue(FILE3, createSecurityHotspot().setResolution(RESOLUTION_WONT_FIX));
232     underTest.afterComponent(FILE3);
233
234     underTest.beforeComponent(PROJECT);
235     underTest.afterComponent(PROJECT);
236
237     assertMeasures(FILE1, entry(VIOLATIONS_KEY, 1), entry(FALSE_POSITIVE_ISSUES_KEY, 1), entry(ACCEPTED_ISSUES_KEY, 1));
238     assertMeasures(FILE2, entry(VIOLATIONS_KEY, 2), entry(FALSE_POSITIVE_ISSUES_KEY, 0), entry(ACCEPTED_ISSUES_KEY, 1));
239     assertMeasures(FILE3, entry(VIOLATIONS_KEY, 0));
240     assertMeasures(PROJECT, entry(VIOLATIONS_KEY, 3), entry(FALSE_POSITIVE_ISSUES_KEY, 1), entry(ACCEPTED_ISSUES_KEY, 2));
241   }
242
243   @Test
244   void count_unresolved_issues_by_severity() {
245     // bottom-up traversal -> from files to project
246     underTest.beforeComponent(FILE1);
247     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER));
248     // this resolved issue is ignored
249     underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR));
250     underTest.afterComponent(FILE1);
251
252     underTest.beforeComponent(FILE2);
253     underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, BLOCKER));
254     underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, MAJOR));
255     underTest.afterComponent(FILE2);
256
257     underTest.beforeComponent(PROJECT);
258     // Security hotspot should be ignored
259     underTest.onIssue(FILE3, createSecurityHotspot().setSeverity(MAJOR));
260     underTest.afterComponent(PROJECT);
261
262     assertMeasures(FILE1, entry(BLOCKER_VIOLATIONS_KEY, 1), entry(CRITICAL_VIOLATIONS_KEY, 0), entry(MAJOR_VIOLATIONS_KEY, 0));
263     assertMeasures(FILE2, entry(BLOCKER_VIOLATIONS_KEY, 1), entry(CRITICAL_VIOLATIONS_KEY, 0), entry(MAJOR_VIOLATIONS_KEY, 1));
264     assertMeasures(PROJECT, entry(BLOCKER_VIOLATIONS_KEY, 2), entry(CRITICAL_VIOLATIONS_KEY, 0), entry(MAJOR_VIOLATIONS_KEY, 1));
265   }
266
267   @Test
268   void count_unresolved_issues_by_type() {
269     // bottom-up traversal -> from files to project
270     // file1 : one open code smell, one closed code smell (which will be excluded from metric)
271     underTest.beforeComponent(FILE1);
272     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER).setType(CODE_SMELL));
273     underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR).setType(CODE_SMELL));
274     underTest.afterComponent(FILE1);
275
276     // file2 : one bug
277     underTest.beforeComponent(FILE2);
278     underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, BLOCKER).setType(BUG));
279     underTest.afterComponent(FILE2);
280
281     // file3 : one unresolved security hotspot
282     underTest.beforeComponent(FILE3);
283     underTest.onIssue(FILE3, createSecurityHotspot());
284     underTest.onIssue(FILE3, createSecurityHotspot().setResolution(RESOLUTION_WONT_FIX).setStatus(STATUS_CLOSED));
285     underTest.afterComponent(FILE3);
286
287     underTest.beforeComponent(PROJECT);
288     underTest.afterComponent(PROJECT);
289
290     assertMeasures(FILE1, entry(CODE_SMELLS_KEY, 1), entry(BUGS_KEY, 0), entry(VULNERABILITIES_KEY, 0), entry(SECURITY_HOTSPOTS_KEY, 0));
291     assertMeasures(FILE2, entry(CODE_SMELLS_KEY, 0), entry(BUGS_KEY, 1), entry(VULNERABILITIES_KEY, 0), entry(SECURITY_HOTSPOTS_KEY, 0));
292     assertMeasures(FILE3, entry(CODE_SMELLS_KEY, 0), entry(BUGS_KEY, 0), entry(VULNERABILITIES_KEY, 0), entry(SECURITY_HOTSPOTS_KEY, 1));
293     assertMeasures(PROJECT, entry(CODE_SMELLS_KEY, 1), entry(BUGS_KEY, 1), entry(VULNERABILITIES_KEY, 0), entry(SECURITY_HOTSPOTS_KEY, 1));
294   }
295
296   @Test
297   void count_new_issues() {
298     when(newIssueClassifier.isEnabled()).thenReturn(true);
299
300     underTest.beforeComponent(FILE1);
301     // created before -> existing issues (so ignored)
302     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER).setType(CODE_SMELL));
303     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER).setType(BUG));
304
305     // created after -> 4 new issues but 1 is closed
306     underTest.onIssue(FILE1, createNewIssue(null, STATUS_OPEN, CRITICAL).setType(CODE_SMELL));
307     underTest.onIssue(FILE1, createNewIssue(null, STATUS_OPEN, CRITICAL).setType(BUG));
308     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR).setType(BUG));
309     underTest.onIssue(FILE1, createNewSecurityHotspot());
310     underTest.onIssue(FILE1, createNewSecurityHotspot().setResolution(RESOLUTION_WONT_FIX).setStatus(STATUS_CLOSED));
311     underTest.afterComponent(FILE1);
312
313     underTest.beforeComponent(FILE2);
314     underTest.afterComponent(FILE2);
315
316     underTest.beforeComponent(PROJECT);
317     underTest.afterComponent(PROJECT);
318
319     assertIntValue(FILE1, entry(NEW_VIOLATIONS_KEY, 2), entry(NEW_CRITICAL_VIOLATIONS_KEY, 2), entry(NEW_BLOCKER_VIOLATIONS_KEY, 0), entry(NEW_MAJOR_VIOLATIONS_KEY, 0),
320       entry(NEW_CODE_SMELLS_KEY, 1), entry(NEW_BUGS_KEY, 1), entry(NEW_VULNERABILITIES_KEY, 0), entry(NEW_SECURITY_HOTSPOTS_KEY, 1));
321     assertIntValue(PROJECT, entry(NEW_VIOLATIONS_KEY, 2), entry(NEW_CRITICAL_VIOLATIONS_KEY, 2), entry(NEW_BLOCKER_VIOLATIONS_KEY, 0), 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   }
324
325   @Test
326   void count_new_accepted_issues() {
327     when(newIssueClassifier.isEnabled()).thenReturn(true);
328
329     underTest.beforeComponent(FILE1);
330     // created before -> existing issues (so ignored)
331     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, CRITICAL));
332     underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, CRITICAL));
333
334     // created after -> 2 accepted, 1 open, 1 hotspot
335     underTest.onIssue(FILE1, createNewIssue(null, STATUS_OPEN, CRITICAL));
336     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, CRITICAL));
337     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, CRITICAL));
338     underTest.onIssue(FILE1, createNewSecurityHotspot());
339     underTest.afterComponent(FILE1);
340
341     underTest.beforeComponent(PROJECT);
342     underTest.afterComponent(PROJECT);
343
344     assertIntValue(FILE1, entry(NEW_VIOLATIONS_KEY, 1), entry(NEW_ACCEPTED_ISSUES_KEY, 2), entry(NEW_SECURITY_HOTSPOTS_KEY, 1));
345     assertIntValue(PROJECT, entry(NEW_VIOLATIONS_KEY, 1), entry(NEW_ACCEPTED_ISSUES_KEY, 2), entry(NEW_SECURITY_HOTSPOTS_KEY, 1));
346   }
347
348   @Test
349   void onIssue_shouldCountOverallSoftwareQualitiesMeasures() {
350     when(newIssueClassifier.isEnabled()).thenReturn(true);
351
352     underTest.beforeComponent(FILE1);
353     underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY,  HIGH));
354     underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, MEDIUM));
355
356     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, HIGH));
357     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, SoftwareQuality.MAINTAINABILITY, HIGH));
358     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, MEDIUM));
359
360     underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY,  HIGH));
361     underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, MEDIUM));
362
363     underTest.onIssue(FILE1, createNewSecurityHotspot());
364     underTest.afterComponent(FILE1);
365
366     underTest.beforeComponent(PROJECT);
367     underTest.afterComponent(PROJECT);
368
369     Set<Map.Entry<String, Measure>> entries = measureRepository.getRawMeasures(FILE1).entrySet();
370
371     assertOverallSoftwareQualityMeasures(SoftwareQuality.MAINTAINABILITY, getImpactMeasure(4, 2, 2, 0), entries);
372     assertOverallSoftwareQualityMeasures(SoftwareQuality.SECURITY, getImpactMeasure(2, 1, 1, 0), entries);
373     assertOverallSoftwareQualityMeasures(SoftwareQuality.RELIABILITY, getImpactMeasure(0, 0, 0, 0), entries);
374   }
375
376   @Test
377   void onIssue_shouldCountNewSoftwareQualitiesMeasures() {
378     when(newIssueClassifier.isEnabled()).thenReturn(true);
379
380     underTest.beforeComponent(FILE1);
381     underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY,  HIGH));
382     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, HIGH));
383     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, SoftwareQuality.MAINTAINABILITY, HIGH));
384     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.MAINTAINABILITY, MEDIUM));
385
386     underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.RELIABILITY,  HIGH));
387     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.RELIABILITY, LOW));
388     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, SoftwareQuality.RELIABILITY, HIGH));
389     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.RELIABILITY, MEDIUM));
390
391     underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY,  MEDIUM));
392     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, LOW));
393     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, HIGH));
394     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, HIGH));
395     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, SoftwareQuality.SECURITY, HIGH));
396     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_OPEN, SoftwareQuality.SECURITY, MEDIUM));
397
398     underTest.afterComponent(FILE1);
399
400     underTest.beforeComponent(PROJECT);
401     underTest.afterComponent(PROJECT);
402
403     Set<Map.Entry<String, Measure>> entries = measureRepository.getRawMeasures(FILE1).entrySet();
404
405     assertNewSoftwareQualityMeasures(SoftwareQuality.MAINTAINABILITY, getImpactMeasure(2, 1, 1, 0), entries);
406     assertNewSoftwareQualityMeasures(SoftwareQuality.RELIABILITY, getImpactMeasure(2, 0, 1, 1), entries);
407     assertNewSoftwareQualityMeasures(SoftwareQuality.SECURITY, getImpactMeasure(4, 2, 1, 1), entries);
408   }
409
410   private static Map<String, Long> getImpactMeasure(long total, long high, long medium, long low) {
411     Map<String, Long> map = new LinkedHashMap<>();
412     map.put(LOW.name(), low);
413     map.put(MEDIUM.name(), medium);
414     map.put(HIGH.name(), high);
415     map.put(ImpactMeasureBuilder.TOTAL_KEY, total);
416     return map;
417   }
418
419   private void assertOverallSoftwareQualityMeasures(SoftwareQuality softwareQuality, Map<? extends String, Long> expectedMap,
420     Set<Map.Entry<String, Measure>> actualRaw) {
421     assertSoftwareQualityMeasures(softwareQuality, expectedMap, actualRaw, IMPACT_TO_METRIC_KEY);
422   }
423
424   private void assertNewSoftwareQualityMeasures(SoftwareQuality softwareQuality, Map<? extends String, Long> expectedMap,
425     Set<Map.Entry<String, Measure>> actualRaw) {
426     assertSoftwareQualityMeasures(softwareQuality, expectedMap, actualRaw, IMPACT_TO_NEW_METRIC_KEY);
427   }
428
429   private void assertSoftwareQualityMeasures(SoftwareQuality softwareQuality, Map<? extends String, Long> expectedMap,
430     Set<Map.Entry<String, Measure>> actualRaw, Map<String, String> impactToMetricMap) {
431
432     Map.Entry<String, Measure> softwareQualityMap = actualRaw.stream()
433       .filter(e -> e.getKey().equals(impactToMetricMap.get(softwareQuality.name())))
434       .findFirst()
435       .get();
436
437     assertThat(softwareQualityMap.getValue().getData()).isEqualTo(new Gson().toJson(expectedMap));
438   }
439
440   @Test
441   void count_high_impact_accepted_issues() {
442     when(newIssueClassifier.isEnabled()).thenReturn(true);
443
444     underTest.beforeComponent(FILE1);
445     // created before -> existing issues with 1 high impact accepted
446     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, HIGH));
447     underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, HIGH));
448     underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, MEDIUM));
449
450     // created after -> 2 high impact accepted
451     underTest.onIssue(FILE1, createNewIssue(null, STATUS_OPEN, HIGH));
452     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, HIGH));
453     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, HIGH));
454     underTest.onIssue(FILE1, createNewIssue(RESOLUTION_WONT_FIX, STATUS_RESOLVED, MEDIUM));
455     underTest.onIssue(FILE1, createNewSecurityHotspot());
456     underTest.afterComponent(FILE1);
457
458     underTest.beforeComponent(PROJECT);
459     underTest.afterComponent(PROJECT);
460
461     assertIntValue(FILE1, entry(VIOLATIONS_KEY, 2), entry(NEW_VIOLATIONS_KEY, 1), entry(NEW_ACCEPTED_ISSUES_KEY, 3),
462       entry(HIGH_IMPACT_ACCEPTED_ISSUES_KEY, 3));
463     assertIntValue(PROJECT, entry(VIOLATIONS_KEY, 2), entry(NEW_VIOLATIONS_KEY, 1), entry(NEW_ACCEPTED_ISSUES_KEY, 3),
464       entry(HIGH_IMPACT_ACCEPTED_ISSUES_KEY, 3));
465   }
466
467   @Test
468   void exclude_hotspots_from_issue_counts() {
469     // bottom-up traversal -> from files to project
470     underTest.beforeComponent(FILE1);
471     underTest.onIssue(FILE1, createSecurityHotspot());
472     underTest.onIssue(FILE1, createSecurityHotspot());
473     underTest.afterComponent(FILE1);
474
475     underTest.beforeComponent(FILE2);
476     underTest.onIssue(FILE2, createSecurityHotspot());
477     underTest.afterComponent(FILE2);
478
479     underTest.beforeComponent(FILE3);
480     underTest.afterComponent(FILE3);
481
482     underTest.beforeComponent(PROJECT);
483     underTest.afterComponent(PROJECT);
484
485     assertMeasures(FILE1, entry(VIOLATIONS_KEY, 0), entry(OPEN_ISSUES_KEY, 0), entry(CONFIRMED_ISSUES_KEY, 0));
486     assertMeasures(FILE2, entry(VIOLATIONS_KEY, 0), entry(OPEN_ISSUES_KEY, 0), entry(CONFIRMED_ISSUES_KEY, 0));
487     assertMeasures(FILE3, entry(VIOLATIONS_KEY, 0));
488     assertMeasures(PROJECT, entry(VIOLATIONS_KEY, 0), entry(OPEN_ISSUES_KEY, 0), entry(CONFIRMED_ISSUES_KEY, 0));
489   }
490
491   @Test
492   void exclude_new_hotspots_from_issue_counts() {
493     when(newIssueClassifier.isEnabled()).thenReturn(true);
494
495     underTest.beforeComponent(FILE1);
496     // created before -> existing issues (so ignored)
497     underTest.onIssue(FILE1, createSecurityHotspot());
498     underTest.onIssue(FILE1, createSecurityHotspot());
499
500     // created after, but closed
501     underTest.onIssue(FILE1, createNewSecurityHotspot().setStatus(STATUS_RESOLVED).setResolution(RESOLUTION_WONT_FIX));
502
503     for (String severity : Arrays.asList(CRITICAL, BLOCKER, MAJOR)) {
504       DefaultIssue issue = createNewSecurityHotspot();
505       issue.setSeverity(severity);
506       underTest.onIssue(FILE1, issue);
507     }
508     underTest.afterComponent(FILE1);
509
510     underTest.beforeComponent(FILE2);
511     underTest.afterComponent(FILE2);
512
513     underTest.beforeComponent(PROJECT);
514     underTest.afterComponent(PROJECT);
515
516     assertIntValue(FILE1, entry(NEW_VIOLATIONS_KEY, 0), entry(NEW_CRITICAL_VIOLATIONS_KEY, 0), entry(NEW_BLOCKER_VIOLATIONS_KEY, 0), entry(NEW_MAJOR_VIOLATIONS_KEY, 0),
517       entry(NEW_VULNERABILITIES_KEY, 0));
518     assertIntValue(PROJECT, entry(NEW_VIOLATIONS_KEY, 0), entry(NEW_CRITICAL_VIOLATIONS_KEY, 0), entry(NEW_BLOCKER_VIOLATIONS_KEY, 0), entry(NEW_MAJOR_VIOLATIONS_KEY, 0),
519       entry(NEW_VULNERABILITIES_KEY, 0));
520   }
521
522   @SafeVarargs
523   private void assertIntValue(Component componentRef, MapEntry<String, Integer>... entries) {
524     assertThat(measureRepository.getRawMeasures(componentRef).entrySet()
525       .stream()
526       .filter(e -> e.getValue().getValueType() == Measure.ValueType.INT)
527       .map(e -> entry(e.getKey(), e.getValue().getIntValue())))
528       .contains(entries);
529   }
530
531   @SafeVarargs
532   private void assertMeasures(Component componentRef, Map.Entry<String, Integer>... entries) {
533     List<MeasureRepoEntry> expected = stream(entries)
534       .map(e -> entryOf(e.getKey(), newMeasureBuilder().create(e.getValue())))
535       .toList();
536
537     assertThat(measureRepository.getRawMeasures(componentRef).entrySet().stream().map(e -> entryOf(e.getKey(), e.getValue())))
538       .containsAll(expected);
539   }
540
541   private DefaultIssue createNewIssue(@Nullable String resolution, String status, String severity) {
542     return createNewIssue(resolution, status, severity, CODE_SMELL);
543   }
544
545   private DefaultIssue createNewIssue(@Nullable String resolution, String status, Severity impactSeverity) {
546     return createNewIssue(resolution, status, SoftwareQuality.MAINTAINABILITY, impactSeverity);
547   }
548
549   private DefaultIssue createNewIssue(@Nullable String resolution, String status, SoftwareQuality softwareQuality, Severity impactSeverity) {
550     DefaultIssue issue = createNewIssue(resolution, status, MAJOR, CODE_SMELL);
551     issue.addImpact(softwareQuality, impactSeverity);
552     return issue;
553   }
554
555   private DefaultIssue createNewIssue(@Nullable String resolution, String status, String severity, RuleType ruleType) {
556     DefaultIssue issue = createIssue(resolution, status, severity, ruleType);
557     when(newIssueClassifier.isNew(any(), eq(issue))).thenReturn(true);
558     return issue;
559   }
560
561   private static DefaultIssue createIssue(@Nullable String resolution, String status, String severity) {
562     return createIssue(resolution, status, severity, CODE_SMELL);
563   }
564
565   private static DefaultIssue createIssue(@Nullable String resolution, String status, Severity impactSeverity) {
566     return createIssue(resolution, status, SoftwareQuality.MAINTAINABILITY, impactSeverity);
567   }
568
569   private static DefaultIssue createIssue(@Nullable String resolution, String status, SoftwareQuality softwareQuality, Severity impactSeverity) {
570     DefaultIssue issue = createIssue(resolution, status, MAJOR, CODE_SMELL);
571     issue.addImpact(softwareQuality, impactSeverity);
572     return issue;
573   }
574
575   private static DefaultIssue createIssue(@Nullable String resolution, String status, String severity, RuleType ruleType) {
576     return new DefaultIssue()
577       .setKey(String.valueOf(++issueCounter))
578       .setResolution(resolution).setStatus(status)
579       .setSeverity(severity).setRuleKey(RuleTesting.XOO_X1)
580       .setType(ruleType);
581   }
582
583   private static DefaultIssue createSecurityHotspot() {
584     return createIssue(null, STATUS_OPEN, "MAJOR", SECURITY_HOTSPOT);
585   }
586
587   private DefaultIssue createNewSecurityHotspot() {
588     return createNewIssue(null, STATUS_OPEN, "MAJOR", SECURITY_HOTSPOT);
589   }
590 }