]> source.dussan.org Git - sonarqube.git/blob
0794d75dea38a312d351d9b77b3835f414be5714
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2018 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 java.util.Arrays;
23 import java.util.Date;
24 import javax.annotation.Nullable;
25 import org.assertj.core.data.Offset;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.measures.CoreMetrics;
29 import org.sonar.api.rules.RuleType;
30 import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
31 import org.sonar.ce.task.projectanalysis.component.Component;
32 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
33 import org.sonar.ce.task.projectanalysis.measure.Measure;
34 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
35 import org.sonar.ce.task.projectanalysis.metric.Metric;
36 import org.sonar.ce.task.projectanalysis.metric.MetricImpl;
37 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
38 import org.sonar.ce.task.projectanalysis.period.Period;
39 import org.sonar.ce.task.projectanalysis.period.PeriodHolderRule;
40 import org.sonar.core.issue.DefaultIssue;
41 import org.sonar.db.rule.RuleTesting;
42
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.sonar.api.issue.Issue.RESOLUTION_FALSE_POSITIVE;
45 import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
46 import static org.sonar.api.issue.Issue.RESOLUTION_WONT_FIX;
47 import static org.sonar.api.issue.Issue.STATUS_CLOSED;
48 import static org.sonar.api.issue.Issue.STATUS_CONFIRMED;
49 import static org.sonar.api.issue.Issue.STATUS_OPEN;
50 import static org.sonar.api.issue.Issue.STATUS_RESOLVED;
51 import static org.sonar.api.measures.CoreMetrics.BLOCKER_VIOLATIONS_KEY;
52 import static org.sonar.api.measures.CoreMetrics.CONFIRMED_ISSUES_KEY;
53 import static org.sonar.api.measures.CoreMetrics.CRITICAL_VIOLATIONS_KEY;
54 import static org.sonar.api.measures.CoreMetrics.FALSE_POSITIVE_ISSUES_KEY;
55 import static org.sonar.api.measures.CoreMetrics.INFO_VIOLATIONS_KEY;
56 import static org.sonar.api.measures.CoreMetrics.MAJOR_VIOLATIONS_KEY;
57 import static org.sonar.api.measures.CoreMetrics.MINOR_VIOLATIONS_KEY;
58 import static org.sonar.api.measures.CoreMetrics.NEW_BLOCKER_VIOLATIONS_KEY;
59 import static org.sonar.api.measures.CoreMetrics.NEW_CRITICAL_VIOLATIONS_KEY;
60 import static org.sonar.api.measures.CoreMetrics.NEW_INFO_VIOLATIONS_KEY;
61 import static org.sonar.api.measures.CoreMetrics.NEW_MAJOR_VIOLATIONS_KEY;
62 import static org.sonar.api.measures.CoreMetrics.NEW_MINOR_VIOLATIONS_KEY;
63 import static org.sonar.api.measures.CoreMetrics.NEW_VIOLATIONS_KEY;
64 import static org.sonar.api.measures.CoreMetrics.OPEN_ISSUES_KEY;
65 import static org.sonar.api.measures.CoreMetrics.REOPENED_ISSUES_KEY;
66 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS_KEY;
67 import static org.sonar.api.measures.CoreMetrics.WONT_FIX_ISSUES_KEY;
68 import static org.sonar.api.rule.Severity.BLOCKER;
69 import static org.sonar.api.rule.Severity.CRITICAL;
70 import static org.sonar.api.rule.Severity.MAJOR;
71 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
72 import static org.sonar.ce.task.projectanalysis.metric.Metric.MetricType.INT;
73
74 public class IssueCounterTest {
75
76   static final Component FILE1 = builder(Component.Type.FILE, 1).build();
77   static final Component FILE2 = builder(Component.Type.FILE, 2).build();
78   static final Component FILE3 = builder(Component.Type.FILE, 3).build();
79   static final Component PROJECT = builder(Component.Type.PROJECT, 4).addChildren(FILE1, FILE2, FILE3).build();
80
81   static final Metric ISSUES_METRIC = new MetricImpl(1, VIOLATIONS_KEY, VIOLATIONS_KEY, INT);
82   static final Metric OPEN_ISSUES_METRIC = new MetricImpl(2, OPEN_ISSUES_KEY, OPEN_ISSUES_KEY, INT);
83   static final Metric REOPENED_ISSUES_METRIC = new MetricImpl(3, REOPENED_ISSUES_KEY, REOPENED_ISSUES_KEY, INT);
84   static final Metric CONFIRMED_ISSUES_METRIC = new MetricImpl(4, CONFIRMED_ISSUES_KEY, CONFIRMED_ISSUES_KEY, INT);
85   static final Metric BLOCKER_ISSUES_METRIC = new MetricImpl(5, BLOCKER_VIOLATIONS_KEY, BLOCKER_VIOLATIONS_KEY, INT);
86   static final Metric CRITICAL_ISSUES_METRIC = new MetricImpl(6, CRITICAL_VIOLATIONS_KEY, CRITICAL_VIOLATIONS_KEY, INT);
87   static final Metric MAJOR_ISSUES_METRIC = new MetricImpl(7, MAJOR_VIOLATIONS_KEY, MAJOR_VIOLATIONS_KEY, INT);
88   static final Metric MINOR_ISSUES_METRIC = new MetricImpl(8, MINOR_VIOLATIONS_KEY, MINOR_VIOLATIONS_KEY, INT);
89   static final Metric INFO_ISSUES_METRIC = new MetricImpl(9, INFO_VIOLATIONS_KEY, INFO_VIOLATIONS_KEY, INT);
90   static final Metric NEW_ISSUES_METRIC = new MetricImpl(10, NEW_VIOLATIONS_KEY, NEW_VIOLATIONS_KEY, INT);
91   static final Metric NEW_BLOCKER_ISSUES_METRIC = new MetricImpl(11, NEW_BLOCKER_VIOLATIONS_KEY, NEW_BLOCKER_VIOLATIONS_KEY, INT);
92   static final Metric NEW_CRITICAL_ISSUES_METRIC = new MetricImpl(12, NEW_CRITICAL_VIOLATIONS_KEY, NEW_CRITICAL_VIOLATIONS_KEY, INT);
93   static final Metric NEW_MAJOR_ISSUES_METRIC = new MetricImpl(13, NEW_MAJOR_VIOLATIONS_KEY, NEW_MAJOR_VIOLATIONS_KEY, INT);
94   static final Metric NEW_MINOR_ISSUES_METRIC = new MetricImpl(14, NEW_MINOR_VIOLATIONS_KEY, NEW_MINOR_VIOLATIONS_KEY, INT);
95   static final Metric NEW_INFO_ISSUES_METRIC = new MetricImpl(15, NEW_INFO_VIOLATIONS_KEY, NEW_INFO_VIOLATIONS_KEY, INT);
96   static final Metric FALSE_POSITIVE_ISSUES_METRIC = new MetricImpl(16, FALSE_POSITIVE_ISSUES_KEY, FALSE_POSITIVE_ISSUES_KEY, INT);
97   static final Metric WONT_FIX_ISSUES_METRIC = new MetricImpl(23, WONT_FIX_ISSUES_KEY, WONT_FIX_ISSUES_KEY, INT);
98   static final Metric CODE_SMELLS_METRIC = new MetricImpl(17, CoreMetrics.CODE_SMELLS_KEY, CoreMetrics.CODE_SMELLS_KEY, INT);
99   static final Metric BUGS_METRIC = new MetricImpl(18, CoreMetrics.BUGS_KEY, CoreMetrics.BUGS_KEY, INT);
100   static final Metric VULNERABILITIES_METRIC = new MetricImpl(19, CoreMetrics.VULNERABILITIES_KEY, CoreMetrics.VULNERABILITIES_KEY, INT);
101   static final Metric NEW_CODE_SMELLS_METRIC = new MetricImpl(20, CoreMetrics.NEW_CODE_SMELLS_KEY, CoreMetrics.NEW_CODE_SMELLS_KEY, INT);
102   static final Metric NEW_BUGS_METRIC = new MetricImpl(21, CoreMetrics.NEW_BUGS_KEY, CoreMetrics.NEW_BUGS_KEY, INT);
103   static final Metric NEW_VULNERABILITIES_METRIC = new MetricImpl(22, CoreMetrics.NEW_VULNERABILITIES_KEY, CoreMetrics.NEW_VULNERABILITIES_KEY, INT);
104
105   @Rule
106   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
107
108   @Rule
109   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
110
111   @Rule
112   public PeriodHolderRule periodsHolder = new PeriodHolderRule();
113
114   @Rule
115   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
116     .add(ISSUES_METRIC)
117     .add(OPEN_ISSUES_METRIC)
118     .add(REOPENED_ISSUES_METRIC)
119     .add(CONFIRMED_ISSUES_METRIC)
120     .add(BLOCKER_ISSUES_METRIC)
121     .add(CRITICAL_ISSUES_METRIC)
122     .add(MAJOR_ISSUES_METRIC)
123     .add(MINOR_ISSUES_METRIC)
124     .add(INFO_ISSUES_METRIC)
125     .add(NEW_ISSUES_METRIC)
126     .add(NEW_BLOCKER_ISSUES_METRIC)
127     .add(NEW_CRITICAL_ISSUES_METRIC)
128     .add(NEW_MAJOR_ISSUES_METRIC)
129     .add(NEW_MINOR_ISSUES_METRIC)
130     .add(NEW_INFO_ISSUES_METRIC)
131     .add(FALSE_POSITIVE_ISSUES_METRIC)
132     .add(WONT_FIX_ISSUES_METRIC)
133     .add(CODE_SMELLS_METRIC)
134     .add(BUGS_METRIC)
135     .add(VULNERABILITIES_METRIC)
136     .add(NEW_CODE_SMELLS_METRIC)
137     .add(NEW_BUGS_METRIC)
138     .add(NEW_VULNERABILITIES_METRIC);
139
140   @Rule
141   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
142
143   private IssueCounter underTest = new IssueCounter(periodsHolder, metricRepository, measureRepository);
144
145   @Test
146   public void count_issues_by_status() {
147     periodsHolder.setPeriod(null);
148
149     // bottom-up traversal -> from files to project
150     underTest.beforeComponent(FILE1);
151     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER));
152     underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR));
153     underTest.onIssue(FILE1, createIssue(RESOLUTION_FALSE_POSITIVE, STATUS_RESOLVED, MAJOR));
154     underTest.afterComponent(FILE1);
155
156     underTest.beforeComponent(FILE2);
157     underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, BLOCKER));
158     underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, MAJOR));
159     underTest.afterComponent(FILE2);
160
161     underTest.beforeComponent(FILE3);
162     underTest.afterComponent(FILE3);
163
164     underTest.beforeComponent(PROJECT);
165     underTest.afterComponent(PROJECT);
166
167     assertThat(measureRepository.getRawMeasure(FILE1, ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
168     assertThat(measureRepository.getRawMeasure(FILE1, OPEN_ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
169     assertThat(measureRepository.getRawMeasure(FILE1, CONFIRMED_ISSUES_METRIC).get().getIntValue()).isEqualTo(0);
170
171     assertThat(measureRepository.getRawMeasure(FILE2, ISSUES_METRIC).get().getIntValue()).isEqualTo(2);
172     assertThat(measureRepository.getRawMeasure(FILE2, OPEN_ISSUES_METRIC).get().getIntValue()).isEqualTo(0);
173     assertThat(measureRepository.getRawMeasure(FILE2, CONFIRMED_ISSUES_METRIC).get().getIntValue()).isEqualTo(2);
174
175     assertThat(measureRepository.getRawMeasure(FILE3, ISSUES_METRIC).get().getIntValue()).isEqualTo(0);
176
177     assertThat(measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC).get().getIntValue()).isEqualTo(3);
178     assertThat(measureRepository.getRawMeasure(PROJECT, OPEN_ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
179     assertThat(measureRepository.getRawMeasure(PROJECT, CONFIRMED_ISSUES_METRIC).get().getIntValue()).isEqualTo(2);
180   }
181
182   @Test
183   public void count_issues_by_resolution() {
184     periodsHolder.setPeriod(null);
185
186     // bottom-up traversal -> from files to project
187     underTest.beforeComponent(FILE1);
188     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER));
189     underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR));
190     underTest.onIssue(FILE1, createIssue(RESOLUTION_FALSE_POSITIVE, STATUS_RESOLVED, MAJOR));
191     underTest.onIssue(FILE1, createIssue(RESOLUTION_WONT_FIX, STATUS_CLOSED, 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.onIssue(FILE2, createIssue(RESOLUTION_WONT_FIX, STATUS_CLOSED, MAJOR));
198     underTest.afterComponent(FILE2);
199
200     underTest.beforeComponent(FILE3);
201     underTest.afterComponent(FILE3);
202
203     underTest.beforeComponent(PROJECT);
204     underTest.afterComponent(PROJECT);
205
206     assertThat(measureRepository.getRawMeasure(FILE1, ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
207     assertThat(measureRepository.getRawMeasure(FILE1, FALSE_POSITIVE_ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
208     assertThat(measureRepository.getRawMeasure(FILE1, WONT_FIX_ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
209
210     assertThat(measureRepository.getRawMeasure(FILE2, ISSUES_METRIC).get().getIntValue()).isEqualTo(2);
211     assertThat(measureRepository.getRawMeasure(FILE2, FALSE_POSITIVE_ISSUES_METRIC).get().getIntValue()).isEqualTo(0);
212     assertThat(measureRepository.getRawMeasure(FILE2, WONT_FIX_ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
213
214     assertThat(measureRepository.getRawMeasure(FILE3, ISSUES_METRIC).get().getIntValue()).isEqualTo(0);
215
216     assertThat(measureRepository.getRawMeasure(PROJECT, FALSE_POSITIVE_ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
217     assertThat(measureRepository.getRawMeasure(PROJECT, WONT_FIX_ISSUES_METRIC).get().getIntValue()).isEqualTo(2);
218   }
219
220   @Test
221   public void count_unresolved_issues_by_severity() {
222     periodsHolder.setPeriod(null);
223
224     // bottom-up traversal -> from files to project
225     underTest.beforeComponent(FILE1);
226     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER));
227     // this resolved issue is ignored
228     underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR));
229     underTest.afterComponent(FILE1);
230
231     underTest.beforeComponent(FILE2);
232     underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, BLOCKER));
233     underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, MAJOR));
234     underTest.afterComponent(FILE2);
235
236     underTest.beforeComponent(PROJECT);
237     underTest.afterComponent(PROJECT);
238
239     assertThat(measureRepository.getRawMeasure(FILE1, BLOCKER_ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
240     assertThat(measureRepository.getRawMeasure(FILE1, CRITICAL_ISSUES_METRIC).get().getIntValue()).isEqualTo(0);
241     assertThat(measureRepository.getRawMeasure(FILE1, MAJOR_ISSUES_METRIC).get().getIntValue()).isEqualTo(0);
242
243     assertThat(measureRepository.getRawMeasure(FILE2, BLOCKER_ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
244     assertThat(measureRepository.getRawMeasure(FILE2, CRITICAL_ISSUES_METRIC).get().getIntValue()).isEqualTo(0);
245     assertThat(measureRepository.getRawMeasure(FILE2, MAJOR_ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
246
247     assertThat(measureRepository.getRawMeasure(PROJECT, BLOCKER_ISSUES_METRIC).get().getIntValue()).isEqualTo(2);
248     assertThat(measureRepository.getRawMeasure(PROJECT, CRITICAL_ISSUES_METRIC).get().getIntValue()).isEqualTo(0);
249     assertThat(measureRepository.getRawMeasure(PROJECT, MAJOR_ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
250   }
251
252   @Test
253   public void count_unresolved_issues_by_type() {
254     periodsHolder.setPeriod(null);
255
256     // bottom-up traversal -> from files to project
257     // file1 : one open code smell, one closed code smell (which will be excluded from metric)
258     underTest.beforeComponent(FILE1);
259     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER).setType(RuleType.CODE_SMELL));
260     underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR).setType(RuleType.CODE_SMELL));
261     underTest.afterComponent(FILE1);
262
263     // file2 : one bug
264     underTest.beforeComponent(FILE2);
265     underTest.onIssue(FILE2, createIssue(null, STATUS_CONFIRMED, BLOCKER).setType(RuleType.BUG));
266     underTest.afterComponent(FILE2);
267
268     underTest.beforeComponent(PROJECT);
269     underTest.afterComponent(PROJECT);
270
271     assertThat(measureRepository.getRawMeasure(FILE1, CODE_SMELLS_METRIC).get().getIntValue()).isEqualTo(1);
272     assertThat(measureRepository.getRawMeasure(FILE1, BUGS_METRIC).get().getIntValue()).isEqualTo(0);
273     assertThat(measureRepository.getRawMeasure(FILE1, VULNERABILITIES_METRIC).get().getIntValue()).isEqualTo(0);
274
275     assertThat(measureRepository.getRawMeasure(FILE2, CODE_SMELLS_METRIC).get().getIntValue()).isEqualTo(0);
276     assertThat(measureRepository.getRawMeasure(FILE2, BUGS_METRIC).get().getIntValue()).isEqualTo(1);
277     assertThat(measureRepository.getRawMeasure(FILE2, VULNERABILITIES_METRIC).get().getIntValue()).isEqualTo(0);
278
279     assertThat(measureRepository.getRawMeasure(PROJECT, CODE_SMELLS_METRIC).get().getIntValue()).isEqualTo(1);
280     assertThat(measureRepository.getRawMeasure(PROJECT, BUGS_METRIC).get().getIntValue()).isEqualTo(1);
281     assertThat(measureRepository.getRawMeasure(PROJECT, VULNERABILITIES_METRIC).get().getIntValue()).isEqualTo(0);
282   }
283
284   @Test
285   public void count_new_issues() {
286     Period period = newPeriod(1500000000000L);
287     periodsHolder.setPeriod(period);
288
289     underTest.beforeComponent(FILE1);
290     // created before -> existing issues (so ignored)
291     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER, period.getSnapshotDate() - 1000000L).setType(RuleType.CODE_SMELL));
292     // created during the first analysis starting the period -> existing issues (so ignored)
293     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, BLOCKER, period.getSnapshotDate()).setType(RuleType.BUG));
294     // created after -> 3 new issues but 1 is closed
295     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, CRITICAL, period.getSnapshotDate() + 100000L).setType(RuleType.CODE_SMELL));
296     underTest.onIssue(FILE1, createIssue(null, STATUS_OPEN, CRITICAL, period.getSnapshotDate() + 100000L).setType(RuleType.BUG));
297     underTest.onIssue(FILE1, createIssue(RESOLUTION_FIXED, STATUS_CLOSED, MAJOR, period.getSnapshotDate() + 200000L).setType(RuleType.BUG));
298     underTest.afterComponent(FILE1);
299
300     underTest.beforeComponent(FILE2);
301     underTest.afterComponent(FILE2);
302
303     underTest.beforeComponent(PROJECT);
304     underTest.afterComponent(PROJECT);
305
306     assertVariation(FILE1, NEW_ISSUES_METRIC, 2);
307     assertVariation(FILE1, NEW_CRITICAL_ISSUES_METRIC, 2);
308     assertVariation(FILE1, NEW_BLOCKER_ISSUES_METRIC, 0);
309     assertVariation(FILE1, NEW_MAJOR_ISSUES_METRIC, 0);
310     assertVariation(FILE1, NEW_CODE_SMELLS_METRIC, 1);
311     assertVariation(FILE1, NEW_BUGS_METRIC, 1);
312     assertVariation(FILE1, NEW_VULNERABILITIES_METRIC, 0);
313
314     assertVariation(PROJECT, NEW_ISSUES_METRIC, 2);
315     assertVariation(PROJECT, NEW_CRITICAL_ISSUES_METRIC, 2);
316     assertVariation(PROJECT, NEW_BLOCKER_ISSUES_METRIC, 0);
317     assertVariation(PROJECT, NEW_MAJOR_ISSUES_METRIC, 0);
318     assertVariation(PROJECT, NEW_CODE_SMELLS_METRIC, 1);
319     assertVariation(PROJECT, NEW_BUGS_METRIC, 1);
320     assertVariation(PROJECT, NEW_VULNERABILITIES_METRIC, 0);
321   }
322
323   @Test
324   public void exclude_hotspots_from_issue_counts() {
325     periodsHolder.setPeriod(null);
326
327     // bottom-up traversal -> from files to project
328     underTest.beforeComponent(FILE1);
329     underTest.onIssue(FILE1, createSecurityHotspot());
330     underTest.onIssue(FILE1, createSecurityHotspot());
331     underTest.afterComponent(FILE1);
332
333     underTest.beforeComponent(FILE2);
334     underTest.onIssue(FILE1, createSecurityHotspot());
335     underTest.afterComponent(FILE2);
336
337     underTest.beforeComponent(FILE3);
338     underTest.afterComponent(FILE3);
339
340     underTest.beforeComponent(PROJECT);
341     underTest.afterComponent(PROJECT);
342
343     assertThat(measureRepository.getRawMeasure(FILE1, ISSUES_METRIC).get().getIntValue()).isEqualTo(2);
344     assertThat(measureRepository.getRawMeasure(FILE1, OPEN_ISSUES_METRIC).get().getIntValue()).isEqualTo(2);
345     assertThat(measureRepository.getRawMeasure(FILE1, CONFIRMED_ISSUES_METRIC).get().getIntValue()).isEqualTo(0);
346
347     assertThat(measureRepository.getRawMeasure(FILE2, ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
348     assertThat(measureRepository.getRawMeasure(FILE2, OPEN_ISSUES_METRIC).get().getIntValue()).isEqualTo(1);
349     assertThat(measureRepository.getRawMeasure(FILE2, CONFIRMED_ISSUES_METRIC).get().getIntValue()).isEqualTo(0);
350
351     assertThat(measureRepository.getRawMeasure(FILE3, ISSUES_METRIC).get().getIntValue()).isEqualTo(0);
352
353     assertThat(measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC).get().getIntValue()).isEqualTo(3);
354     assertThat(measureRepository.getRawMeasure(PROJECT, OPEN_ISSUES_METRIC).get().getIntValue()).isEqualTo(3);
355     assertThat(measureRepository.getRawMeasure(PROJECT, CONFIRMED_ISSUES_METRIC).get().getIntValue()).isEqualTo(0);
356   }
357
358   @Test
359   public void exclude_new_hotspots_from_issue_counts() {
360     Period period = newPeriod(1500000000000L);
361     periodsHolder.setPeriod(period);
362
363     underTest.beforeComponent(FILE1);
364     // created before -> existing issues (so ignored)
365     underTest.onIssue(FILE1, createSecurityHotspot(period.getSnapshotDate() - 1000000L));
366     // created during the first analysis starting the period -> existing issues (so ignored)
367     underTest.onIssue(FILE1, createSecurityHotspot(period.getSnapshotDate()));
368
369     // created after, but closed
370     underTest.onIssue(FILE1, createSecurityHotspot(period.getSnapshotDate() + 100000L).setStatus(STATUS_RESOLVED).setResolution(RESOLUTION_WONT_FIX));
371
372     for (String severity : Arrays.asList(CRITICAL, BLOCKER, MAJOR)) {
373       DefaultIssue issue = createSecurityHotspot(period.getSnapshotDate() + 100000L);
374       issue.setSeverity(severity);
375       underTest.onIssue(FILE1, issue);
376     }
377     underTest.afterComponent(FILE1);
378
379     underTest.beforeComponent(FILE2);
380     underTest.afterComponent(FILE2);
381
382     underTest.beforeComponent(PROJECT);
383     underTest.afterComponent(PROJECT);
384
385     assertVariation(FILE1, NEW_ISSUES_METRIC, 0);
386     assertVariation(FILE1, NEW_CRITICAL_ISSUES_METRIC, 0);
387     assertVariation(FILE1, NEW_BLOCKER_ISSUES_METRIC, 0);
388     assertVariation(FILE1, NEW_MAJOR_ISSUES_METRIC, 0);
389     assertVariation(FILE1, NEW_VULNERABILITIES_METRIC, 0);
390
391     assertVariation(PROJECT, NEW_ISSUES_METRIC, 0);
392     assertVariation(PROJECT, NEW_CRITICAL_ISSUES_METRIC, 0);
393     assertVariation(PROJECT, NEW_BLOCKER_ISSUES_METRIC, 0);
394     assertVariation(PROJECT, NEW_MAJOR_ISSUES_METRIC, 0);
395     assertVariation(PROJECT, NEW_VULNERABILITIES_METRIC, 0);
396   }
397
398   private void assertVariation(Component component, Metric metric, int expectedVariation) {
399     Measure measure = measureRepository.getRawMeasure(component, metric).get();
400     assertThat(measure.getVariation()).isEqualTo((double) expectedVariation, Offset.offset(0.01));
401   }
402
403   private static DefaultIssue createIssue(@Nullable String resolution, String status, String severity) {
404     return createIssue(resolution, status, severity, RuleType.CODE_SMELL, new Date().getTime());
405   }
406
407   private static DefaultIssue createIssue(@Nullable String resolution, String status, String severity, long creationDate) {
408     return createIssue(resolution, status, severity, RuleType.CODE_SMELL, creationDate);
409   }
410
411   private static DefaultIssue createIssue(@Nullable String resolution, String status, String severity, RuleType ruleType, long creationDate) {
412     return new DefaultIssue()
413       .setResolution(resolution).setStatus(status)
414       .setSeverity(severity).setRuleKey(RuleTesting.XOO_X1)
415       .setType(ruleType)
416       .setCreationDate(new Date(creationDate));
417   }
418
419   private static DefaultIssue createSecurityHotspot() {
420     return createSecurityHotspot(new Date().getTime());
421   }
422
423   private static DefaultIssue createSecurityHotspot(long creationDate) {
424     return createIssue(null, STATUS_OPEN, "MAJOR", RuleType.SECURITY_HOTSPOT, creationDate);
425   }
426
427   private static Period newPeriod(long date) {
428     return new Period("mode", null, date, "U1");
429   }
430
431 }