]> source.dussan.org Git - sonarqube.git/blob
85975c863e0368c144c0ddcab09035ba3f40d964
[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.step;
21
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.ImmutableSet;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Date;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Random;
32 import java.util.Set;
33 import java.util.function.Supplier;
34 import java.util.stream.IntStream;
35 import java.util.stream.Stream;
36 import org.assertj.core.groups.Tuple;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.TemporaryFolder;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.invocation.InvocationOnMock;
43 import org.mockito.stubbing.Answer;
44 import org.sonar.api.notifications.Notification;
45 import org.sonar.api.rule.RuleKey;
46 import org.sonar.api.rules.RuleType;
47 import org.sonar.api.utils.Duration;
48 import org.sonar.api.utils.System2;
49 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
50 import org.sonar.ce.task.projectanalysis.analysis.Branch;
51 import org.sonar.ce.task.projectanalysis.component.Component;
52 import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
53 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
54 import org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache;
55 import org.sonar.ce.task.projectanalysis.notification.NotificationFactory;
56 import org.sonar.ce.task.projectanalysis.util.cache.DiskCache;
57 import org.sonar.ce.task.step.ComputationStep;
58 import org.sonar.ce.task.step.TestComputationStepContext;
59 import org.sonar.core.issue.DefaultIssue;
60 import org.sonar.db.DbTester;
61 import org.sonar.db.component.BranchType;
62 import org.sonar.db.component.ComponentDto;
63 import org.sonar.db.rule.RuleDto;
64 import org.sonar.db.user.UserDto;
65 import org.sonar.server.issue.notification.IssuesChangesNotification;
66 import org.sonar.server.issue.notification.MyNewIssuesNotification;
67 import org.sonar.server.issue.notification.NewIssuesNotification;
68 import org.sonar.server.issue.notification.NewIssuesStatistics;
69 import org.sonar.server.notification.NotificationService;
70 import org.sonar.server.project.Project;
71
72 import static java.util.Arrays.stream;
73 import static java.util.Collections.emptyList;
74 import static java.util.Collections.shuffle;
75 import static java.util.Collections.singleton;
76 import static java.util.stream.Collectors.toList;
77 import static java.util.stream.Stream.concat;
78 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
79 import static org.apache.commons.lang.math.RandomUtils.nextInt;
80 import static org.assertj.core.api.Assertions.assertThat;
81 import static org.assertj.core.groups.Tuple.tuple;
82 import static org.mockito.ArgumentCaptor.forClass;
83 import static org.mockito.ArgumentMatchers.anyCollection;
84 import static org.mockito.ArgumentMatchers.anyMap;
85 import static org.mockito.ArgumentMatchers.anySet;
86 import static org.mockito.ArgumentMatchers.eq;
87 import static org.mockito.Mockito.any;
88 import static org.mockito.Mockito.doReturn;
89 import static org.mockito.Mockito.mock;
90 import static org.mockito.Mockito.never;
91 import static org.mockito.Mockito.times;
92 import static org.mockito.Mockito.verify;
93 import static org.mockito.Mockito.verifyNoInteractions;
94 import static org.mockito.Mockito.verifyNoMoreInteractions;
95 import static org.mockito.Mockito.when;
96 import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
97 import static org.sonar.ce.task.projectanalysis.component.Component.Type;
98 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
99 import static org.sonar.ce.task.projectanalysis.step.SendIssueNotificationsStep.NOTIF_TYPES;
100 import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
101 import static org.sonar.db.component.BranchType.BRANCH;
102 import static org.sonar.db.component.BranchType.PULL_REQUEST;
103 import static org.sonar.db.component.ComponentTesting.newBranchComponent;
104 import static org.sonar.db.component.ComponentTesting.newBranchDto;
105 import static org.sonar.db.component.ComponentTesting.newFileDto;
106 import static org.sonar.db.component.ComponentTesting.newMainBranchDto;
107 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
108 import static org.sonar.db.issue.IssueTesting.newIssue;
109 import static org.sonar.db.rule.RuleTesting.newRule;
110
111 public class SendIssueNotificationsStepIT extends BaseStepTest {
112
113   private static final String BRANCH_NAME = "feature";
114   private static final String PULL_REQUEST_ID = "pr-123";
115
116   private static final long ANALYSE_DATE = 123L;
117   private static final int FIVE_MINUTES_IN_MS = 1000 * 60 * 5;
118
119   private static final Duration ISSUE_DURATION = Duration.create(100L);
120
121   private static final Component FILE = builder(Type.FILE, 11).build();
122   private static final Component PROJECT = builder(Type.PROJECT, 1)
123     .setProjectVersion(randomAlphanumeric(10))
124     .addChildren(FILE).build();
125
126   @Rule
127   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
128     .setRoot(PROJECT);
129   @Rule
130   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
131     .setBranch(new DefaultBranchImpl(DEFAULT_MAIN_BRANCH_NAME))
132     .setAnalysisDate(new Date(ANALYSE_DATE));
133   @Rule
134   public TemporaryFolder temp = new TemporaryFolder();
135   @Rule
136   public DbTester db = DbTester.create(System2.INSTANCE);
137
138   private final Random random = new Random();
139   private final RuleType[] RULE_TYPES_EXCEPT_HOTSPOTS = Stream.of(RuleType.values()).filter(r -> r != SECURITY_HOTSPOT).toArray(RuleType[]::new);
140   private final RuleType randomRuleType = RULE_TYPES_EXCEPT_HOTSPOTS[random.nextInt(RULE_TYPES_EXCEPT_HOTSPOTS.length)];
141   @SuppressWarnings("unchecked")
142   private Class<Map<String, UserDto>> assigneeCacheType = (Class<Map<String, UserDto>>) (Object) Map.class;
143   @SuppressWarnings("unchecked")
144   private Class<Set<DefaultIssue>> setType = (Class<Set<DefaultIssue>>) (Class<?>) Set.class;
145   @SuppressWarnings("unchecked")
146   private Class<Map<String, UserDto>> mapType = (Class<Map<String, UserDto>>) (Class<?>) Map.class;
147   private ArgumentCaptor<Map<String, UserDto>> assigneeCacheCaptor = ArgumentCaptor.forClass(assigneeCacheType);
148   private ArgumentCaptor<Set<DefaultIssue>> issuesSetCaptor = forClass(setType);
149   private ArgumentCaptor<Map<String, UserDto>> assigneeByUuidCaptor = forClass(mapType);
150   private NotificationService notificationService = mock(NotificationService.class);
151   private NotificationFactory notificationFactory = mock(NotificationFactory.class);
152   private NewIssuesNotification newIssuesNotificationMock = createNewIssuesNotificationMock();
153   private MyNewIssuesNotification myNewIssuesNotificationMock = createMyNewIssuesNotificationMock();
154
155   private ProtoIssueCache protoIssueCache;
156   private SendIssueNotificationsStep underTest;
157
158   @Before
159   public void setUp() throws Exception {
160     protoIssueCache = new ProtoIssueCache(temp.newFile(), System2.INSTANCE);
161     underTest = new SendIssueNotificationsStep(protoIssueCache, treeRootHolder, notificationService, analysisMetadataHolder,
162       notificationFactory, db.getDbClient());
163     when(notificationFactory.newNewIssuesNotification(any(assigneeCacheType))).thenReturn(newIssuesNotificationMock);
164     when(notificationFactory.newMyNewIssuesNotification(any(assigneeCacheType))).thenReturn(myNewIssuesNotificationMock);
165   }
166
167   @Test
168   public void do_not_send_notifications_if_no_subscribers() {
169     analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
170     when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(false);
171
172     TestComputationStepContext context = new TestComputationStepContext();
173     underTest.execute(context);
174
175     verify(notificationService, never()).deliver(any(Notification.class));
176     verify(notificationService, never()).deliverEmails(anyCollection());
177     verifyStatistics(context, 0, 0, 0);
178   }
179
180   @Test
181   public void send_global_new_issues_notification() {
182     analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
183     protoIssueCache.newAppender().append(
184         createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION)
185           .setCreationDate(new Date(ANALYSE_DATE)))
186       .close();
187     when(notificationService.hasProjectSubscribersForTypes(eq(PROJECT.getUuid()), any())).thenReturn(true);
188
189     TestComputationStepContext context = new TestComputationStepContext();
190     underTest.execute(context);
191
192     verify(notificationService).deliver(newIssuesNotificationMock);
193     verify(newIssuesNotificationMock).setProject(PROJECT.getKey(), PROJECT.getName(), null, null);
194     verify(newIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
195     verify(newIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), any());
196     verifyStatistics(context, 1, 0, 0);
197   }
198
199   @Test
200   public void send_global_new_issues_notification_only_for_non_backdated_issues() {
201     Random random = new Random();
202     Integer[] efforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10_000 * i).toArray(Integer[]::new);
203     Integer[] backDatedEfforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10 + random.nextInt(100)).toArray(Integer[]::new);
204     Duration expectedEffort = Duration.create(stream(efforts).mapToInt(i -> i).sum());
205     List<DefaultIssue> issues = concat(stream(efforts)
206         .map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort))
207           .setCreationDate(new Date(ANALYSE_DATE))),
208       stream(backDatedEfforts)
209         .map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort))
210           .setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS))))
211       .collect(toList());
212     shuffle(issues);
213     DiskCache.CacheAppender issueCache = this.protoIssueCache.newAppender();
214     issues.forEach(issueCache::append);
215     issueCache.close();
216     analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
217     when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
218
219     TestComputationStepContext context = new TestComputationStepContext();
220     underTest.execute(context);
221
222     verify(notificationService).deliver(newIssuesNotificationMock);
223     ArgumentCaptor<NewIssuesStatistics.Stats> statsCaptor = forClass(NewIssuesStatistics.Stats.class);
224     verify(newIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), statsCaptor.capture());
225     NewIssuesStatistics.Stats stats = statsCaptor.getValue();
226     assertThat(stats.hasIssues()).isTrue();
227     // just checking all issues have been added to the stats
228     assertThat(stats.getIssueCount().getOnCurrentAnalysis()).isEqualTo(efforts.length);
229     assertThat(stats.getIssueCount().getTotal()).isEqualTo(backDatedEfforts.length + efforts.length);
230     verifyStatistics(context, 1, 0, 0);
231   }
232
233   @Test
234   public void do_not_send_global_new_issues_notification_if_issue_has_been_backdated() {
235     analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
236     protoIssueCache.newAppender().append(
237         createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION)
238           .setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS)))
239       .close();
240     when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
241
242     TestComputationStepContext context = new TestComputationStepContext();
243     underTest.execute(context);
244
245     verify(notificationService, never()).deliver(any(Notification.class));
246     verify(notificationService, never()).deliverEmails(anyCollection());
247     verifyStatistics(context, 0, 0, 0);
248   }
249
250   @Test
251   public void send_global_new_issues_notification_on_branch() {
252     ComponentDto project = newPrivateProjectDto();
253     ComponentDto branch = setUpBranch(project, BRANCH);
254     protoIssueCache.newAppender().append(
255       createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE))).close();
256     when(notificationService.hasProjectSubscribersForTypes(branch.uuid(), NOTIF_TYPES)).thenReturn(true);
257     analysisMetadataHolder.setProject(Project.from(project));
258     analysisMetadataHolder.setBranch(newBranch(BranchType.BRANCH));
259
260     TestComputationStepContext context = new TestComputationStepContext();
261     underTest.execute(context);
262
263     verify(notificationService).deliver(newIssuesNotificationMock);
264     verify(newIssuesNotificationMock).setProject(branch.getKey(), branch.longName(), BRANCH_NAME, null);
265     verify(newIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
266     verify(newIssuesNotificationMock).setStatistics(eq(branch.longName()), any(NewIssuesStatistics.Stats.class));
267     verifyStatistics(context, 1, 0, 0);
268   }
269
270   @Test
271   public void do_not_send_global_new_issues_notification_on_pull_request() {
272     ComponentDto project = newPrivateProjectDto();
273     ComponentDto branch = setUpBranch(project, PULL_REQUEST);
274     protoIssueCache.newAppender().append(
275       createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE))).close();
276     when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
277     analysisMetadataHolder.setProject(Project.from(project));
278     analysisMetadataHolder.setBranch(newPullRequest());
279     analysisMetadataHolder.setPullRequestKey(PULL_REQUEST_ID);
280
281     TestComputationStepContext context = new TestComputationStepContext();
282     underTest.execute(context);
283
284     verifyNoInteractions(notificationService, newIssuesNotificationMock);
285   }
286
287   private DefaultIssue createIssue() {
288     return new DefaultIssue().setKey("k").setProjectKey("p").setStatus("OPEN").setProjectUuid("uuid").setComponentKey("c").setRuleKey(RuleKey.of("r", "r"));
289   }
290
291   @Test
292   public void do_not_send_global_new_issues_notification_on_branch_if_issue_has_been_backdated() {
293     ComponentDto project = newPrivateProjectDto();
294     ComponentDto branch = setUpBranch(project, BRANCH);
295     protoIssueCache.newAppender().append(
296       createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS))).close();
297     when(notificationService.hasProjectSubscribersForTypes(branch.uuid(), NOTIF_TYPES)).thenReturn(true);
298     analysisMetadataHolder.setProject(Project.from(project));
299     analysisMetadataHolder.setBranch(newBranch(BranchType.BRANCH));
300
301     TestComputationStepContext context = new TestComputationStepContext();
302     underTest.execute(context);
303
304     verify(notificationService, never()).deliver(any(Notification.class));
305     verify(notificationService, never()).deliverEmails(anyCollection());
306     verifyStatistics(context, 0, 0, 0);
307   }
308
309   @Test
310   public void send_new_issues_notification_to_user() {
311     UserDto user = db.users().insertUser();
312     analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
313
314     protoIssueCache.newAppender().append(
315         createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setAssigneeUuid(user.getUuid()).setCreationDate(new Date(ANALYSE_DATE)))
316       .close();
317     when(notificationService.hasProjectSubscribersForTypes(eq(PROJECT.getUuid()), any())).thenReturn(true);
318
319     TestComputationStepContext context = new TestComputationStepContext();
320     underTest.execute(context);
321
322     verify(notificationService).deliverEmails(ImmutableSet.of(newIssuesNotificationMock));
323     verify(notificationService).deliverEmails(ImmutableSet.of(myNewIssuesNotificationMock));
324     // old API compatibility call
325     verify(notificationService).deliver(newIssuesNotificationMock);
326     verify(notificationService).deliver(myNewIssuesNotificationMock);
327     verify(myNewIssuesNotificationMock).setAssignee(any(UserDto.class));
328     verify(myNewIssuesNotificationMock).setProject(PROJECT.getKey(), PROJECT.getName(), null, null);
329     verify(myNewIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
330     verify(myNewIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), any(NewIssuesStatistics.Stats.class));
331     verifyStatistics(context, 1, 1, 0);
332   }
333
334   @Test
335   public void send_new_issues_notification_to_user_only_for_those_assigned_to_her() throws IOException {
336     UserDto perceval = db.users().insertUser(u -> u.setLogin("perceval"));
337     Integer[] assigned = IntStream.range(0, 5).mapToObj(i -> 10_000 * i).toArray(Integer[]::new);
338     Duration expectedEffort = Duration.create(stream(assigned).mapToInt(i -> i).sum());
339
340     UserDto arthur = db.users().insertUser(u -> u.setLogin("arthur"));
341     Integer[] assignedToOther = IntStream.range(0, 3).mapToObj(i -> 10).toArray(Integer[]::new);
342
343     List<DefaultIssue> issues = concat(stream(assigned)
344         .map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort))
345           .setAssigneeUuid(perceval.getUuid())
346           .setNew(true)
347           .setCreationDate(new Date(ANALYSE_DATE))),
348       stream(assignedToOther)
349         .map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort))
350           .setAssigneeUuid(arthur.getUuid())
351           .setNew(true)
352           .setCreationDate(new Date(ANALYSE_DATE))))
353       .collect(toList());
354     shuffle(issues);
355     ProtoIssueCache protoIssueCache = new ProtoIssueCache(temp.newFile(), System2.INSTANCE);
356     DiskCache.CacheAppender newIssueCache = protoIssueCache.newAppender();
357     issues.forEach(newIssueCache::append);
358     newIssueCache.close();
359     analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
360     when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
361
362     NotificationFactory notificationFactory = mock(NotificationFactory.class);
363     NewIssuesNotification newIssuesNotificationMock = createNewIssuesNotificationMock();
364     when(notificationFactory.newNewIssuesNotification(assigneeCacheCaptor.capture()))
365       .thenReturn(newIssuesNotificationMock);
366
367     MyNewIssuesNotification myNewIssuesNotificationMock1 = createMyNewIssuesNotificationMock();
368     MyNewIssuesNotification myNewIssuesNotificationMock2 = createMyNewIssuesNotificationMock();
369     doReturn(myNewIssuesNotificationMock1).doReturn(myNewIssuesNotificationMock2).when(notificationFactory).newMyNewIssuesNotification(any(assigneeCacheType));
370
371     TestComputationStepContext context = new TestComputationStepContext();
372     new SendIssueNotificationsStep(protoIssueCache, treeRootHolder, notificationService, analysisMetadataHolder, notificationFactory, db.getDbClient())
373       .execute(context);
374
375     verify(notificationService).deliverEmails(ImmutableSet.of(myNewIssuesNotificationMock1, myNewIssuesNotificationMock2));
376     // old API compatibility
377     verify(notificationService).deliver(myNewIssuesNotificationMock1);
378     verify(notificationService).deliver(myNewIssuesNotificationMock2);
379
380     verify(notificationFactory).newNewIssuesNotification(assigneeCacheCaptor.capture());
381     verify(notificationFactory, times(2)).newMyNewIssuesNotification(assigneeCacheCaptor.capture());
382     verifyNoMoreInteractions(notificationFactory);
383     verifyAssigneeCache(assigneeCacheCaptor, perceval, arthur);
384
385     Map<String, MyNewIssuesNotification> myNewIssuesNotificationMocksByUsersName = new HashMap<>();
386     ArgumentCaptor<UserDto> userCaptor1 = forClass(UserDto.class);
387     verify(myNewIssuesNotificationMock1).setAssignee(userCaptor1.capture());
388     myNewIssuesNotificationMocksByUsersName.put(userCaptor1.getValue().getLogin(), myNewIssuesNotificationMock1);
389
390     ArgumentCaptor<UserDto> userCaptor2 = forClass(UserDto.class);
391     verify(myNewIssuesNotificationMock2).setAssignee(userCaptor2.capture());
392     myNewIssuesNotificationMocksByUsersName.put(userCaptor2.getValue().getLogin(), myNewIssuesNotificationMock2);
393
394     MyNewIssuesNotification myNewIssuesNotificationMock = myNewIssuesNotificationMocksByUsersName.get("perceval");
395     ArgumentCaptor<NewIssuesStatistics.Stats> statsCaptor = forClass(NewIssuesStatistics.Stats.class);
396     verify(myNewIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), statsCaptor.capture());
397
398     NewIssuesStatistics.Stats stats = statsCaptor.getValue();
399     assertThat(stats.hasIssues()).isTrue();
400     // just checking all issues have been added to the stats
401     assertThat(stats.getIssueCount().getOnCurrentAnalysis()).isEqualTo(assigned.length);
402     assertThat(stats.getIssueCount().getTotal()).isEqualTo(assigned.length);
403
404     verifyStatistics(context, 1, 2, 0);
405   }
406
407   @Test
408   public void send_new_issues_notification_to_user_only_for_non_backdated_issues() {
409     UserDto user = db.users().insertUser();
410     Random random = new Random();
411     Integer[] efforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10_000 * i).toArray(Integer[]::new);
412     Integer[] backDatedEfforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10 + random.nextInt(100)).toArray(Integer[]::new);
413     Duration expectedEffort = Duration.create(stream(efforts).mapToInt(i -> i).sum());
414     List<DefaultIssue> issues = concat(stream(efforts)
415         .map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort))
416           .setAssigneeUuid(user.getUuid())
417           .setCreationDate(new Date(ANALYSE_DATE))),
418       stream(backDatedEfforts)
419         .map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort))
420           .setAssigneeUuid(user.getUuid())
421           .setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS))))
422       .collect(toList());
423     shuffle(issues);
424     DiskCache.CacheAppender issueCache = this.protoIssueCache.newAppender();
425     issues.forEach(issueCache::append);
426     issueCache.close();
427     analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
428     when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
429
430     TestComputationStepContext context = new TestComputationStepContext();
431     underTest.execute(context);
432
433     verify(notificationService).deliver(newIssuesNotificationMock);
434     verify(notificationService).deliverEmails(ImmutableSet.of(myNewIssuesNotificationMock));
435     // old API compatibility
436     verify(notificationService).deliver(myNewIssuesNotificationMock);
437
438     verify(notificationFactory).newNewIssuesNotification(assigneeCacheCaptor.capture());
439     verify(notificationFactory).newMyNewIssuesNotification(assigneeCacheCaptor.capture());
440     verifyNoMoreInteractions(notificationFactory);
441     verifyAssigneeCache(assigneeCacheCaptor, user);
442
443     verify(myNewIssuesNotificationMock).setAssignee(any(UserDto.class));
444     ArgumentCaptor<NewIssuesStatistics.Stats> statsCaptor = forClass(NewIssuesStatistics.Stats.class);
445     verify(myNewIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), statsCaptor.capture());
446     NewIssuesStatistics.Stats stats = statsCaptor.getValue();
447     assertThat(stats.hasIssues()).isTrue();
448     // just checking all issues have been added to the stats
449     assertThat(stats.getIssueCount().getOnCurrentAnalysis()).isEqualTo(efforts.length);
450     assertThat(stats.getIssueCount().getTotal()).isEqualTo(backDatedEfforts.length + efforts.length);
451
452     verifyStatistics(context, 1, 1, 0);
453   }
454
455   private static void verifyAssigneeCache(ArgumentCaptor<Map<String, UserDto>> assigneeCacheCaptor, UserDto... users) {
456     Map<String, UserDto> cache = assigneeCacheCaptor.getAllValues().iterator().next();
457     assertThat(assigneeCacheCaptor.getAllValues())
458       .filteredOn(t -> t != cache)
459       .isEmpty();
460     Tuple[] expected = stream(users).map(user -> tuple(user.getUuid(), user.getUuid(), user.getUuid(), user.getLogin())).toArray(Tuple[]::new);
461     assertThat(cache.entrySet())
462       .extracting(Map.Entry::getKey, t -> t.getValue().getUuid(), t -> t.getValue().getUuid(), t -> t.getValue().getLogin())
463       .containsOnly(expected);
464   }
465
466   @Test
467   public void do_not_send_new_issues_notification_to_user_if_issue_is_backdated() {
468     analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
469     UserDto user = db.users().insertUser();
470     protoIssueCache.newAppender().append(
471         createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setAssigneeUuid(user.getUuid())
472           .setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS)))
473       .close();
474     when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
475
476     TestComputationStepContext context = new TestComputationStepContext();
477     underTest.execute(context);
478
479     verify(notificationService, never()).deliver(any(Notification.class));
480     verify(notificationService, never()).deliverEmails(anyCollection());
481     verifyStatistics(context, 0, 0, 0);
482   }
483
484   @Test
485   public void send_issues_change_notification() {
486     sendIssueChangeNotification(ANALYSE_DATE);
487   }
488
489   @Test
490   public void do_not_send_new_issues_notifications_for_hotspot() {
491     UserDto user = db.users().insertUser();
492     ComponentDto project = newPrivateProjectDto().setKey(PROJECT.getKey()).setLongName(PROJECT.getName());
493     ComponentDto file = newFileDto(project).setKey(FILE.getKey()).setLongName(FILE.getName());
494     RuleDto ruleDefinitionDto = newRule();
495     prepareIssue(ANALYSE_DATE, user, project, file, ruleDefinitionDto, RuleType.SECURITY_HOTSPOT);
496     analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
497     when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
498
499     TestComputationStepContext context = new TestComputationStepContext();
500     underTest.execute(context);
501
502     verify(notificationService, never()).deliver(any(Notification.class));
503     verify(notificationService, never()).deliverEmails(anyCollection());
504     verifyStatistics(context, 0, 0, 0);
505   }
506
507   @Test
508   public void send_issues_change_notification_even_if_issue_is_backdated() {
509     sendIssueChangeNotification(ANALYSE_DATE - FIVE_MINUTES_IN_MS);
510   }
511
512   private void sendIssueChangeNotification(long issueCreatedAt) {
513     UserDto user = db.users().insertUser();
514     ComponentDto project = newPrivateProjectDto().setKey(PROJECT.getKey()).setLongName(PROJECT.getName());
515     analysisMetadataHolder.setProject(Project.from(project));
516     ComponentDto file = newFileDto(project).setKey(FILE.getKey()).setLongName(FILE.getName());
517     treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(project.getKey()).setName(project.longName()).setUuid(project.uuid())
518       .addChildren(
519         builder(Type.FILE, 11).setKey(file.getKey()).setName(file.longName()).build())
520       .build());
521     RuleDto ruleDefinitionDto = newRule();
522     RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)];
523     DefaultIssue issue = prepareIssue(issueCreatedAt, user, project, file, ruleDefinitionDto, randomTypeExceptHotspot);
524     IssuesChangesNotification issuesChangesNotification = mock(IssuesChangesNotification.class);
525     when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
526     when(notificationFactory.newIssuesChangesNotification(anySet(), anyMap())).thenReturn(issuesChangesNotification);
527
528     underTest.execute(new TestComputationStepContext());
529
530     verify(notificationFactory).newIssuesChangesNotification(issuesSetCaptor.capture(), assigneeByUuidCaptor.capture());
531     assertThat(issuesSetCaptor.getValue()).hasSize(1);
532     assertThat(issuesSetCaptor.getValue().iterator().next()).isEqualTo(issue);
533     assertThat(assigneeByUuidCaptor.getValue()).hasSize(1);
534     assertThat(assigneeByUuidCaptor.getValue().get(user.getUuid())).isNotNull();
535     verify(notificationService).hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES);
536     verify(notificationService).deliverEmails(singleton(issuesChangesNotification));
537     verify(notificationService).deliver(issuesChangesNotification);
538     verifyNoMoreInteractions(notificationService);
539   }
540
541   private DefaultIssue prepareIssue(long issueCreatedAt, UserDto user, ComponentDto project, ComponentDto file, RuleDto ruleDefinitionDto, RuleType type) {
542     DefaultIssue issue = newIssue(ruleDefinitionDto, project, file).setType(type).toDefaultIssue()
543       .setNew(false).setChanged(true).setSendNotifications(true).setCreationDate(new Date(issueCreatedAt)).setAssigneeUuid(user.getUuid());
544     protoIssueCache.newAppender().append(issue).close();
545     when(notificationService.hasProjectSubscribersForTypes(project.branchUuid(), NOTIF_TYPES)).thenReturn(true);
546     return issue;
547   }
548
549   @Test
550   public void send_issues_change_notification_on_branch() {
551     sendIssueChangeNotificationOnBranch(ANALYSE_DATE);
552   }
553
554   @Test
555   public void send_issues_change_notification_on_branch_even_if_issue_is_backdated() {
556     sendIssueChangeNotificationOnBranch(ANALYSE_DATE - FIVE_MINUTES_IN_MS);
557   }
558
559   private void sendIssueChangeNotificationOnBranch(long issueCreatedAt) {
560     ComponentDto project = newPrivateProjectDto();
561     ComponentDto branch = newBranchComponent(project, newBranchDto(project).setKey(BRANCH_NAME));
562     ComponentDto file = newFileDto(branch, project.uuid());
563     treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(branch.getKey()).setName(branch.longName()).setUuid(branch.uuid()).addChildren(
564       builder(Type.FILE, 11).setKey(file.getKey()).setName(file.longName()).build()).build());
565     analysisMetadataHolder.setProject(Project.from(project));
566     RuleDto ruleDefinitionDto = newRule();
567     RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)];
568     DefaultIssue issue = newIssue(ruleDefinitionDto, branch, file).setType(randomTypeExceptHotspot).toDefaultIssue()
569       .setNew(false)
570       .setChanged(true)
571       .setSendNotifications(true)
572       .setCreationDate(new Date(issueCreatedAt));
573     protoIssueCache.newAppender().append(issue).close();
574     when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
575     IssuesChangesNotification issuesChangesNotification = mock(IssuesChangesNotification.class);
576     when(notificationFactory.newIssuesChangesNotification(anySet(), anyMap())).thenReturn(issuesChangesNotification);
577     analysisMetadataHolder.setBranch(newBranch(BranchType.BRANCH));
578
579     underTest.execute(new TestComputationStepContext());
580
581     verify(notificationFactory).newIssuesChangesNotification(issuesSetCaptor.capture(), assigneeByUuidCaptor.capture());
582     assertThat(issuesSetCaptor.getValue()).hasSize(1);
583     assertThat(issuesSetCaptor.getValue().iterator().next()).isEqualTo(issue);
584     assertThat(assigneeByUuidCaptor.getValue()).isEmpty();
585     verify(notificationService).hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES);
586     verify(notificationService).deliverEmails(singleton(issuesChangesNotification));
587     verify(notificationService).deliver(issuesChangesNotification);
588     verifyNoMoreInteractions(notificationService);
589   }
590
591   @Test
592   public void sends_one_issue_change_notification_every_1000_issues() {
593     UserDto user = db.users().insertUser();
594     ComponentDto project = newPrivateProjectDto().setKey(PROJECT.getKey()).setLongName(PROJECT.getName());
595     ComponentDto file = newFileDto(project).setKey(FILE.getKey()).setLongName(FILE.getName());
596     RuleDto ruleDefinitionDto = newRule();
597     RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)];
598     List<DefaultIssue> issues = IntStream.range(0, 2001 + new Random().nextInt(10))
599       .mapToObj(i -> newIssue(ruleDefinitionDto, project, file).setKee("uuid_" + i).setType(randomTypeExceptHotspot).toDefaultIssue()
600         .setNew(false).setChanged(true).setSendNotifications(true).setAssigneeUuid(user.getUuid()))
601       .toList();
602     DiskCache.CacheAppender cacheAppender = protoIssueCache.newAppender();
603     issues.forEach(cacheAppender::append);
604     cacheAppender.close();
605     analysisMetadataHolder.setProject(Project.from(project));
606     NewIssuesFactoryCaptor newIssuesFactoryCaptor = new NewIssuesFactoryCaptor(() -> mock(IssuesChangesNotification.class));
607     when(notificationFactory.newIssuesChangesNotification(anySet(), anyMap())).thenAnswer(newIssuesFactoryCaptor);
608     when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
609     when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
610
611     underTest.execute(new TestComputationStepContext());
612
613     verify(notificationFactory, times(3)).newIssuesChangesNotification(anySet(), anyMap());
614     assertThat(newIssuesFactoryCaptor.issuesSetCaptor).hasSize(3);
615     assertThat(newIssuesFactoryCaptor.issuesSetCaptor.get(0)).hasSize(1000);
616     assertThat(newIssuesFactoryCaptor.issuesSetCaptor.get(1)).hasSize(1000);
617     assertThat(newIssuesFactoryCaptor.issuesSetCaptor.get(2)).hasSize(issues.size() - 2000);
618     assertThat(newIssuesFactoryCaptor.assigneeCacheCaptor)
619       .hasSize(3)
620       .containsOnly(newIssuesFactoryCaptor.assigneeCacheCaptor.iterator().next());
621     ArgumentCaptor<Collection> collectionCaptor = forClass(Collection.class);
622     verify(notificationService, times(3)).deliverEmails(collectionCaptor.capture());
623     assertThat(collectionCaptor.getAllValues()).hasSize(3);
624     assertThat(collectionCaptor.getAllValues().get(0)).hasSize(1);
625     assertThat(collectionCaptor.getAllValues().get(1)).hasSize(1);
626     assertThat(collectionCaptor.getAllValues().get(2)).hasSize(1);
627     verify(notificationService, times(3)).deliver(any(IssuesChangesNotification.class));
628   }
629
630   /**
631    * Since the very same Set object is passed to {@link NotificationFactory#newIssuesChangesNotification(Set, Map)} and
632    * reset between each call. We must make a copy of each argument to capture what's been passed to the factory.
633    * This is of course not supported by Mockito's {@link ArgumentCaptor} and we implement this ourselves with a
634    * {@link Answer}.
635    */
636   private static class NewIssuesFactoryCaptor implements Answer<Object> {
637     private final Supplier<IssuesChangesNotification> delegate;
638     private final List<Set<DefaultIssue>> issuesSetCaptor = new ArrayList<>();
639     private final List<Map<String, UserDto>> assigneeCacheCaptor = new ArrayList<>();
640
641     private NewIssuesFactoryCaptor(Supplier<IssuesChangesNotification> delegate) {
642       this.delegate = delegate;
643     }
644
645     @Override
646     public Object answer(InvocationOnMock t) {
647       Set<DefaultIssue> issuesSet = t.getArgument(0);
648       Map<String, UserDto> assigneeCatch = t.getArgument(1);
649       issuesSetCaptor.add(ImmutableSet.copyOf(issuesSet));
650       assigneeCacheCaptor.add(ImmutableMap.copyOf(assigneeCatch));
651       return delegate.get();
652     }
653   }
654
655   private NewIssuesNotification createNewIssuesNotificationMock() {
656     NewIssuesNotification notification = mock(NewIssuesNotification.class);
657     when(notification.setProject(any(), any(), any(), any())).thenReturn(notification);
658     when(notification.setProjectVersion(any())).thenReturn(notification);
659     when(notification.setAnalysisDate(any())).thenReturn(notification);
660     when(notification.setStatistics(any(), any())).thenReturn(notification);
661     return notification;
662   }
663
664   private MyNewIssuesNotification createMyNewIssuesNotificationMock() {
665     MyNewIssuesNotification notification = mock(MyNewIssuesNotification.class);
666     when(notification.setAssignee(any(UserDto.class))).thenReturn(notification);
667     when(notification.setProject(any(), any(), any(), any())).thenReturn(notification);
668     when(notification.setProjectVersion(any())).thenReturn(notification);
669     when(notification.setAnalysisDate(any())).thenReturn(notification);
670     when(notification.setStatistics(any(), any())).thenReturn(notification);
671     return notification;
672   }
673
674   private static Branch newBranch(BranchType type) {
675     Branch branch = mock(Branch.class);
676     when(branch.isMain()).thenReturn(false);
677     when(branch.getName()).thenReturn(BRANCH_NAME);
678     when(branch.getType()).thenReturn(type);
679     return branch;
680   }
681
682   private static Branch newPullRequest() {
683     Branch branch = mock(Branch.class);
684     when(branch.isMain()).thenReturn(false);
685     when(branch.getType()).thenReturn(PULL_REQUEST);
686     when(branch.getName()).thenReturn(BRANCH_NAME);
687     when(branch.getPullRequestKey()).thenReturn(PULL_REQUEST_ID);
688     return branch;
689   }
690
691   private ComponentDto setUpBranch(ComponentDto project, BranchType branchType) {
692     ComponentDto branch = null;
693     if(branchType == PULL_REQUEST) {
694       branch = newBranchComponent(project, newBranchDto(project, PULL_REQUEST, project.uuid()));
695     } else {
696       branch = newBranchComponent(project, newMainBranchDto(project, project.uuid()).setKey(BRANCH_NAME));
697     }
698     ComponentDto file = newFileDto(branch, project.uuid());
699     treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(branch.getKey()).setName(branch.longName()).setUuid(branch.uuid()).addChildren(
700       builder(Type.FILE, 11).setKey(file.getKey()).setName(file.longName()).build()).build());
701     return branch;
702   }
703
704   private static void verifyStatistics(TestComputationStepContext context, int expectedNewIssuesNotifications, int expectedMyNewIssuesNotifications,
705     int expectedIssueChangesNotifications) {
706     context.getStatistics().assertValue("newIssuesNotifs", expectedNewIssuesNotifications);
707     context.getStatistics().assertValue("myNewIssuesNotifs", expectedMyNewIssuesNotifications);
708     context.getStatistics().assertValue("changesNotifs", expectedIssueChangesNotifications);
709   }
710
711   @Override
712   protected ComputationStep step() {
713     return underTest;
714   }
715 }