]> source.dussan.org Git - sonarqube.git/blob
ba166a5ff04119968cada7bcae23de64dc107b9f
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.server.issue.notification;
21
22 import java.util.Date;
23 import java.util.Optional;
24 import java.util.stream.IntStream;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.utils.DateUtils;
28 import org.sonar.api.utils.Duration;
29 import org.sonar.api.utils.Durations;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.issue.IssueDto;
33 import org.sonar.db.rule.RuleDefinitionDto;
34 import org.sonar.db.user.UserDto;
35 import org.sonar.server.issue.notification.NewIssuesNotification.DetailsSupplier;
36 import org.sonar.server.issue.notification.NewIssuesNotification.RuleDefinition;
37
38 import static java.util.Arrays.asList;
39 import static java.util.Collections.singletonList;
40 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
41 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45 import static org.sonar.api.rules.RuleType.BUG;
46 import static org.sonar.api.rules.RuleType.CODE_SMELL;
47 import static org.sonar.db.component.ComponentTesting.newDirectory;
48 import static org.sonar.db.component.ComponentTesting.newFileDto;
49 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.ASSIGNEE;
50 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.COMPONENT;
51 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.EFFORT;
52 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.RULE;
53 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.RULE_TYPE;
54 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.TAG;
55
56 public class NewIssuesNotificationTest {
57
58   @Rule
59   public DbTester db = DbTester.create();
60
61   private DetailsSupplier detailsSupplier = mock(DetailsSupplier.class);
62   private NewIssuesNotification underTest = new NewIssuesNotification(new Durations(), detailsSupplier);
63
64   @Test
65   public void set_project_without_branch() {
66     underTest.setProject("project-key", "project-long-name", null, null);
67
68     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_NAME)).isEqualTo("project-long-name");
69     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_KEY)).isEqualTo("project-key");
70     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_BRANCH)).isNull();
71   }
72
73   @Test
74   public void set_project_with_branch() {
75     underTest.setProject("project-key", "project-long-name", "feature", null);
76
77     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_NAME)).isEqualTo("project-long-name");
78     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_KEY)).isEqualTo("project-key");
79     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_BRANCH)).isEqualTo("feature");
80   }
81
82   @Test
83   public void set_project_with_pull_request() {
84     underTest.setProject("project-key", "project-long-name", null, "pr-123");
85
86     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_NAME)).isEqualTo("project-long-name");
87     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_KEY)).isEqualTo("project-key");
88     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PULL_REQUEST)).isEqualTo("pr-123");
89   }
90
91   @Test
92   public void set_project_version() {
93     String version = randomAlphanumeric(5);
94
95     underTest.setProjectVersion(version);
96
97     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_VERSION)).isEqualTo(version);
98   }
99
100   @Test
101   public void set_project_version_supports_null() {
102     underTest.setProjectVersion(null);
103
104     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_VERSION)).isNull();
105   }
106
107   @Test
108   public void getProjectKey_returns_null_if_setProject_has_no_been_called() {
109     assertThat(underTest.getProjectKey()).isNull();
110   }
111
112   @Test
113   public void getProjectKey_returns_projectKey_if_setProject_has_been_called() {
114     String projectKey = randomAlphabetic(5);
115     String projectName = randomAlphabetic(6);
116     String branchName = randomAlphabetic(7);
117     String pullRequest = randomAlphabetic(8);
118     underTest.setProject(projectKey, projectName, branchName, pullRequest);
119
120     assertThat(underTest.getProjectKey()).isEqualTo(projectKey);
121   }
122
123   @Test
124   public void getProjectKey_returns_value_of_field_projectKey() {
125     String projectKey = randomAlphabetic(5);
126     underTest.setFieldValue("projectKey", projectKey);
127
128     assertThat(underTest.getProjectKey()).isEqualTo(projectKey);
129   }
130
131   @Test
132   public void set_date() {
133     Date date = new Date();
134
135     underTest.setAnalysisDate(date);
136
137     assertThat(underTest.getFieldValue(NewIssuesEmailTemplate.FIELD_PROJECT_DATE)).isEqualTo(DateUtils.formatDateTime(date));
138   }
139
140   @Test
141   public void set_statistics() {
142     UserDto maynard = db.users().insertUser(u -> u.setLogin("maynard"));
143     UserDto keenan = db.users().insertUser(u -> u.setLogin("keenan"));
144     ComponentDto project = db.components().insertPrivateProject();
145     ComponentDto directory = db.components().insertComponent(newDirectory(project, "path"));
146     ComponentDto file = db.components().insertComponent(newFileDto(directory));
147     RuleDefinitionDto rule1 = db.rules().insert(r -> r.setRepositoryKey("SonarQube").setRuleKey("rule1-the-world").setName("Rule the World").setLanguage("Java"));
148     RuleDefinitionDto rule2 = db.rules().insert(r -> r.setRepositoryKey("SonarQube").setRuleKey("rule1-the-universe").setName("Rule the Universe").setLanguage("Clojure"));
149     IssueDto issue1 = db.issues().insert(rule1, project, file, i -> i.setType(BUG).setAssigneeUuid(maynard.getUuid()).setTags(asList("bug", "owasp")));
150     IssueDto issue2 = db.issues().insert(rule2, project, directory, i -> i.setType(CODE_SMELL).setAssigneeUuid(keenan.getUuid()).setTags(singletonList("owasp")));
151
152     NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats(i -> true);
153     IntStream.rangeClosed(1, 5).forEach(i -> stats.add(issue1.toDefaultIssue()));
154     IntStream.rangeClosed(1, 3).forEach(i -> stats.add(issue2.toDefaultIssue()));
155     mockDetailsSupplierComponents(project, directory, file);
156     mockDetailsSupplierRules(rule1, rule2);
157     mockDetailsSupplierAssignees(maynard, keenan);
158
159     underTest.setStatistics(project.longName(), stats);
160
161     assertThat(underTest.getFieldValue(RULE_TYPE + ".BUG.count")).isEqualTo("5");
162     assertThat(underTest.getFieldValue(RULE_TYPE + ".CODE_SMELL.count")).isEqualTo("3");
163     assertThat(underTest.getFieldValue(ASSIGNEE + ".1.label")).isEqualTo(maynard.getName());
164     assertThat(underTest.getFieldValue(ASSIGNEE + ".1.count")).isEqualTo("5");
165     assertThat(underTest.getFieldValue(ASSIGNEE + ".2.label")).isEqualTo(keenan.getName());
166     assertThat(underTest.getFieldValue(ASSIGNEE + ".2.count")).isEqualTo("3");
167     assertThat(underTest.getFieldValue(TAG + ".1.label")).isEqualTo("owasp");
168     assertThat(underTest.getFieldValue(TAG + ".1.count")).isEqualTo("8");
169     assertThat(underTest.getFieldValue(TAG + ".2.label")).isEqualTo("bug");
170     assertThat(underTest.getFieldValue(TAG + ".2.count")).isEqualTo("5");
171     assertThat(underTest.getFieldValue(COMPONENT + ".1.label")).isEqualTo(file.name());
172     assertThat(underTest.getFieldValue(COMPONENT + ".1.count")).isEqualTo("5");
173     assertThat(underTest.getFieldValue(COMPONENT + ".2.label")).isEqualTo(directory.name());
174     assertThat(underTest.getFieldValue(COMPONENT + ".2.count")).isEqualTo("3");
175     assertThat(underTest.getFieldValue(RULE + ".1.label")).isEqualTo("Rule the World (Java)");
176     assertThat(underTest.getFieldValue(RULE + ".1.count")).isEqualTo("5");
177     assertThat(underTest.getFieldValue(RULE + ".2.label")).isEqualTo("Rule the Universe (Clojure)");
178     assertThat(underTest.getFieldValue(RULE + ".2.count")).isEqualTo("3");
179     assertThat(underTest.getDefaultMessage()).startsWith("8 new issues on " + project.longName());
180   }
181
182   @Test
183   public void set_statistics_when_no_issues_created_on_current_analysis() {
184     UserDto maynard = db.users().insertUser(u -> u.setLogin("maynard"));
185     UserDto keenan = db.users().insertUser(u -> u.setLogin("keenan"));
186     ComponentDto project = db.components().insertPrivateProject();
187     ComponentDto directory = db.components().insertComponent(newDirectory(project, "path"));
188     ComponentDto file = db.components().insertComponent(newFileDto(directory));
189     RuleDefinitionDto rule1 = db.rules().insert(r -> r.setRepositoryKey("SonarQube").setRuleKey("rule1-the-world").setName("Rule the World").setLanguage("Java"));
190     RuleDefinitionDto rule2 = db.rules().insert(r -> r.setRepositoryKey("SonarQube").setRuleKey("rule1-the-universe").setName("Rule the Universe").setLanguage("Clojure"));
191     IssueDto issue1 = db.issues().insert(rule1, project, file, i -> i.setType(BUG).setAssigneeUuid(maynard.getUuid()).setTags(asList("bug", "owasp")));
192     IssueDto issue2 = db.issues().insert(rule2, project, directory, i -> i.setType(CODE_SMELL).setAssigneeUuid(keenan.getUuid()).setTags(singletonList("owasp")));
193
194     NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats(i -> false);
195     IntStream.rangeClosed(1, 5).forEach(i -> stats.add(issue1.toDefaultIssue()));
196     IntStream.rangeClosed(1, 3).forEach(i -> stats.add(issue2.toDefaultIssue()));
197     mockDetailsSupplierComponents(project, directory, file);
198     mockDetailsSupplierRules(rule1, rule2);
199     mockDetailsSupplierAssignees(maynard, keenan);
200
201     underTest.setStatistics(project.longName(), stats);
202
203     assertThat(underTest.getFieldValue(RULE_TYPE + ".BUG.count")).isEqualTo("0");
204     assertThat(underTest.getFieldValue(RULE_TYPE + ".CODE_SMELL.count")).isEqualTo("0");
205     assertThat(underTest.getFieldValue(ASSIGNEE + ".1.label")).isNull();
206     assertThat(underTest.getFieldValue(ASSIGNEE + ".1.count")).isNull();
207     assertThat(underTest.getFieldValue(ASSIGNEE + ".2.label")).isNull();
208     assertThat(underTest.getFieldValue(ASSIGNEE + ".2.count")).isNull();
209     assertThat(underTest.getFieldValue(TAG + ".1.label")).isNull();
210     assertThat(underTest.getFieldValue(TAG + ".1.count")).isNull();
211     assertThat(underTest.getFieldValue(TAG + ".2.label")).isNull();
212     assertThat(underTest.getFieldValue(TAG + ".2.count")).isNull();
213     assertThat(underTest.getFieldValue(COMPONENT + ".1.label")).isNull();
214     assertThat(underTest.getFieldValue(COMPONENT + ".1.count")).isNull();
215     assertThat(underTest.getFieldValue(COMPONENT + ".2.label")).isNull();
216     assertThat(underTest.getFieldValue(COMPONENT + ".2.count")).isNull();
217     assertThat(underTest.getFieldValue(RULE + ".1.label")).isNull();
218     assertThat(underTest.getFieldValue(RULE + ".1.count")).isNull();
219     assertThat(underTest.getFieldValue(RULE + ".2.label")).isNull();
220     assertThat(underTest.getFieldValue(RULE + ".2.count")).isNull();
221     assertThat(underTest.getDefaultMessage()).startsWith("0 new issues on " + project.longName());
222   }
223
224   @Test
225   public void set_statistics_when_some_issues_are_no_created_on_current_analysis() {
226
227     UserDto maynard = db.users().insertUser(u -> u.setLogin("maynard"));
228     UserDto keenan = db.users().insertUser(u -> u.setLogin("keenan"));
229     ComponentDto project = db.components().insertPrivateProject();
230     ComponentDto directory = db.components().insertComponent(newDirectory(project, "path"));
231     ComponentDto file = db.components().insertComponent(newFileDto(directory));
232     RuleDefinitionDto rule1 = db.rules().insert(r -> r.setRepositoryKey("SonarQube").setRuleKey("rule1-the-world").setName("Rule the World").setLanguage("Java"));
233     RuleDefinitionDto rule2 = db.rules().insert(r -> r.setRepositoryKey("SonarQube").setRuleKey("rule1-the-universe").setName("Rule the Universe").setLanguage("Clojure"));
234     IssueDto issue1 = db.issues().insert(rule1, project, file, i -> i.setType(BUG).setAssigneeUuid(maynard.getUuid()).setTags(asList("bug", "owasp")));
235     IssueDto issue2 = db.issues().insert(rule2, project, directory, i -> i.setType(CODE_SMELL).setAssigneeUuid(keenan.getUuid()).setTags(singletonList("owasp")));
236
237     NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats(i -> i.key().equals(issue2.getKey()));
238     IntStream.rangeClosed(1, 5).forEach(i -> stats.add(issue1.toDefaultIssue()));
239     IntStream.rangeClosed(1, 3).forEach(i -> stats.add(issue2.toDefaultIssue()));
240     mockDetailsSupplierComponents(project, directory, file);
241     mockDetailsSupplierRules(rule1, rule2);
242     mockDetailsSupplierAssignees(maynard, keenan);
243
244     underTest.setStatistics(project.longName(), stats);
245
246     assertThat(underTest.getFieldValue(RULE_TYPE + ".BUG.count")).isEqualTo("0");
247     assertThat(underTest.getFieldValue(RULE_TYPE + ".CODE_SMELL.count")).isEqualTo("3");
248     assertThat(underTest.getFieldValue(ASSIGNEE + ".1.label")).isEqualTo(keenan.getName());
249     assertThat(underTest.getFieldValue(ASSIGNEE + ".1.count")).isEqualTo("3");
250     assertThat(underTest.getFieldValue(ASSIGNEE + ".2.label")).isNull();
251     assertThat(underTest.getFieldValue(ASSIGNEE + ".2.count")).isNull();
252     assertThat(underTest.getFieldValue(TAG + ".1.label")).isEqualTo("owasp");
253     assertThat(underTest.getFieldValue(TAG + ".1.count")).isEqualTo("3");
254     assertThat(underTest.getFieldValue(TAG + ".2.label")).isNull();
255     assertThat(underTest.getFieldValue(TAG + ".2.count")).isNull();
256     assertThat(underTest.getFieldValue(COMPONENT + ".1.label")).isEqualTo(directory.name());
257     assertThat(underTest.getFieldValue(COMPONENT + ".1.count")).isEqualTo("3");
258     assertThat(underTest.getFieldValue(COMPONENT + ".2.label")).isNull();
259     assertThat(underTest.getFieldValue(COMPONENT + ".2.count")).isNull();
260     assertThat(underTest.getFieldValue(RULE + ".1.label")).isEqualTo("Rule the Universe (Clojure)");
261     assertThat(underTest.getFieldValue(RULE + ".1.count")).isEqualTo("3");
262     assertThat(underTest.getFieldValue(RULE + ".2.label")).isNull();
263     assertThat(underTest.getFieldValue(RULE + ".2.count")).isNull();
264     assertThat(underTest.getDefaultMessage()).startsWith("3 new issues on " + project.longName());
265   }
266
267   private void mockDetailsSupplierAssignees(UserDto... users) {
268     for (UserDto user : users) {
269       when(detailsSupplier.getUserNameByUuid(user.getUuid())).thenReturn(Optional.of(user.getName()));
270     }
271   }
272
273   private void mockDetailsSupplierRules(RuleDefinitionDto... rules) {
274     for (RuleDefinitionDto rule : rules) {
275       when(detailsSupplier.getRuleDefinitionByRuleKey(rule.getKey()))
276         .thenReturn(Optional.of(new RuleDefinition(rule.getName(), rule.getLanguage())));
277     }
278   }
279
280   private void mockDetailsSupplierComponents(ComponentDto... components) {
281     for (ComponentDto component : components) {
282       when(detailsSupplier.getComponentNameByUuid(component.uuid())).thenReturn(Optional.of(component.name()));
283     }
284   }
285
286   @Test
287   public void set_assignee() {
288     ComponentDto project = db.components().insertPrivateProject();
289     ComponentDto file = db.components().insertComponent(newFileDto(project));
290     RuleDefinitionDto rule = db.rules().insert();
291     UserDto user = db.users().insertUser();
292     IssueDto issue = db.issues().insert(rule, project, file, i -> i.setAssigneeUuid(user.getUuid()));
293     NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats(i -> true);
294     IntStream.rangeClosed(1, 5).forEach(i -> stats.add(issue.toDefaultIssue()));
295     mockDetailsSupplierRules(rule);
296     mockDetailsSupplierAssignees(user);
297     mockDetailsSupplierComponents(project, file);
298
299     underTest.setStatistics(project.longName(), stats);
300
301     assertThat(underTest.getFieldValue(ASSIGNEE + ".1.label")).isEqualTo(user.getName());
302     assertThat(underTest.getFieldValue(ASSIGNEE + ".1.count")).isEqualTo("5");
303   }
304
305   @Test
306   public void add_only_5_assignees_with_biggest_issue_counts() {
307     UserDto user1 = db.users().insertUser();
308     UserDto user2 = db.users().insertUser();
309     UserDto user3 = db.users().insertUser();
310     UserDto user4 = db.users().insertUser();
311     UserDto user5 = db.users().insertUser();
312     UserDto user6 = db.users().insertUser();
313     UserDto user7 = db.users().insertUser();
314     UserDto user8 = db.users().insertUser();
315     ComponentDto project = db.components().insertPrivateProject();
316     ComponentDto file = db.components().insertComponent(newFileDto(project));
317     RuleDefinitionDto rule = db.rules().insert();
318     NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats(i -> true);
319     IntStream.rangeClosed(1, 10).forEach(i -> stats.add(db.issues().insert(rule, project, file, issue -> issue.setAssigneeUuid(user1.getUuid())).toDefaultIssue()));
320     IntStream.rangeClosed(1, 9).forEach(i -> stats.add(db.issues().insert(rule, project, file, issue -> issue.setAssigneeUuid(user2.getUuid())).toDefaultIssue()));
321     IntStream.rangeClosed(1, 8).forEach(i -> stats.add(db.issues().insert(rule, project, file, issue -> issue.setAssigneeUuid(user3.getUuid())).toDefaultIssue()));
322     IntStream.rangeClosed(1, 7).forEach(i -> stats.add(db.issues().insert(rule, project, file, issue -> issue.setAssigneeUuid(user4.getUuid())).toDefaultIssue()));
323     IntStream.rangeClosed(1, 6).forEach(i -> stats.add(db.issues().insert(rule, project, file, issue -> issue.setAssigneeUuid(user5.getUuid())).toDefaultIssue()));
324     IntStream.rangeClosed(1, 5).forEach(i -> stats.add(db.issues().insert(rule, project, file, issue -> issue.setAssigneeUuid(user6.getUuid())).toDefaultIssue()));
325     IntStream.rangeClosed(1, 4).forEach(i -> stats.add(db.issues().insert(rule, project, file, issue -> issue.setAssigneeUuid(user7.getUuid())).toDefaultIssue()));
326     IntStream.rangeClosed(1, 3).forEach(i -> stats.add(db.issues().insert(rule, project, file, issue -> issue.setAssigneeUuid(user8.getUuid())).toDefaultIssue()));
327     mockDetailsSupplierAssignees(user1, user2, user3, user4, user5, user6, user7, user8);
328     mockDetailsSupplierComponents(project, file);
329     mockDetailsSupplierRules(rule);
330
331     underTest.setStatistics(project.longName(), stats);
332
333     assertThat(underTest.getFieldValue(ASSIGNEE + ".1.label")).isEqualTo(user1.getName());
334     assertThat(underTest.getFieldValue(ASSIGNEE + ".1.count")).isEqualTo("10");
335     assertThat(underTest.getFieldValue(ASSIGNEE + ".2.label")).isEqualTo(user2.getName());
336     assertThat(underTest.getFieldValue(ASSIGNEE + ".2.count")).isEqualTo("9");
337     assertThat(underTest.getFieldValue(ASSIGNEE + ".3.label")).isEqualTo(user3.getName());
338     assertThat(underTest.getFieldValue(ASSIGNEE + ".3.count")).isEqualTo("8");
339     assertThat(underTest.getFieldValue(ASSIGNEE + ".4.label")).isEqualTo(user4.getName());
340     assertThat(underTest.getFieldValue(ASSIGNEE + ".4.count")).isEqualTo("7");
341     assertThat(underTest.getFieldValue(ASSIGNEE + ".5.label")).isEqualTo(user5.getName());
342     assertThat(underTest.getFieldValue(ASSIGNEE + ".5.count")).isEqualTo("6");
343     assertThat(underTest.getFieldValue(ASSIGNEE + ".6.label")).isNull();
344     assertThat(underTest.getFieldValue(ASSIGNEE + ".6.count")).isNull();
345   }
346
347   @Test
348   public void add_only_5_components_with_biggest_issue_counts() {
349     ComponentDto project = db.components().insertPrivateProject();
350     RuleDefinitionDto rule = db.rules().insert();
351     NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats(i -> true);
352     ComponentDto file1 = db.components().insertComponent(newFileDto(project));
353     IntStream.rangeClosed(1, 10).forEach(i -> stats.add(db.issues().insert(rule, project, file1).toDefaultIssue()));
354     ComponentDto file2 = db.components().insertComponent(newFileDto(project));
355     IntStream.rangeClosed(1, 9).forEach(i -> stats.add(db.issues().insert(rule, project, file2).toDefaultIssue()));
356     ComponentDto file3 = db.components().insertComponent(newFileDto(project));
357     IntStream.rangeClosed(1, 8).forEach(i -> stats.add(db.issues().insert(rule, project, file3).toDefaultIssue()));
358     ComponentDto file4 = db.components().insertComponent(newFileDto(project));
359     IntStream.rangeClosed(1, 7).forEach(i -> stats.add(db.issues().insert(rule, project, file4).toDefaultIssue()));
360     ComponentDto file5 = db.components().insertComponent(newFileDto(project));
361     IntStream.rangeClosed(1, 6).forEach(i -> stats.add(db.issues().insert(rule, project, file5).toDefaultIssue()));
362     ComponentDto file6 = db.components().insertComponent(newFileDto(project));
363     IntStream.rangeClosed(1, 5).forEach(i -> stats.add(db.issues().insert(rule, project, file6).toDefaultIssue()));
364     ComponentDto file7 = db.components().insertComponent(newFileDto(project));
365     IntStream.rangeClosed(1, 4).forEach(i -> stats.add(db.issues().insert(rule, project, file7).toDefaultIssue()));
366     ComponentDto file8 = db.components().insertComponent(newFileDto(project));
367     IntStream.rangeClosed(1, 3).forEach(i -> stats.add(db.issues().insert(rule, project, file8).toDefaultIssue()));
368     mockDetailsSupplierComponents(project, file1, file2, file3, file4, file5, file6, file7, file8);
369     mockDetailsSupplierRules(rule);
370
371     underTest.setStatistics(project.longName(), stats);
372
373     assertThat(underTest.getFieldValue(COMPONENT + ".1.label")).isEqualTo(file1.name());
374     assertThat(underTest.getFieldValue(COMPONENT + ".1.count")).isEqualTo("10");
375     assertThat(underTest.getFieldValue(COMPONENT + ".2.label")).isEqualTo(file2.name());
376     assertThat(underTest.getFieldValue(COMPONENT + ".2.count")).isEqualTo("9");
377     assertThat(underTest.getFieldValue(COMPONENT + ".3.label")).isEqualTo(file3.name());
378     assertThat(underTest.getFieldValue(COMPONENT + ".3.count")).isEqualTo("8");
379     assertThat(underTest.getFieldValue(COMPONENT + ".4.label")).isEqualTo(file4.name());
380     assertThat(underTest.getFieldValue(COMPONENT + ".4.count")).isEqualTo("7");
381     assertThat(underTest.getFieldValue(COMPONENT + ".5.label")).isEqualTo(file5.name());
382     assertThat(underTest.getFieldValue(COMPONENT + ".5.count")).isEqualTo("6");
383     assertThat(underTest.getFieldValue(COMPONENT + ".6.label")).isNull();
384     assertThat(underTest.getFieldValue(COMPONENT + ".6.count")).isNull();
385   }
386
387   @Test
388   public void add_only_5_rules_with_biggest_issue_counts() {
389     ComponentDto project = db.components().insertPrivateProject();
390     ComponentDto file = db.components().insertComponent(newFileDto(project));
391     NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats(i -> true);
392     RuleDefinitionDto rule1 = db.rules().insert(r -> r.setLanguage("Java"));
393     IntStream.rangeClosed(1, 10).forEach(i -> stats.add(db.issues().insert(rule1, project, file).toDefaultIssue()));
394     RuleDefinitionDto rule2 = db.rules().insert(r -> r.setLanguage("Java"));
395     IntStream.rangeClosed(1, 9).forEach(i -> stats.add(db.issues().insert(rule2, project, file).toDefaultIssue()));
396     RuleDefinitionDto rule3 = db.rules().insert(r -> r.setLanguage("Java"));
397     IntStream.rangeClosed(1, 8).forEach(i -> stats.add(db.issues().insert(rule3, project, file).toDefaultIssue()));
398     RuleDefinitionDto rule4 = db.rules().insert(r -> r.setLanguage("Java"));
399     IntStream.rangeClosed(1, 7).forEach(i -> stats.add(db.issues().insert(rule4, project, file).toDefaultIssue()));
400     RuleDefinitionDto rule5 = db.rules().insert(r -> r.setLanguage("Java"));
401     IntStream.rangeClosed(1, 6).forEach(i -> stats.add(db.issues().insert(rule5, project, file).toDefaultIssue()));
402     RuleDefinitionDto rule6 = db.rules().insert(r -> r.setLanguage("Java"));
403     IntStream.rangeClosed(1, 5).forEach(i -> stats.add(db.issues().insert(rule6, project, file).toDefaultIssue()));
404     RuleDefinitionDto rule7 = db.rules().insert(r -> r.setLanguage("Java"));
405     IntStream.rangeClosed(1, 4).forEach(i -> stats.add(db.issues().insert(rule7, project, file).toDefaultIssue()));
406     RuleDefinitionDto rule8 = db.rules().insert(r -> r.setLanguage("Java"));
407     IntStream.rangeClosed(1, 3).forEach(i -> stats.add(db.issues().insert(rule8, project, file).toDefaultIssue()));
408     mockDetailsSupplierComponents(project, file);
409     mockDetailsSupplierRules(rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8);
410
411     underTest.setStatistics(project.longName(), stats);
412
413     String javaSuffix = " (Java)";
414     assertThat(underTest.getFieldValue(RULE + ".1.label")).isEqualTo(rule1.getName() + javaSuffix);
415     assertThat(underTest.getFieldValue(RULE + ".1.count")).isEqualTo("10");
416     assertThat(underTest.getFieldValue(RULE + ".2.label")).isEqualTo(rule2.getName() + javaSuffix);
417     assertThat(underTest.getFieldValue(RULE + ".2.count")).isEqualTo("9");
418     assertThat(underTest.getFieldValue(RULE + ".3.label")).isEqualTo(rule3.getName() + javaSuffix);
419     assertThat(underTest.getFieldValue(RULE + ".3.count")).isEqualTo("8");
420     assertThat(underTest.getFieldValue(RULE + ".4.label")).isEqualTo(rule4.getName() + javaSuffix);
421     assertThat(underTest.getFieldValue(RULE + ".4.count")).isEqualTo("7");
422     assertThat(underTest.getFieldValue(RULE + ".5.label")).isEqualTo(rule5.getName() + javaSuffix);
423     assertThat(underTest.getFieldValue(RULE + ".5.count")).isEqualTo("6");
424     assertThat(underTest.getFieldValue(RULE + ".6.label")).isNull();
425     assertThat(underTest.getFieldValue(RULE + ".6.count")).isNull();
426   }
427
428   @Test
429   public void set_debt() {
430     underTest.setDebt(Duration.create(55));
431
432     assertThat(underTest.getFieldValue(EFFORT + ".count")).isEqualTo("55min");
433   }
434
435   @Test
436   public void RuleDefinition_implements_equals_base_on_name_and_language() {
437     String name = randomAlphabetic(5);
438     String language = randomAlphabetic(6);
439     RuleDefinition underTest = new RuleDefinition(name, language);
440
441     assertThat(underTest)
442       .isEqualTo(underTest)
443       .isEqualTo(new RuleDefinition(name, language))
444       .isNotEqualTo(new RuleDefinition(language, name))
445       .isNotEqualTo(new RuleDefinition(randomAlphabetic(7), name))
446       .isNotEqualTo(new RuleDefinition(language, randomAlphabetic(7)))
447       .isNotEqualTo(new RuleDefinition(language, null))
448       .isNotNull()
449       .isNotEqualTo(new Object());
450   }
451
452   @Test
453   public void RuleDefinition_implements_hashcode_base_on_name_and_language() {
454     String name = randomAlphabetic(5);
455     String language = randomAlphabetic(6);
456     RuleDefinition underTest = new RuleDefinition(name, language);
457
458     assertThat(underTest)
459       .hasSameHashCodeAs(underTest)
460       .hasSameHashCodeAs(new RuleDefinition(name, language));
461
462     assertThat(underTest.hashCode())
463       .isNotEqualTo(new RuleDefinition(language, name).hashCode())
464       .isNotEqualTo(new RuleDefinition(randomAlphabetic(7), name).hashCode())
465       .isNotEqualTo(new RuleDefinition(language, randomAlphabetic(7)).hashCode())
466       .isNotEqualTo(new RuleDefinition(language, null).hashCode())
467       .isNotEqualTo(new Object().hashCode());
468   }
469 }