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