use of org.sonar.server.issue.notification.DistributedMetricStatsInt in project sonarqube by SonarSource.
the class SendIssueNotificationsStepTest method send_new_issues_notification_to_user_only_for_non_backdated_issues.
@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 -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setAssigneeUuid(user.getUuid()).setCreationDate(new Date(ANALYSE_DATE))), stream(backDatedEfforts).map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setAssigneeUuid(user.getUuid()).setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS)))).collect(toList());
shuffle(issues);
DiskCache.CacheAppender issueCache = this.protoIssueCache.newAppender();
issues.forEach(issueCache::append);
issueCache.close();
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(notificationFactory).newNewIssuesNotification(assigneeCacheCaptor.capture());
verify(notificationFactory).newMyNewIssuesNotification(assigneeCacheCaptor.capture());
verifyNoMoreInteractions(notificationFactory);
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.getOnCurrentAnalysis()).isEqualTo(efforts.length);
assertThat(severity.getTotal()).isEqualTo(backDatedEfforts.length + efforts.length);
verifyStatistics(context, 1, 1, 0);
}
use of org.sonar.server.issue.notification.DistributedMetricStatsInt in project sonarqube by SonarSource.
the class SendIssueNotificationsStepTest method send_new_issues_notification_to_user_only_for_those_assigned_to_her.
@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 -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setAssigneeUuid(perceval.getUuid()).setNew(true).setCreationDate(new Date(ANALYSE_DATE))), stream(assignedToOther).map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setAssigneeUuid(arthur.getUuid()).setNew(true).setCreationDate(new Date(ANALYSE_DATE)))).collect(toList());
shuffle(issues);
ProtoIssueCache protoIssueCache = new ProtoIssueCache(temp.newFile(), System2.INSTANCE);
DiskCache.CacheAppender newIssueCache = protoIssueCache.newAppender();
issues.forEach(newIssueCache::append);
newIssueCache.close();
analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
NotificationFactory notificationFactory = mock(NotificationFactory.class);
NewIssuesNotification newIssuesNotificationMock = createNewIssuesNotificationMock();
when(notificationFactory.newNewIssuesNotification(assigneeCacheCaptor.capture())).thenReturn(newIssuesNotificationMock);
MyNewIssuesNotification myNewIssuesNotificationMock1 = createMyNewIssuesNotificationMock();
MyNewIssuesNotification myNewIssuesNotificationMock2 = createMyNewIssuesNotificationMock();
doReturn(myNewIssuesNotificationMock1).doReturn(myNewIssuesNotificationMock2).when(notificationFactory).newMyNewIssuesNotification(any(assigneeCacheType));
TestComputationStepContext context = new TestComputationStepContext();
new SendIssueNotificationsStep(protoIssueCache, treeRootHolder, notificationService, analysisMetadataHolder, notificationFactory, db.getDbClient()).execute(context);
verify(notificationService).deliverEmails(ImmutableSet.of(myNewIssuesNotificationMock1, myNewIssuesNotificationMock2));
// old API compatibility
verify(notificationService).deliver(myNewIssuesNotificationMock1);
verify(notificationService).deliver(myNewIssuesNotificationMock2);
verify(notificationFactory).newNewIssuesNotification(assigneeCacheCaptor.capture());
verify(notificationFactory, times(2)).newMyNewIssuesNotification(assigneeCacheCaptor.capture());
verifyNoMoreInteractions(notificationFactory);
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.getOnCurrentAnalysis()).isEqualTo(assigned.length);
assertThat(severity.getTotal()).isEqualTo(assigned.length);
verifyStatistics(context, 1, 2, 0);
}
use of org.sonar.server.issue.notification.DistributedMetricStatsInt in project sonarqube by SonarSource.
the class SendIssueNotificationsStepTest method send_global_new_issues_notification_only_for_non_backdated_issues.
@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 -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setCreationDate(new Date(ANALYSE_DATE))), stream(backDatedEfforts).map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS)))).collect(toList());
shuffle(issues);
DiskCache.CacheAppender issueCache = this.protoIssueCache.newAppender();
issues.forEach(issueCache::append);
issueCache.close();
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.getOnCurrentAnalysis()).isEqualTo(efforts.length);
assertThat(severity.getTotal()).isEqualTo(backDatedEfforts.length + efforts.length);
verifyStatistics(context, 1, 0, 0);
}
Aggregations