3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.step;
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;
31 import java.util.Random;
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;
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.lang3.RandomStringUtils.randomAlphanumeric;
79 import static org.assertj.core.api.Assertions.assertThat;
80 import static org.assertj.core.groups.Tuple.tuple;
81 import static org.mockito.ArgumentCaptor.forClass;
82 import static org.mockito.ArgumentMatchers.anyCollection;
83 import static org.mockito.ArgumentMatchers.anyMap;
84 import static org.mockito.ArgumentMatchers.anySet;
85 import static org.mockito.ArgumentMatchers.eq;
86 import static org.mockito.Mockito.any;
87 import static org.mockito.Mockito.doReturn;
88 import static org.mockito.Mockito.mock;
89 import static org.mockito.Mockito.never;
90 import static org.mockito.Mockito.times;
91 import static org.mockito.Mockito.verify;
92 import static org.mockito.Mockito.verifyNoInteractions;
93 import static org.mockito.Mockito.verifyNoMoreInteractions;
94 import static org.mockito.Mockito.when;
95 import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
96 import static org.sonar.ce.task.projectanalysis.component.Component.Type;
97 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
98 import static org.sonar.ce.task.projectanalysis.step.SendIssueNotificationsStep.NOTIF_TYPES;
99 import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
100 import static org.sonar.db.component.BranchType.BRANCH;
101 import static org.sonar.db.component.BranchType.PULL_REQUEST;
102 import static org.sonar.db.component.ComponentTesting.newBranchComponent;
103 import static org.sonar.db.component.ComponentTesting.newBranchDto;
104 import static org.sonar.db.component.ComponentTesting.newFileDto;
105 import static org.sonar.db.component.ComponentTesting.newMainBranchDto;
106 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
107 import static org.sonar.db.issue.IssueTesting.newIssue;
108 import static org.sonar.db.rule.RuleTesting.newRule;
110 public class SendIssueNotificationsStepIT extends BaseStepTest {
112 private static final String BRANCH_NAME = "feature";
113 private static final String PULL_REQUEST_ID = "pr-123";
115 private static final long ANALYSE_DATE = 123L;
116 private static final int FIVE_MINUTES_IN_MS = 1000 * 60 * 5;
118 private static final Duration ISSUE_DURATION = Duration.create(100L);
120 private static final Component FILE = builder(Type.FILE, 11).build();
121 private static final Component PROJECT = builder(Type.PROJECT, 1)
122 .setProjectVersion(randomAlphanumeric(10))
123 .addChildren(FILE).build();
126 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
129 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
130 .setBranch(new DefaultBranchImpl(DEFAULT_MAIN_BRANCH_NAME))
131 .setAnalysisDate(new Date(ANALYSE_DATE));
133 public TemporaryFolder temp = new TemporaryFolder();
135 public DbTester db = DbTester.create(System2.INSTANCE);
137 private final Random random = new Random();
138 private final RuleType[] RULE_TYPES_EXCEPT_HOTSPOTS = Stream.of(RuleType.values()).filter(r -> r != SECURITY_HOTSPOT).toArray(RuleType[]::new);
139 private final RuleType randomRuleType = RULE_TYPES_EXCEPT_HOTSPOTS[random.nextInt(RULE_TYPES_EXCEPT_HOTSPOTS.length)];
140 @SuppressWarnings("unchecked")
141 private Class<Map<String, UserDto>> assigneeCacheType = (Class<Map<String, UserDto>>) (Object) Map.class;
142 @SuppressWarnings("unchecked")
143 private Class<Set<DefaultIssue>> setType = (Class<Set<DefaultIssue>>) (Class<?>) Set.class;
144 @SuppressWarnings("unchecked")
145 private Class<Map<String, UserDto>> mapType = (Class<Map<String, UserDto>>) (Class<?>) Map.class;
146 private ArgumentCaptor<Map<String, UserDto>> assigneeCacheCaptor = ArgumentCaptor.forClass(assigneeCacheType);
147 private ArgumentCaptor<Set<DefaultIssue>> issuesSetCaptor = forClass(setType);
148 private ArgumentCaptor<Map<String, UserDto>> assigneeByUuidCaptor = forClass(mapType);
149 private NotificationService notificationService = mock(NotificationService.class);
150 private NotificationFactory notificationFactory = mock(NotificationFactory.class);
151 private NewIssuesNotification newIssuesNotificationMock = createNewIssuesNotificationMock();
152 private MyNewIssuesNotification myNewIssuesNotificationMock = createMyNewIssuesNotificationMock();
154 private ProtoIssueCache protoIssueCache;
155 private SendIssueNotificationsStep underTest;
158 public void setUp() throws Exception {
159 protoIssueCache = new ProtoIssueCache(temp.newFile(), System2.INSTANCE);
160 underTest = new SendIssueNotificationsStep(protoIssueCache, treeRootHolder, notificationService, analysisMetadataHolder,
161 notificationFactory, db.getDbClient());
162 when(notificationFactory.newNewIssuesNotification(any(assigneeCacheType))).thenReturn(newIssuesNotificationMock);
163 when(notificationFactory.newMyNewIssuesNotification(any(assigneeCacheType))).thenReturn(myNewIssuesNotificationMock);
167 public void do_not_send_notifications_if_no_subscribers() {
168 analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
169 when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(false);
171 TestComputationStepContext context = new TestComputationStepContext();
172 underTest.execute(context);
174 verify(notificationService, never()).deliver(any(Notification.class));
175 verify(notificationService, never()).deliverEmails(anyCollection());
176 verifyStatistics(context, 0, 0, 0);
180 public void send_global_new_issues_notification() {
181 analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
182 protoIssueCache.newAppender().append(
183 createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION)
184 .setCreationDate(new Date(ANALYSE_DATE)))
186 when(notificationService.hasProjectSubscribersForTypes(eq(PROJECT.getUuid()), any())).thenReturn(true);
188 TestComputationStepContext context = new TestComputationStepContext();
189 underTest.execute(context);
191 verify(notificationService).deliver(newIssuesNotificationMock);
192 verify(newIssuesNotificationMock).setProject(PROJECT.getKey(), PROJECT.getName(), null, null);
193 verify(newIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
194 verify(newIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), any());
195 verifyStatistics(context, 1, 0, 0);
199 public void send_global_new_issues_notification_only_for_non_backdated_issues() {
200 Random random = new Random();
201 Integer[] efforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10_000 * i).toArray(Integer[]::new);
202 Integer[] backDatedEfforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10 + random.nextInt(100)).toArray(Integer[]::new);
203 Duration expectedEffort = Duration.create(stream(efforts).mapToInt(i -> i).sum());
204 List<DefaultIssue> issues = concat(stream(efforts)
205 .map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort))
206 .setCreationDate(new Date(ANALYSE_DATE))),
207 stream(backDatedEfforts)
208 .map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort))
209 .setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS))))
212 DiskCache.CacheAppender issueCache = this.protoIssueCache.newAppender();
213 issues.forEach(issueCache::append);
215 analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
216 when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
218 TestComputationStepContext context = new TestComputationStepContext();
219 underTest.execute(context);
221 verify(notificationService).deliver(newIssuesNotificationMock);
222 ArgumentCaptor<NewIssuesStatistics.Stats> statsCaptor = forClass(NewIssuesStatistics.Stats.class);
223 verify(newIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), statsCaptor.capture());
224 NewIssuesStatistics.Stats stats = statsCaptor.getValue();
225 assertThat(stats.hasIssues()).isTrue();
226 // just checking all issues have been added to the stats
227 assertThat(stats.getIssueCount().getOnCurrentAnalysis()).isEqualTo(efforts.length);
228 assertThat(stats.getIssueCount().getTotal()).isEqualTo(backDatedEfforts.length + efforts.length);
229 verifyStatistics(context, 1, 0, 0);
233 public void do_not_send_global_new_issues_notification_if_issue_has_been_backdated() {
234 analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
235 protoIssueCache.newAppender().append(
236 createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION)
237 .setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS)))
239 when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
241 TestComputationStepContext context = new TestComputationStepContext();
242 underTest.execute(context);
244 verify(notificationService, never()).deliver(any(Notification.class));
245 verify(notificationService, never()).deliverEmails(anyCollection());
246 verifyStatistics(context, 0, 0, 0);
250 public void send_global_new_issues_notification_on_branch() {
251 ComponentDto project = newPrivateProjectDto();
252 ComponentDto branch = setUpBranch(project, BRANCH);
253 protoIssueCache.newAppender().append(
254 createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE))).close();
255 when(notificationService.hasProjectSubscribersForTypes(branch.uuid(), NOTIF_TYPES)).thenReturn(true);
256 analysisMetadataHolder.setProject(Project.from(project));
257 analysisMetadataHolder.setBranch(newBranch(BranchType.BRANCH));
259 TestComputationStepContext context = new TestComputationStepContext();
260 underTest.execute(context);
262 verify(notificationService).deliver(newIssuesNotificationMock);
263 verify(newIssuesNotificationMock).setProject(branch.getKey(), branch.longName(), BRANCH_NAME, null);
264 verify(newIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
265 verify(newIssuesNotificationMock).setStatistics(eq(branch.longName()), any(NewIssuesStatistics.Stats.class));
266 verifyStatistics(context, 1, 0, 0);
270 public void do_not_send_global_new_issues_notification_on_pull_request() {
271 ComponentDto project = newPrivateProjectDto();
272 ComponentDto branch = setUpBranch(project, PULL_REQUEST);
273 protoIssueCache.newAppender().append(
274 createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE))).close();
275 when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
276 analysisMetadataHolder.setProject(Project.from(project));
277 analysisMetadataHolder.setBranch(newPullRequest());
278 analysisMetadataHolder.setPullRequestKey(PULL_REQUEST_ID);
280 TestComputationStepContext context = new TestComputationStepContext();
281 underTest.execute(context);
283 verifyNoInteractions(notificationService, newIssuesNotificationMock);
286 private DefaultIssue createIssue() {
287 return new DefaultIssue().setKey("k").setProjectKey("p").setStatus("OPEN").setProjectUuid("uuid").setComponentKey("c").setRuleKey(RuleKey.of("r", "r"));
291 public void do_not_send_global_new_issues_notification_on_branch_if_issue_has_been_backdated() {
292 ComponentDto project = newPrivateProjectDto();
293 ComponentDto branch = setUpBranch(project, BRANCH);
294 protoIssueCache.newAppender().append(
295 createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS))).close();
296 when(notificationService.hasProjectSubscribersForTypes(branch.uuid(), NOTIF_TYPES)).thenReturn(true);
297 analysisMetadataHolder.setProject(Project.from(project));
298 analysisMetadataHolder.setBranch(newBranch(BranchType.BRANCH));
300 TestComputationStepContext context = new TestComputationStepContext();
301 underTest.execute(context);
303 verify(notificationService, never()).deliver(any(Notification.class));
304 verify(notificationService, never()).deliverEmails(anyCollection());
305 verifyStatistics(context, 0, 0, 0);
309 public void send_new_issues_notification_to_user() {
310 UserDto user = db.users().insertUser();
311 analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
313 protoIssueCache.newAppender().append(
314 createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setAssigneeUuid(user.getUuid()).setCreationDate(new Date(ANALYSE_DATE)))
316 when(notificationService.hasProjectSubscribersForTypes(eq(PROJECT.getUuid()), any())).thenReturn(true);
318 TestComputationStepContext context = new TestComputationStepContext();
319 underTest.execute(context);
321 verify(notificationService).deliverEmails(ImmutableSet.of(newIssuesNotificationMock));
322 verify(notificationService).deliverEmails(ImmutableSet.of(myNewIssuesNotificationMock));
323 // old API compatibility call
324 verify(notificationService).deliver(newIssuesNotificationMock);
325 verify(notificationService).deliver(myNewIssuesNotificationMock);
326 verify(myNewIssuesNotificationMock).setAssignee(any(UserDto.class));
327 verify(myNewIssuesNotificationMock).setProject(PROJECT.getKey(), PROJECT.getName(), null, null);
328 verify(myNewIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
329 verify(myNewIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), any(NewIssuesStatistics.Stats.class));
330 verifyStatistics(context, 1, 1, 0);
334 public void send_new_issues_notification_to_user_only_for_those_assigned_to_her() throws IOException {
335 UserDto perceval = db.users().insertUser(u -> u.setLogin("perceval"));
336 Integer[] assigned = IntStream.range(0, 5).mapToObj(i -> 10_000 * i).toArray(Integer[]::new);
337 Duration expectedEffort = Duration.create(stream(assigned).mapToInt(i -> i).sum());
339 UserDto arthur = db.users().insertUser(u -> u.setLogin("arthur"));
340 Integer[] assignedToOther = IntStream.range(0, 3).mapToObj(i -> 10).toArray(Integer[]::new);
342 List<DefaultIssue> issues = concat(stream(assigned)
343 .map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort))
344 .setAssigneeUuid(perceval.getUuid())
346 .setCreationDate(new Date(ANALYSE_DATE))),
347 stream(assignedToOther)
348 .map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort))
349 .setAssigneeUuid(arthur.getUuid())
351 .setCreationDate(new Date(ANALYSE_DATE))))
354 ProtoIssueCache protoIssueCache = new ProtoIssueCache(temp.newFile(), System2.INSTANCE);
355 DiskCache.CacheAppender newIssueCache = protoIssueCache.newAppender();
356 issues.forEach(newIssueCache::append);
357 newIssueCache.close();
358 analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
359 when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
361 NotificationFactory notificationFactory = mock(NotificationFactory.class);
362 NewIssuesNotification newIssuesNotificationMock = createNewIssuesNotificationMock();
363 when(notificationFactory.newNewIssuesNotification(assigneeCacheCaptor.capture()))
364 .thenReturn(newIssuesNotificationMock);
366 MyNewIssuesNotification myNewIssuesNotificationMock1 = createMyNewIssuesNotificationMock();
367 MyNewIssuesNotification myNewIssuesNotificationMock2 = createMyNewIssuesNotificationMock();
368 doReturn(myNewIssuesNotificationMock1).doReturn(myNewIssuesNotificationMock2).when(notificationFactory).newMyNewIssuesNotification(any(assigneeCacheType));
370 TestComputationStepContext context = new TestComputationStepContext();
371 new SendIssueNotificationsStep(protoIssueCache, treeRootHolder, notificationService, analysisMetadataHolder, notificationFactory, db.getDbClient())
374 verify(notificationService).deliverEmails(ImmutableSet.of(myNewIssuesNotificationMock1, myNewIssuesNotificationMock2));
375 // old API compatibility
376 verify(notificationService).deliver(myNewIssuesNotificationMock1);
377 verify(notificationService).deliver(myNewIssuesNotificationMock2);
379 verify(notificationFactory).newNewIssuesNotification(assigneeCacheCaptor.capture());
380 verify(notificationFactory, times(2)).newMyNewIssuesNotification(assigneeCacheCaptor.capture());
381 verifyNoMoreInteractions(notificationFactory);
382 verifyAssigneeCache(assigneeCacheCaptor, perceval, arthur);
384 Map<String, MyNewIssuesNotification> myNewIssuesNotificationMocksByUsersName = new HashMap<>();
385 ArgumentCaptor<UserDto> userCaptor1 = forClass(UserDto.class);
386 verify(myNewIssuesNotificationMock1).setAssignee(userCaptor1.capture());
387 myNewIssuesNotificationMocksByUsersName.put(userCaptor1.getValue().getLogin(), myNewIssuesNotificationMock1);
389 ArgumentCaptor<UserDto> userCaptor2 = forClass(UserDto.class);
390 verify(myNewIssuesNotificationMock2).setAssignee(userCaptor2.capture());
391 myNewIssuesNotificationMocksByUsersName.put(userCaptor2.getValue().getLogin(), myNewIssuesNotificationMock2);
393 MyNewIssuesNotification myNewIssuesNotificationMock = myNewIssuesNotificationMocksByUsersName.get("perceval");
394 ArgumentCaptor<NewIssuesStatistics.Stats> statsCaptor = forClass(NewIssuesStatistics.Stats.class);
395 verify(myNewIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), statsCaptor.capture());
397 NewIssuesStatistics.Stats stats = statsCaptor.getValue();
398 assertThat(stats.hasIssues()).isTrue();
399 // just checking all issues have been added to the stats
400 assertThat(stats.getIssueCount().getOnCurrentAnalysis()).isEqualTo(assigned.length);
401 assertThat(stats.getIssueCount().getTotal()).isEqualTo(assigned.length);
403 verifyStatistics(context, 1, 2, 0);
407 public void send_new_issues_notification_to_user_only_for_non_backdated_issues() {
408 UserDto user = db.users().insertUser();
409 Random random = new Random();
410 Integer[] efforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10_000 * i).toArray(Integer[]::new);
411 Integer[] backDatedEfforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10 + random.nextInt(100)).toArray(Integer[]::new);
412 Duration expectedEffort = Duration.create(stream(efforts).mapToInt(i -> i).sum());
413 List<DefaultIssue> issues = concat(stream(efforts)
414 .map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort))
415 .setAssigneeUuid(user.getUuid())
416 .setCreationDate(new Date(ANALYSE_DATE))),
417 stream(backDatedEfforts)
418 .map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort))
419 .setAssigneeUuid(user.getUuid())
420 .setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS))))
423 DiskCache.CacheAppender issueCache = this.protoIssueCache.newAppender();
424 issues.forEach(issueCache::append);
426 analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
427 when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
429 TestComputationStepContext context = new TestComputationStepContext();
430 underTest.execute(context);
432 verify(notificationService).deliver(newIssuesNotificationMock);
433 verify(notificationService).deliverEmails(ImmutableSet.of(myNewIssuesNotificationMock));
434 // old API compatibility
435 verify(notificationService).deliver(myNewIssuesNotificationMock);
437 verify(notificationFactory).newNewIssuesNotification(assigneeCacheCaptor.capture());
438 verify(notificationFactory).newMyNewIssuesNotification(assigneeCacheCaptor.capture());
439 verifyNoMoreInteractions(notificationFactory);
440 verifyAssigneeCache(assigneeCacheCaptor, user);
442 verify(myNewIssuesNotificationMock).setAssignee(any(UserDto.class));
443 ArgumentCaptor<NewIssuesStatistics.Stats> statsCaptor = forClass(NewIssuesStatistics.Stats.class);
444 verify(myNewIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), statsCaptor.capture());
445 NewIssuesStatistics.Stats stats = statsCaptor.getValue();
446 assertThat(stats.hasIssues()).isTrue();
447 // just checking all issues have been added to the stats
448 assertThat(stats.getIssueCount().getOnCurrentAnalysis()).isEqualTo(efforts.length);
449 assertThat(stats.getIssueCount().getTotal()).isEqualTo(backDatedEfforts.length + efforts.length);
451 verifyStatistics(context, 1, 1, 0);
454 private static void verifyAssigneeCache(ArgumentCaptor<Map<String, UserDto>> assigneeCacheCaptor, UserDto... users) {
455 Map<String, UserDto> cache = assigneeCacheCaptor.getAllValues().iterator().next();
456 assertThat(assigneeCacheCaptor.getAllValues())
457 .filteredOn(t -> t != cache)
459 Tuple[] expected = stream(users).map(user -> tuple(user.getUuid(), user.getUuid(), user.getUuid(), user.getLogin())).toArray(Tuple[]::new);
460 assertThat(cache.entrySet())
461 .extracting(Map.Entry::getKey, t -> t.getValue().getUuid(), t -> t.getValue().getUuid(), t -> t.getValue().getLogin())
462 .containsOnly(expected);
466 public void do_not_send_new_issues_notification_to_user_if_issue_is_backdated() {
467 analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
468 UserDto user = db.users().insertUser();
469 protoIssueCache.newAppender().append(
470 createIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setAssigneeUuid(user.getUuid())
471 .setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS)))
473 when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
475 TestComputationStepContext context = new TestComputationStepContext();
476 underTest.execute(context);
478 verify(notificationService, never()).deliver(any(Notification.class));
479 verify(notificationService, never()).deliverEmails(anyCollection());
480 verifyStatistics(context, 0, 0, 0);
484 public void send_issues_change_notification() {
485 sendIssueChangeNotification(ANALYSE_DATE);
489 public void do_not_send_new_issues_notifications_for_hotspot() {
490 UserDto user = db.users().insertUser();
491 ComponentDto project = newPrivateProjectDto().setKey(PROJECT.getKey()).setLongName(PROJECT.getName());
492 ComponentDto file = newFileDto(project).setKey(FILE.getKey()).setLongName(FILE.getName());
493 RuleDto ruleDefinitionDto = newRule();
494 prepareIssue(ANALYSE_DATE, user, project, file, ruleDefinitionDto, RuleType.SECURITY_HOTSPOT);
495 analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
496 when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
498 TestComputationStepContext context = new TestComputationStepContext();
499 underTest.execute(context);
501 verify(notificationService, never()).deliver(any(Notification.class));
502 verify(notificationService, never()).deliverEmails(anyCollection());
503 verifyStatistics(context, 0, 0, 0);
507 public void send_issues_change_notification_even_if_issue_is_backdated() {
508 sendIssueChangeNotification(ANALYSE_DATE - FIVE_MINUTES_IN_MS);
511 private void sendIssueChangeNotification(long issueCreatedAt) {
512 UserDto user = db.users().insertUser();
513 ComponentDto project = newPrivateProjectDto().setKey(PROJECT.getKey()).setLongName(PROJECT.getName());
514 analysisMetadataHolder.setProject(Project.from(project));
515 ComponentDto file = newFileDto(project).setKey(FILE.getKey()).setLongName(FILE.getName());
516 treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(project.getKey()).setName(project.longName()).setUuid(project.uuid())
518 builder(Type.FILE, 11).setKey(file.getKey()).setName(file.longName()).build())
520 RuleDto ruleDefinitionDto = newRule();
521 RuleType randomTypeExceptHotspot = RuleType.values()[random.nextInt(RuleType.values().length - 1)];
522 DefaultIssue issue = prepareIssue(issueCreatedAt, user, project, file, ruleDefinitionDto, randomTypeExceptHotspot);
523 IssuesChangesNotification issuesChangesNotification = mock(IssuesChangesNotification.class);
524 when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
525 when(notificationFactory.newIssuesChangesNotification(anySet(), anyMap())).thenReturn(issuesChangesNotification);
527 underTest.execute(new TestComputationStepContext());
529 verify(notificationFactory).newIssuesChangesNotification(issuesSetCaptor.capture(), assigneeByUuidCaptor.capture());
530 assertThat(issuesSetCaptor.getValue()).hasSize(1);
531 assertThat(issuesSetCaptor.getValue().iterator().next()).isEqualTo(issue);
532 assertThat(assigneeByUuidCaptor.getValue()).hasSize(1);
533 assertThat(assigneeByUuidCaptor.getValue().get(user.getUuid())).isNotNull();
534 verify(notificationService).hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES);
535 verify(notificationService).deliverEmails(singleton(issuesChangesNotification));
536 verify(notificationService).deliver(issuesChangesNotification);
537 verifyNoMoreInteractions(notificationService);
540 private DefaultIssue prepareIssue(long issueCreatedAt, UserDto user, ComponentDto project, ComponentDto file, RuleDto ruleDefinitionDto, RuleType type) {
541 DefaultIssue issue = newIssue(ruleDefinitionDto, project, file).setType(type).toDefaultIssue()
542 .setNew(false).setChanged(true).setSendNotifications(true).setCreationDate(new Date(issueCreatedAt)).setAssigneeUuid(user.getUuid());
543 protoIssueCache.newAppender().append(issue).close();
544 when(notificationService.hasProjectSubscribersForTypes(project.branchUuid(), NOTIF_TYPES)).thenReturn(true);
549 public void send_issues_change_notification_on_branch() {
550 sendIssueChangeNotificationOnBranch(ANALYSE_DATE);
554 public void send_issues_change_notification_on_branch_even_if_issue_is_backdated() {
555 sendIssueChangeNotificationOnBranch(ANALYSE_DATE - FIVE_MINUTES_IN_MS);
558 private void sendIssueChangeNotificationOnBranch(long issueCreatedAt) {
559 ComponentDto project = newPrivateProjectDto();
560 ComponentDto branch = newBranchComponent(project, newBranchDto(project).setKey(BRANCH_NAME));
561 ComponentDto file = newFileDto(branch, project.uuid());
562 treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(branch.getKey()).setName(branch.longName()).setUuid(branch.uuid()).addChildren(
563 builder(Type.FILE, 11).setKey(file.getKey()).setName(file.longName()).build()).build());
564 analysisMetadataHolder.setProject(Project.from(project));
565 RuleDto ruleDefinitionDto = newRule();
566 RuleType randomTypeExceptHotspot = RuleType.values()[random.nextInt(RuleType.values().length - 1)];
567 DefaultIssue issue = newIssue(ruleDefinitionDto, branch, file).setType(randomTypeExceptHotspot).toDefaultIssue()
570 .setSendNotifications(true)
571 .setCreationDate(new Date(issueCreatedAt));
572 protoIssueCache.newAppender().append(issue).close();
573 when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
574 IssuesChangesNotification issuesChangesNotification = mock(IssuesChangesNotification.class);
575 when(notificationFactory.newIssuesChangesNotification(anySet(), anyMap())).thenReturn(issuesChangesNotification);
576 analysisMetadataHolder.setBranch(newBranch(BranchType.BRANCH));
578 underTest.execute(new TestComputationStepContext());
580 verify(notificationFactory).newIssuesChangesNotification(issuesSetCaptor.capture(), assigneeByUuidCaptor.capture());
581 assertThat(issuesSetCaptor.getValue()).hasSize(1);
582 assertThat(issuesSetCaptor.getValue().iterator().next()).isEqualTo(issue);
583 assertThat(assigneeByUuidCaptor.getValue()).isEmpty();
584 verify(notificationService).hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES);
585 verify(notificationService).deliverEmails(singleton(issuesChangesNotification));
586 verify(notificationService).deliver(issuesChangesNotification);
587 verifyNoMoreInteractions(notificationService);
591 public void sends_one_issue_change_notification_every_1000_issues() {
592 UserDto user = db.users().insertUser();
593 ComponentDto project = newPrivateProjectDto().setKey(PROJECT.getKey()).setLongName(PROJECT.getName());
594 ComponentDto file = newFileDto(project).setKey(FILE.getKey()).setLongName(FILE.getName());
595 RuleDto ruleDefinitionDto = newRule();
596 RuleType randomTypeExceptHotspot = RuleType.values()[random.nextInt(RuleType.values().length - 1)];
597 List<DefaultIssue> issues = IntStream.range(0, 2001 + new Random().nextInt(10))
598 .mapToObj(i -> newIssue(ruleDefinitionDto, project, file).setKee("uuid_" + i).setType(randomTypeExceptHotspot).toDefaultIssue()
599 .setNew(false).setChanged(true).setSendNotifications(true).setAssigneeUuid(user.getUuid()))
601 DiskCache.CacheAppender cacheAppender = protoIssueCache.newAppender();
602 issues.forEach(cacheAppender::append);
603 cacheAppender.close();
604 analysisMetadataHolder.setProject(Project.from(project));
605 NewIssuesFactoryCaptor newIssuesFactoryCaptor = new NewIssuesFactoryCaptor(() -> mock(IssuesChangesNotification.class));
606 when(notificationFactory.newIssuesChangesNotification(anySet(), anyMap())).thenAnswer(newIssuesFactoryCaptor);
607 when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
608 when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
610 underTest.execute(new TestComputationStepContext());
612 verify(notificationFactory, times(3)).newIssuesChangesNotification(anySet(), anyMap());
613 assertThat(newIssuesFactoryCaptor.issuesSetCaptor).hasSize(3);
614 assertThat(newIssuesFactoryCaptor.issuesSetCaptor.get(0)).hasSize(1000);
615 assertThat(newIssuesFactoryCaptor.issuesSetCaptor.get(1)).hasSize(1000);
616 assertThat(newIssuesFactoryCaptor.issuesSetCaptor.get(2)).hasSize(issues.size() - 2000);
617 assertThat(newIssuesFactoryCaptor.assigneeCacheCaptor)
619 .containsOnly(newIssuesFactoryCaptor.assigneeCacheCaptor.iterator().next());
620 ArgumentCaptor<Collection> collectionCaptor = forClass(Collection.class);
621 verify(notificationService, times(3)).deliverEmails(collectionCaptor.capture());
622 assertThat(collectionCaptor.getAllValues()).hasSize(3);
623 assertThat(collectionCaptor.getAllValues().get(0)).hasSize(1);
624 assertThat(collectionCaptor.getAllValues().get(1)).hasSize(1);
625 assertThat(collectionCaptor.getAllValues().get(2)).hasSize(1);
626 verify(notificationService, times(3)).deliver(any(IssuesChangesNotification.class));
630 * Since the very same Set object is passed to {@link NotificationFactory#newIssuesChangesNotification(Set, Map)} and
631 * reset between each call. We must make a copy of each argument to capture what's been passed to the factory.
632 * This is of course not supported by Mockito's {@link ArgumentCaptor} and we implement this ourselves with a
635 private static class NewIssuesFactoryCaptor implements Answer<Object> {
636 private final Supplier<IssuesChangesNotification> delegate;
637 private final List<Set<DefaultIssue>> issuesSetCaptor = new ArrayList<>();
638 private final List<Map<String, UserDto>> assigneeCacheCaptor = new ArrayList<>();
640 private NewIssuesFactoryCaptor(Supplier<IssuesChangesNotification> delegate) {
641 this.delegate = delegate;
645 public Object answer(InvocationOnMock t) {
646 Set<DefaultIssue> issuesSet = t.getArgument(0);
647 Map<String, UserDto> assigneeCatch = t.getArgument(1);
648 issuesSetCaptor.add(ImmutableSet.copyOf(issuesSet));
649 assigneeCacheCaptor.add(ImmutableMap.copyOf(assigneeCatch));
650 return delegate.get();
654 private NewIssuesNotification createNewIssuesNotificationMock() {
655 NewIssuesNotification notification = mock(NewIssuesNotification.class);
656 when(notification.setProject(any(), any(), any(), any())).thenReturn(notification);
657 when(notification.setProjectVersion(any())).thenReturn(notification);
658 when(notification.setAnalysisDate(any())).thenReturn(notification);
659 when(notification.setStatistics(any(), any())).thenReturn(notification);
663 private MyNewIssuesNotification createMyNewIssuesNotificationMock() {
664 MyNewIssuesNotification notification = mock(MyNewIssuesNotification.class);
665 when(notification.setAssignee(any(UserDto.class))).thenReturn(notification);
666 when(notification.setProject(any(), any(), any(), any())).thenReturn(notification);
667 when(notification.setProjectVersion(any())).thenReturn(notification);
668 when(notification.setAnalysisDate(any())).thenReturn(notification);
669 when(notification.setStatistics(any(), any())).thenReturn(notification);
673 private static Branch newBranch(BranchType type) {
674 Branch branch = mock(Branch.class);
675 when(branch.isMain()).thenReturn(false);
676 when(branch.getName()).thenReturn(BRANCH_NAME);
677 when(branch.getType()).thenReturn(type);
681 private static Branch newPullRequest() {
682 Branch branch = mock(Branch.class);
683 when(branch.isMain()).thenReturn(false);
684 when(branch.getType()).thenReturn(PULL_REQUEST);
685 when(branch.getName()).thenReturn(BRANCH_NAME);
686 when(branch.getPullRequestKey()).thenReturn(PULL_REQUEST_ID);
690 private ComponentDto setUpBranch(ComponentDto project, BranchType branchType) {
691 ComponentDto branch = null;
692 if(branchType == PULL_REQUEST) {
693 branch = newBranchComponent(project, newBranchDto(project, PULL_REQUEST, project.uuid()));
695 branch = newBranchComponent(project, newMainBranchDto(project, project.uuid()).setKey(BRANCH_NAME));
697 ComponentDto file = newFileDto(branch, project.uuid());
698 treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(branch.getKey()).setName(branch.longName()).setUuid(branch.uuid()).addChildren(
699 builder(Type.FILE, 11).setKey(file.getKey()).setName(file.longName()).build()).build());
703 private static void verifyStatistics(TestComputationStepContext context, int expectedNewIssuesNotifications, int expectedMyNewIssuesNotifications,
704 int expectedIssueChangesNotifications) {
705 context.getStatistics().assertValue("newIssuesNotifs", expectedNewIssuesNotifications);
706 context.getStatistics().assertValue("myNewIssuesNotifs", expectedMyNewIssuesNotifications);
707 context.getStatistics().assertValue("changesNotifs", expectedIssueChangesNotifications);
711 protected ComputationStep step() {