-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
SendIssueNotificationsStepTest.java
677 lines (589 loc) · 36.6 KB
/
SendIssueNotificationsStepTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
* SonarQube
* Copyright (C) 2009-2019 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.ce.task.projectanalysis.step;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.assertj.core.groups.Tuple;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentCaptor;
import org.sonar.api.notifications.Notification;
import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.Duration;
import org.sonar.api.utils.System2;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
import org.sonar.ce.task.projectanalysis.analysis.Branch;
import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
import org.sonar.ce.task.projectanalysis.issue.IssueCache;
import org.sonar.ce.task.projectanalysis.issue.RuleRepositoryRule;
import org.sonar.ce.task.projectanalysis.notification.NewIssuesNotificationFactory;
import org.sonar.ce.task.projectanalysis.util.cache.DiskCache;
import org.sonar.ce.task.step.ComputationStep;
import org.sonar.ce.task.step.TestComputationStepContext;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.db.DbTester;
import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.issue.notification.DistributedMetricStatsInt;
import org.sonar.server.issue.notification.IssueChangeNotification;
import org.sonar.server.issue.notification.MyNewIssuesNotification;
import org.sonar.server.issue.notification.NewIssuesNotification;
import org.sonar.server.issue.notification.NewIssuesStatistics;
import org.sonar.server.notification.NotificationService;
import org.sonar.server.project.Project;
import static java.util.Arrays.stream;
import static java.util.Collections.emptyList;
import static java.util.Collections.shuffle;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Stream.concat;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.apache.commons.lang.math.RandomUtils.nextInt;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
import static org.mockito.ArgumentCaptor.forClass;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.sonar.ce.task.projectanalysis.component.Component.Type;
import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
import static org.sonar.ce.task.projectanalysis.step.SendIssueNotificationsStep.NOTIF_TYPES;
import static org.sonar.db.component.BranchType.LONG;
import static org.sonar.db.component.BranchType.PULL_REQUEST;
import static org.sonar.db.component.BranchType.SHORT;
import static org.sonar.db.component.ComponentTesting.newBranchDto;
import static org.sonar.db.component.ComponentTesting.newFileDto;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
import static org.sonar.db.component.ComponentTesting.newProjectBranch;
import static org.sonar.db.issue.IssueTesting.newIssue;
import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
import static org.sonar.db.rule.RuleTesting.newRule;
public class SendIssueNotificationsStepTest extends BaseStepTest {
private static final String BRANCH_NAME = "feature";
private static final String PULL_REQUEST_ID = "pr-123";
private static final long ANALYSE_DATE = 123L;
private static final int FIVE_MINUTES_IN_MS = 1000 * 60 * 5;
private static final Duration ISSUE_DURATION = Duration.create(100L);
private static final Component FILE = builder(Type.FILE, 11).build();
private static final Component PROJECT = builder(Type.PROJECT, 1)
.setProjectVersion(randomAlphanumeric(10))
.addChildren(FILE).build();
@Rule
public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
.setRoot(PROJECT);
@Rule
public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
.setBranch(new DefaultBranchImpl())
.setAnalysisDate(new Date(ANALYSE_DATE));
@Rule
public RuleRepositoryRule ruleRepository = new RuleRepositoryRule();
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
private final Random random = new Random();
private final RuleType[] RULE_TYPES_EXCEPT_HOTSPOTS = Stream.of(RuleType.values()).filter(r -> r != RuleType.SECURITY_HOTSPOT).toArray(RuleType[]::new);
private final RuleType randomRuleType = RULE_TYPES_EXCEPT_HOTSPOTS[random.nextInt(RULE_TYPES_EXCEPT_HOTSPOTS.length)];
@SuppressWarnings("unchecked")
private Class<Map<String, UserDto>> assigneeCacheType = (Class<Map<String, UserDto>>) (Object) Map.class;
private ArgumentCaptor<Map<String, UserDto>> assigneeCacheCaptor = ArgumentCaptor.forClass(assigneeCacheType);
private NotificationService notificationService = mock(NotificationService.class);
private NewIssuesNotificationFactory newIssuesNotificationFactory = mock(NewIssuesNotificationFactory.class);
private NewIssuesNotification newIssuesNotificationMock = createNewIssuesNotificationMock();
private MyNewIssuesNotification myNewIssuesNotificationMock = createMyNewIssuesNotificationMock();
private IssueCache issueCache;
private SendIssueNotificationsStep underTest;
@Before
public void setUp() throws Exception {
issueCache = new IssueCache(temp.newFile(), System2.INSTANCE);
underTest = new SendIssueNotificationsStep(issueCache, ruleRepository, treeRootHolder, notificationService, analysisMetadataHolder,
newIssuesNotificationFactory, db.getDbClient());
when(newIssuesNotificationFactory.newNewIssuesNotification(any(assigneeCacheType))).thenReturn(newIssuesNotificationMock);
when(newIssuesNotificationFactory.newMyNewIssuesNotification(any(assigneeCacheType))).thenReturn(myNewIssuesNotificationMock);
}
@Test
public void do_not_send_notifications_if_no_subscribers() {
analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(false);
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
verify(notificationService, never()).deliver(any(Notification.class));
verify(notificationService, never()).deliverEmails(anyCollection());
verifyStatistics(context, 0, 0, 0);
}
@Test
public void send_global_new_issues_notification() {
analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
issueCache.newAppender().append(
new DefaultIssue().setType(randomRuleType).setEffort(ISSUE_DURATION)
.setCreationDate(new Date(ANALYSE_DATE)))
.close();
when(notificationService.hasProjectSubscribersForTypes(eq(PROJECT.getUuid()), any())).thenReturn(true);
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
verify(notificationService).deliver(newIssuesNotificationMock);
verify(newIssuesNotificationMock).setProject(PROJECT.getKey(), PROJECT.getName(), null, null);
verify(newIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
verify(newIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), any());
verify(newIssuesNotificationMock).setDebt(ISSUE_DURATION);
verifyStatistics(context, 1, 0, 0);
}
@Test
public void send_global_new_issues_notification_only_for_non_backdated_issues() {
Random random = new Random();
Integer[] efforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10_000 * i).toArray(Integer[]::new);
Integer[] backDatedEfforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10 + random.nextInt(100)).toArray(Integer[]::new);
Duration expectedEffort = Duration.create(stream(efforts).mapToInt(i -> i).sum());
List<DefaultIssue> issues = concat(stream(efforts)
.map(effort -> new DefaultIssue().setType(randomRuleType).setEffort(Duration.create(effort))
.setCreationDate(new Date(ANALYSE_DATE))),
stream(backDatedEfforts)
.map(effort -> new DefaultIssue().setType(randomRuleType).setEffort(Duration.create(effort))
.setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS))))
.collect(toList());
shuffle(issues);
DiskCache<DefaultIssue>.DiskAppender issueCache = this.issueCache.newAppender();
issues.forEach(issueCache::append);
analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
verify(notificationService).deliver(newIssuesNotificationMock);
ArgumentCaptor<NewIssuesStatistics.Stats> statsCaptor = forClass(NewIssuesStatistics.Stats.class);
verify(newIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), statsCaptor.capture());
verify(newIssuesNotificationMock).setDebt(expectedEffort);
NewIssuesStatistics.Stats stats = statsCaptor.getValue();
assertThat(stats.hasIssues()).isTrue();
// just checking all issues have been added to the stats
DistributedMetricStatsInt severity = stats.getDistributedMetricStats(NewIssuesStatistics.Metric.RULE_TYPE);
assertThat(severity.getOnLeak()).isEqualTo(efforts.length);
assertThat(severity.getTotal()).isEqualTo(backDatedEfforts.length + efforts.length);
verifyStatistics(context, 1, 0, 0);
}
@Test
public void do_not_send_global_new_issues_notification_if_issue_has_been_backdated() {
analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
issueCache.newAppender().append(
new DefaultIssue().setType(randomRuleType).setEffort(ISSUE_DURATION)
.setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS)))
.close();
when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
verify(notificationService, never()).deliver(any(Notification.class));
verify(notificationService, never()).deliverEmails(anyCollection());
verifyStatistics(context, 0, 0, 0);
}
@Test
public void send_global_new_issues_notification_on_long_branch() {
ComponentDto project = newPrivateProjectDto(newOrganizationDto());
ComponentDto branch = setUpBranch(project, LONG);
issueCache.newAppender().append(
new DefaultIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE))).close();
when(notificationService.hasProjectSubscribersForTypes(branch.uuid(), NOTIF_TYPES)).thenReturn(true);
analysisMetadataHolder.setProject(Project.from(project));
analysisMetadataHolder.setBranch(newBranch(BranchType.LONG));
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
verify(notificationService).deliver(newIssuesNotificationMock);
verify(newIssuesNotificationMock).setProject(branch.getKey(), branch.longName(), BRANCH_NAME, null);
verify(newIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
verify(newIssuesNotificationMock).setStatistics(eq(branch.longName()), any(NewIssuesStatistics.Stats.class));
verify(newIssuesNotificationMock).setDebt(ISSUE_DURATION);
verifyStatistics(context, 1, 0, 0);
}
@Test
public void do_not_send_global_new_issues_notification_on_short_branch() {
ComponentDto project = newPrivateProjectDto(newOrganizationDto());
ComponentDto branch = setUpBranch(project, SHORT);
issueCache.newAppender().append(
new DefaultIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE))).close();
when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
analysisMetadataHolder.setProject(Project.from(project));
analysisMetadataHolder.setBranch(newBranch(SHORT));
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
verifyZeroInteractions(notificationService, newIssuesNotificationMock);
}
@Test
public void do_not_send_global_new_issues_notification_on_pull_request() {
ComponentDto project = newPrivateProjectDto(newOrganizationDto());
ComponentDto branch = setUpBranch(project, PULL_REQUEST);
issueCache.newAppender().append(
new DefaultIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE))).close();
when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
analysisMetadataHolder.setProject(Project.from(project));
analysisMetadataHolder.setBranch(newPullRequest());
analysisMetadataHolder.setPullRequestKey(PULL_REQUEST_ID);
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
verifyZeroInteractions(notificationService, newIssuesNotificationMock);
}
@Test
public void do_not_send_global_new_issues_notification_on_long_branch_if_issue_has_been_backdated() {
ComponentDto project = newPrivateProjectDto(newOrganizationDto());
ComponentDto branch = setUpBranch(project, LONG);
issueCache.newAppender().append(
new DefaultIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS))).close();
when(notificationService.hasProjectSubscribersForTypes(branch.uuid(), NOTIF_TYPES)).thenReturn(true);
analysisMetadataHolder.setProject(Project.from(project));
analysisMetadataHolder.setBranch(newBranch(BranchType.LONG));
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
verify(notificationService, never()).deliver(any(Notification.class));
verify(notificationService, never()).deliverEmails(anyCollection());
verifyStatistics(context, 0, 0, 0);
}
@Test
public void send_new_issues_notification_to_user() {
UserDto user = db.users().insertUser();
analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
issueCache.newAppender().append(
new DefaultIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setAssigneeUuid(user.getUuid())
.setCreationDate(new Date(ANALYSE_DATE)))
.close();
when(notificationService.hasProjectSubscribersForTypes(eq(PROJECT.getUuid()), any())).thenReturn(true);
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
verify(notificationService).deliverEmails(ImmutableSet.of(newIssuesNotificationMock));
verify(notificationService).deliverEmails(ImmutableSet.of(myNewIssuesNotificationMock));
// old API compatibility call
verify(notificationService).deliver(newIssuesNotificationMock);
verify(notificationService).deliver(myNewIssuesNotificationMock);
verify(myNewIssuesNotificationMock).setAssignee(any(UserDto.class));
verify(myNewIssuesNotificationMock).setProject(PROJECT.getKey(), PROJECT.getName(), null, null);
verify(myNewIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE));
verify(myNewIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), any(NewIssuesStatistics.Stats.class));
verify(myNewIssuesNotificationMock).setDebt(ISSUE_DURATION);
verifyStatistics(context, 1, 1, 0);
}
@Test
public void send_new_issues_notification_to_user_only_for_those_assigned_to_her() throws IOException {
UserDto perceval = db.users().insertUser(u -> u.setLogin("perceval"));
Integer[] assigned = IntStream.range(0, 5).mapToObj(i -> 10_000 * i).toArray(Integer[]::new);
Duration expectedEffort = Duration.create(stream(assigned).mapToInt(i -> i).sum());
UserDto arthur = db.users().insertUser(u -> u.setLogin("arthur"));
Integer[] assignedToOther = IntStream.range(0, 3).mapToObj(i -> 10).toArray(Integer[]::new);
List<DefaultIssue> issues = concat(stream(assigned)
.map(effort -> new DefaultIssue().setType(randomRuleType).setEffort(Duration.create(effort))
.setAssigneeUuid(perceval.getUuid())
.setCreationDate(new Date(ANALYSE_DATE))),
stream(assignedToOther)
.map(effort -> new DefaultIssue().setType(randomRuleType).setEffort(Duration.create(effort))
.setAssigneeUuid(arthur.getUuid())
.setCreationDate(new Date(ANALYSE_DATE))))
.collect(toList());
shuffle(issues);
IssueCache issueCache = new IssueCache(temp.newFile(), System2.INSTANCE);
DiskCache<DefaultIssue>.DiskAppender newIssueCache = issueCache.newAppender();
issues.forEach(newIssueCache::append);
analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
NewIssuesNotificationFactory newIssuesNotificationFactory = mock(NewIssuesNotificationFactory.class);
NewIssuesNotification newIssuesNotificationMock = createNewIssuesNotificationMock();
when(newIssuesNotificationFactory.newNewIssuesNotification(assigneeCacheCaptor.capture()))
.thenReturn(newIssuesNotificationMock);
MyNewIssuesNotification myNewIssuesNotificationMock1 = createMyNewIssuesNotificationMock();
MyNewIssuesNotification myNewIssuesNotificationMock2 = createMyNewIssuesNotificationMock();
when(newIssuesNotificationFactory.newMyNewIssuesNotification(any(assigneeCacheType)))
.thenReturn(myNewIssuesNotificationMock1)
.thenReturn(myNewIssuesNotificationMock2);
TestComputationStepContext context = new TestComputationStepContext();
new SendIssueNotificationsStep(issueCache, ruleRepository, treeRootHolder, notificationService, analysisMetadataHolder, newIssuesNotificationFactory, db.getDbClient())
.execute(context);
verify(notificationService).deliverEmails(ImmutableSet.of(myNewIssuesNotificationMock1, myNewIssuesNotificationMock2));
// old API compatibility
verify(notificationService).deliver(myNewIssuesNotificationMock1);
verify(notificationService).deliver(myNewIssuesNotificationMock2);
verify(newIssuesNotificationFactory).newNewIssuesNotification(assigneeCacheCaptor.capture());
verify(newIssuesNotificationFactory, times(2)).newMyNewIssuesNotification(assigneeCacheCaptor.capture());
verifyNoMoreInteractions(newIssuesNotificationFactory);
verifyAssigneeCache(assigneeCacheCaptor, perceval, arthur);
Map<String, MyNewIssuesNotification> myNewIssuesNotificationMocksByUsersName = new HashMap<>();
ArgumentCaptor<UserDto> userCaptor1 = forClass(UserDto.class);
verify(myNewIssuesNotificationMock1).setAssignee(userCaptor1.capture());
myNewIssuesNotificationMocksByUsersName.put(userCaptor1.getValue().getLogin(), myNewIssuesNotificationMock1);
ArgumentCaptor<UserDto> userCaptor2 = forClass(UserDto.class);
verify(myNewIssuesNotificationMock2).setAssignee(userCaptor2.capture());
myNewIssuesNotificationMocksByUsersName.put(userCaptor2.getValue().getLogin(), myNewIssuesNotificationMock2);
MyNewIssuesNotification myNewIssuesNotificationMock = myNewIssuesNotificationMocksByUsersName.get("perceval");
ArgumentCaptor<NewIssuesStatistics.Stats> statsCaptor = forClass(NewIssuesStatistics.Stats.class);
verify(myNewIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), statsCaptor.capture());
verify(myNewIssuesNotificationMock).setDebt(expectedEffort);
NewIssuesStatistics.Stats stats = statsCaptor.getValue();
assertThat(stats.hasIssues()).isTrue();
// just checking all issues have been added to the stats
DistributedMetricStatsInt severity = stats.getDistributedMetricStats(NewIssuesStatistics.Metric.RULE_TYPE);
assertThat(severity.getOnLeak()).isEqualTo(assigned.length);
assertThat(severity.getTotal()).isEqualTo(assigned.length);
verifyStatistics(context, 1, 2, 0);
}
@Test
public void send_new_issues_notification_to_user_only_for_non_backdated_issues() {
UserDto user = db.users().insertUser();
Random random = new Random();
Integer[] efforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10_000 * i).toArray(Integer[]::new);
Integer[] backDatedEfforts = IntStream.range(0, 1 + random.nextInt(10)).mapToObj(i -> 10 + random.nextInt(100)).toArray(Integer[]::new);
Duration expectedEffort = Duration.create(stream(efforts).mapToInt(i -> i).sum());
List<DefaultIssue> issues = concat(stream(efforts)
.map(effort -> new DefaultIssue().setType(randomRuleType).setEffort(Duration.create(effort))
.setAssigneeUuid(user.getUuid())
.setCreationDate(new Date(ANALYSE_DATE))),
stream(backDatedEfforts)
.map(effort -> new DefaultIssue().setType(randomRuleType).setEffort(Duration.create(effort))
.setAssigneeUuid(user.getUuid())
.setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS))))
.collect(toList());
shuffle(issues);
DiskCache<DefaultIssue>.DiskAppender issueCache = this.issueCache.newAppender();
issues.forEach(issueCache::append);
analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
verify(notificationService).deliver(newIssuesNotificationMock);
verify(notificationService).deliverEmails(ImmutableSet.of(myNewIssuesNotificationMock));
// old API compatibility
verify(notificationService).deliver(myNewIssuesNotificationMock);
verify(newIssuesNotificationFactory).newNewIssuesNotification(assigneeCacheCaptor.capture());
verify(newIssuesNotificationFactory).newMyNewIssuesNotification(assigneeCacheCaptor.capture());
verifyNoMoreInteractions(newIssuesNotificationFactory);
verifyAssigneeCache(assigneeCacheCaptor, user);
verify(myNewIssuesNotificationMock).setAssignee(any(UserDto.class));
ArgumentCaptor<NewIssuesStatistics.Stats> statsCaptor = forClass(NewIssuesStatistics.Stats.class);
verify(myNewIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), statsCaptor.capture());
verify(myNewIssuesNotificationMock).setDebt(expectedEffort);
NewIssuesStatistics.Stats stats = statsCaptor.getValue();
assertThat(stats.hasIssues()).isTrue();
// just checking all issues have been added to the stats
DistributedMetricStatsInt severity = stats.getDistributedMetricStats(NewIssuesStatistics.Metric.RULE_TYPE);
assertThat(severity.getOnLeak()).isEqualTo(efforts.length);
assertThat(severity.getTotal()).isEqualTo(backDatedEfforts.length + efforts.length);
verifyStatistics(context, 1, 1, 0);
}
private static void verifyAssigneeCache(ArgumentCaptor<Map<String, UserDto>> assigneeCacheCaptor, UserDto... users) {
Map<String, UserDto> cache = assigneeCacheCaptor.getAllValues().iterator().next();
assertThat(assigneeCacheCaptor.getAllValues())
.filteredOn(t -> t != cache)
.isEmpty();
Tuple[] expected = stream(users).map(user -> tuple(user.getUuid(), user.getUuid(), user.getId(), user.getLogin())).toArray(Tuple[]::new);
assertThat(cache.entrySet())
.extracting(t -> t.getKey(), t -> t.getValue().getUuid(), t -> t.getValue().getId(), t -> t.getValue().getLogin())
.containsOnly(expected);
}
@Test
public void do_not_send_new_issues_notification_to_user_if_issue_is_backdated() {
analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
UserDto user = db.users().insertUser();
issueCache.newAppender().append(
new DefaultIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setAssigneeUuid(user.getUuid())
.setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS)))
.close();
when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
verify(notificationService, never()).deliver(any(Notification.class));
verify(notificationService, never()).deliverEmails(anyCollection());
verifyStatistics(context, 0, 0, 0);
}
@Test
public void send_issues_change_notification() {
sendIssueChangeNotification(ANALYSE_DATE);
}
@Test
public void dont_send_issues_change_notification_for_hotspot() {
UserDto user = db.users().insertUser();
ComponentDto project = newPrivateProjectDto(newOrganizationDto()).setDbKey(PROJECT.getDbKey()).setLongName(PROJECT.getName());
ComponentDto file = newFileDto(project).setDbKey(FILE.getDbKey()).setLongName(FILE.getName());
RuleDefinitionDto ruleDefinitionDto = newRule();
DefaultIssue issue = prepareIssue(ANALYSE_DATE, user, project, file, ruleDefinitionDto, RuleType.SECURITY_HOTSPOT);
analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
TestComputationStepContext context = new TestComputationStepContext();
underTest.execute(context);
verify(notificationService, never()).deliver(any(Notification.class));
verify(notificationService, never()).deliverEmails(anyCollection());
verifyStatistics(context, 0, 0, 0);
}
@Test
public void send_issues_change_notification_even_if_issue_is_backdated() {
sendIssueChangeNotification(ANALYSE_DATE - FIVE_MINUTES_IN_MS);
}
private void sendIssueChangeNotification(long issueCreatedAt) {
UserDto user = db.users().insertUser();
ComponentDto project = newPrivateProjectDto(newOrganizationDto()).setDbKey(PROJECT.getDbKey()).setLongName(PROJECT.getName());
analysisMetadataHolder.setProject(Project.from(project));
ComponentDto file = newFileDto(project).setDbKey(FILE.getDbKey()).setLongName(FILE.getName());
RuleDefinitionDto ruleDefinitionDto = newRule();
RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)];
DefaultIssue issue = prepareIssue(issueCreatedAt, user, project, file, ruleDefinitionDto, randomTypeExceptHotspot);
underTest.execute(new TestComputationStepContext());
ArgumentCaptor<IssueChangeNotification> issueChangeNotificationCaptor = forClass(IssueChangeNotification.class);
verify(notificationService).deliver(issueChangeNotificationCaptor.capture());
IssueChangeNotification issueChangeNotification = issueChangeNotificationCaptor.getValue();
assertThat(issueChangeNotification.getFieldValue("key")).isEqualTo(issue.key());
assertThat(issueChangeNotification.getFieldValue("message")).isEqualTo(issue.message());
assertThat(issueChangeNotification.getFieldValue("ruleName")).isEqualTo(ruleDefinitionDto.getName());
assertThat(issueChangeNotification.getFieldValue("projectName")).isEqualTo(project.longName());
assertThat(issueChangeNotification.getFieldValue("projectKey")).isEqualTo(project.getKey());
assertThat(issueChangeNotification.getFieldValue("componentKey")).isEqualTo(file.getKey());
assertThat(issueChangeNotification.getFieldValue("componentName")).isEqualTo(file.longName());
assertThat(issueChangeNotification.getFieldValue("assignee")).isEqualTo(user.getLogin());
}
private DefaultIssue prepareIssue(long issueCreatedAt, UserDto user, ComponentDto project, ComponentDto file, RuleDefinitionDto ruleDefinitionDto, RuleType type) {
DefaultIssue issue = newIssue(ruleDefinitionDto, project, file).setType(type).toDefaultIssue()
.setNew(false).setChanged(true).setSendNotifications(true).setCreationDate(new Date(issueCreatedAt)).setAssigneeUuid(user.getUuid());
ruleRepository.add(ruleDefinitionDto.getKey()).setName(ruleDefinitionDto.getName());
issueCache.newAppender().append(issue).close();
when(notificationService.hasProjectSubscribersForTypes(project.projectUuid(), NOTIF_TYPES)).thenReturn(true);
return issue;
}
@Test
public void send_issues_change_notification_on_long_branch() {
sendIssueChangeNotificationOnLongBranch(ANALYSE_DATE);
}
@Test
public void send_issues_change_notification_on_long_branch_even_if_issue_is_backdated() {
sendIssueChangeNotificationOnLongBranch(ANALYSE_DATE - FIVE_MINUTES_IN_MS);
}
private void sendIssueChangeNotificationOnLongBranch(long issueCreatedAt) {
ComponentDto project = newPrivateProjectDto(newOrganizationDto());
ComponentDto branch = newProjectBranch(project, newBranchDto(project).setKey(BRANCH_NAME));
ComponentDto file = newFileDto(branch);
treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(branch.getDbKey()).setPublicKey(branch.getKey()).setName(branch.longName()).setUuid(branch.uuid()).addChildren(
builder(Type.FILE, 11).setKey(file.getDbKey()).setPublicKey(file.getKey()).setName(file.longName()).build()).build());
analysisMetadataHolder.setProject(Project.from(project));
RuleDefinitionDto ruleDefinitionDto = newRule();
RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)];
DefaultIssue issue = newIssue(ruleDefinitionDto, branch, file).setType(randomTypeExceptHotspot).toDefaultIssue()
.setNew(false)
.setChanged(true)
.setSendNotifications(true)
.setCreationDate(new Date(issueCreatedAt));
ruleRepository.add(ruleDefinitionDto.getKey()).setName(ruleDefinitionDto.getName());
issueCache.newAppender().append(issue).close();
when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
analysisMetadataHolder.setBranch(newBranch(BranchType.LONG));
underTest.execute(new TestComputationStepContext());
ArgumentCaptor<IssueChangeNotification> issueChangeNotificationCaptor = forClass(IssueChangeNotification.class);
verify(notificationService).deliver(issueChangeNotificationCaptor.capture());
IssueChangeNotification issueChangeNotification = issueChangeNotificationCaptor.getValue();
assertThat(issueChangeNotification.getFieldValue("projectName")).isEqualTo(branch.longName());
assertThat(issueChangeNotification.getFieldValue("projectKey")).isEqualTo(branch.getKey());
assertThat(issueChangeNotification.getFieldValue("branch")).isEqualTo(BRANCH_NAME);
assertThat(issueChangeNotification.getFieldValue("componentKey")).isEqualTo(file.getKey());
assertThat(issueChangeNotification.getFieldValue("componentName")).isEqualTo(file.longName());
}
@Test
public void send_issue_change_notification_in_bulks_of_1000() {
UserDto user = db.users().insertUser();
ComponentDto project = newPrivateProjectDto(newOrganizationDto()).setDbKey(PROJECT.getDbKey()).setLongName(PROJECT.getName());
ComponentDto file = newFileDto(project).setDbKey(FILE.getDbKey()).setLongName(FILE.getName());
RuleDefinitionDto ruleDefinitionDto = newRule();
ruleRepository.add(ruleDefinitionDto.getKey()).setName(ruleDefinitionDto.getName());
RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)];
List<DefaultIssue> issues = IntStream.range(0, 1001 + new Random().nextInt(10))
.mapToObj(i -> newIssue(ruleDefinitionDto, project, file).setType(randomTypeExceptHotspot).toDefaultIssue()
.setNew(false).setChanged(true).setSendNotifications(true).setAssigneeUuid(user.getUuid()))
.collect(toList());
DiskCache<DefaultIssue>.DiskAppender diskAppender = issueCache.newAppender();
issues.forEach(diskAppender::append);
diskAppender.close();
analysisMetadataHolder.setProject(Project.from(project));
when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
underTest.execute(new TestComputationStepContext());
ArgumentCaptor<Collection> collectionCaptor = forClass(Collection.class);
verify(notificationService, times(2)).deliverEmails(collectionCaptor.capture());
verify(notificationService, times(issues.size())).deliver(any(IssueChangeNotification.class));
assertThat(collectionCaptor.getAllValues().get(0)).hasSize(1000);
assertThat(collectionCaptor.getAllValues().get(1)).hasSize(issues.size() - 1000);
}
private NewIssuesNotification createNewIssuesNotificationMock() {
NewIssuesNotification notification = mock(NewIssuesNotification.class);
when(notification.setProject(any(), any(), any(), any())).thenReturn(notification);
when(notification.setProjectVersion(any())).thenReturn(notification);
when(notification.setAnalysisDate(any())).thenReturn(notification);
when(notification.setStatistics(any(), any())).thenReturn(notification);
when(notification.setDebt(any())).thenReturn(notification);
return notification;
}
private MyNewIssuesNotification createMyNewIssuesNotificationMock() {
MyNewIssuesNotification notification = mock(MyNewIssuesNotification.class);
when(notification.setAssignee(any(UserDto.class))).thenReturn(notification);
when(notification.setProject(any(), any(), any(), any())).thenReturn(notification);
when(notification.setProjectVersion(any())).thenReturn(notification);
when(notification.setAnalysisDate(any())).thenReturn(notification);
when(notification.setStatistics(any(), any())).thenReturn(notification);
when(notification.setDebt(any())).thenReturn(notification);
return notification;
}
private static Branch newBranch(BranchType type) {
Branch branch = mock(Branch.class);
when(branch.isMain()).thenReturn(false);
when(branch.getName()).thenReturn(BRANCH_NAME);
when(branch.getType()).thenReturn(type);
return branch;
}
private static Branch newPullRequest() {
Branch branch = mock(Branch.class);
when(branch.isMain()).thenReturn(false);
when(branch.getType()).thenReturn(PULL_REQUEST);
when(branch.getName()).thenReturn(BRANCH_NAME);
when(branch.getPullRequestKey()).thenReturn(PULL_REQUEST_ID);
return branch;
}
private ComponentDto setUpBranch(ComponentDto project, BranchType branchType) {
ComponentDto branch = newProjectBranch(project, newBranchDto(project, branchType).setKey(BRANCH_NAME));
ComponentDto file = newFileDto(branch);
treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(branch.getDbKey()).setPublicKey(branch.getKey()).setName(branch.longName()).setUuid(branch.uuid()).addChildren(
builder(Type.FILE, 11).setKey(file.getDbKey()).setPublicKey(file.getKey()).setName(file.longName()).build()).build());
return branch;
}
private static void verifyStatistics(TestComputationStepContext context, int expectedNewIssuesNotifications, int expectedMyNewIssuesNotifications,
int expectedIssueChangesNotifications) {
context.getStatistics().assertValue("newIssuesNotifs", expectedNewIssuesNotifications);
context.getStatistics().assertValue("myNewIssuesNotifs", expectedMyNewIssuesNotifications);
context.getStatistics().assertValue("changesNotifs", expectedIssueChangesNotifications);
}
@Override
protected ComputationStep step() {
return underTest;
}
}