use of org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER in project sonarqube by SonarSource.
the class MyNewIssuesNotificationHandlerTest method deliver_ignores_notification_without_assignee.
@Test
public void deliver_ignores_notification_without_assignee() {
String projectKey = randomAlphabetic(10);
Set<MyNewIssuesNotification> withAssignee = IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> newNotification(projectKey, randomAlphabetic(11 + i))).collect(toSet());
Set<MyNewIssuesNotification> noAssignee = randomSetOfNotifications(projectKey, null);
Set<MyNewIssuesNotification> noProjectKeyNoAssignee = randomSetOfNotifications(null, null);
Set<EmailRecipient> authorizedRecipients = withAssignee.stream().map(n -> new EmailRecipient(n.getAssignee(), n.getAssignee() + "@foo")).collect(toSet());
Set<EmailDeliveryRequest> expectedRequests = withAssignee.stream().map(n -> new EmailDeliveryRequest(n.getAssignee() + "@foo", n)).collect(toSet());
when(emailNotificationChannel.isActivated()).thenReturn(true);
Set<String> assignees = withAssignee.stream().map(MyNewIssuesNotification::getAssignee).collect(toSet());
when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER)).thenReturn(authorizedRecipients);
Set<MyNewIssuesNotification> notifications = Stream.of(withAssignee.stream(), noAssignee.stream(), noProjectKeyNoAssignee.stream()).flatMap(t -> t).collect(toSet());
int deliver = underTest.deliver(notifications);
assertThat(deliver).isZero();
verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER);
verifyNoMoreInteractions(notificationManager);
verify(emailNotificationChannel).isActivated();
verify(emailNotificationChannel).deliverAll(expectedRequests);
verifyNoMoreInteractions(emailNotificationChannel);
}
use of org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER in project sonarqube by SonarSource.
the class MyNewIssuesNotificationHandlerTest method deliver_returns_sum_of_delivery_counts_when_multiple_projects.
@Test
public void deliver_returns_sum_of_delivery_counts_when_multiple_projects() {
String projectKey1 = randomAlphabetic(5);
String projectKey2 = randomAlphabetic(6);
String projectKey3 = randomAlphabetic(7);
String assignee1 = randomAlphabetic(8);
String assignee2 = randomAlphabetic(9);
String assignee3 = randomAlphabetic(10);
// assignee1 has subscribed to project1 only, no notification on project3
Set<MyNewIssuesNotification> assignee1Project1 = randomSetOfNotifications(projectKey1, assignee1);
Set<MyNewIssuesNotification> assignee1Project2 = randomSetOfNotifications(projectKey2, assignee1);
// assignee2 is subscribed to project1 and project2, notifications on all projects
Set<MyNewIssuesNotification> assignee2Project1 = randomSetOfNotifications(projectKey1, assignee2);
Set<MyNewIssuesNotification> assignee2Project2 = randomSetOfNotifications(projectKey2, assignee2);
Set<MyNewIssuesNotification> assignee2Project3 = randomSetOfNotifications(projectKey3, assignee2);
// assignee3 is subscribed to project2 only, no notification on project1
Set<MyNewIssuesNotification> assignee3Project2 = randomSetOfNotifications(projectKey2, assignee3);
Set<MyNewIssuesNotification> assignee3Project3 = randomSetOfNotifications(projectKey3, assignee3);
when(emailNotificationChannel.isActivated()).thenReturn(true);
when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey1, of(assignee1, assignee2), ALL_MUST_HAVE_ROLE_USER)).thenReturn(of(emailRecipientOf(assignee1), emailRecipientOf(assignee2)));
when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey2, of(assignee1, assignee2, assignee3), ALL_MUST_HAVE_ROLE_USER)).thenReturn(of(emailRecipientOf(assignee2), emailRecipientOf(assignee3)));
when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey3, of(assignee2, assignee3), ALL_MUST_HAVE_ROLE_USER)).thenReturn(emptySet());
Set<EmailDeliveryRequest> expectedRequests = Stream.of(assignee1Project1.stream(), assignee2Project1.stream(), assignee2Project2.stream(), assignee3Project2.stream()).flatMap(t -> t).map(t -> new EmailDeliveryRequest(emailOf(t.getAssignee()), t)).collect(toSet());
int deliveredCount = new Random().nextInt(expectedRequests.size());
when(emailNotificationChannel.deliverAll(expectedRequests)).thenReturn(deliveredCount);
Set<MyNewIssuesNotification> notifications = Stream.of(assignee1Project1.stream(), assignee1Project2.stream(), assignee2Project1.stream(), assignee2Project2.stream(), assignee2Project3.stream(), assignee3Project2.stream(), assignee3Project3.stream()).flatMap(t -> t).collect(toSet());
int deliver = underTest.deliver(notifications);
assertThat(deliver).isEqualTo(deliveredCount);
verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey1, of(assignee1, assignee2), ALL_MUST_HAVE_ROLE_USER);
verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey2, of(assignee1, assignee2, assignee3), ALL_MUST_HAVE_ROLE_USER);
verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey3, of(assignee2, assignee3), ALL_MUST_HAVE_ROLE_USER);
verifyNoMoreInteractions(notificationManager);
verify(emailNotificationChannel).isActivated();
verify(emailNotificationChannel).deliverAll(expectedRequests);
verifyNoMoreInteractions(emailNotificationChannel);
}
use of org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER in project sonarqube by SonarSource.
the class MyNewIssuesNotificationHandlerTest method deliver_ignores_notification_without_projectKey.
@Test
public void deliver_ignores_notification_without_projectKey() {
String projectKey = randomAlphabetic(10);
Set<MyNewIssuesNotification> withProjectKey = IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> newNotification(projectKey, randomAlphabetic(11 + i))).collect(toSet());
Set<MyNewIssuesNotification> noProjectKey = IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> newNotification(null, randomAlphabetic(11 + i))).collect(toSet());
Set<MyNewIssuesNotification> noProjectKeyNoAssignee = randomSetOfNotifications(null, null);
Set<EmailRecipient> authorizedRecipients = withProjectKey.stream().map(n -> new EmailRecipient(n.getAssignee(), n.getAssignee() + "@foo")).collect(toSet());
Set<EmailDeliveryRequest> expectedRequests = withProjectKey.stream().map(n -> new EmailDeliveryRequest(n.getAssignee() + "@foo", n)).collect(toSet());
when(emailNotificationChannel.isActivated()).thenReturn(true);
Set<String> assignees = withProjectKey.stream().map(MyNewIssuesNotification::getAssignee).collect(toSet());
when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER)).thenReturn(authorizedRecipients);
Set<MyNewIssuesNotification> notifications = Stream.of(withProjectKey.stream(), noProjectKey.stream(), noProjectKeyNoAssignee.stream()).flatMap(t -> t).collect(toSet());
int deliver = underTest.deliver(notifications);
assertThat(deliver).isZero();
verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER);
verifyNoMoreInteractions(notificationManager);
verify(emailNotificationChannel).isActivated();
verify(emailNotificationChannel).deliverAll(expectedRequests);
verifyNoMoreInteractions(emailNotificationChannel);
}
use of org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER in project sonarqube by SonarSource.
the class FPOrWontFixNotificationHandlerTest method deliver_does_not_send_email_request_for_notifications_a_subscriber_is_the_changeAuthor_of.
@Test
@UseDataProvider("FPorWontFixResolution")
public void deliver_does_not_send_email_request_for_notifications_a_subscriber_is_the_changeAuthor_of(String newResolution) {
Project project = newProject(randomAlphabetic(5));
User subscriber1 = newUser("subscriber1");
User subscriber2 = newUser("subscriber2");
User subscriber3 = newUser("subscriber3");
User otherChangeAuthor = newUser("otherChangeAuthor");
// subscriber1 is the changeAuthor of some notifications with issues assigned to subscriber1 only
Set<IssuesChangesNotificationBuilder> subscriber1Notifications = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(j -> new IssuesChangesNotificationBuilder(randomIssues(t -> t.setProject(project).setNewResolution(newResolution).setAssignee(subscriber2)).collect(toSet()), newUserChange(subscriber1))).collect(toSet());
// subscriber1 is the changeAuthor of some notifications with issues assigned to subscriber1 and subscriber2
Set<IssuesChangesNotificationBuilder> subscriber1and2Notifications = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(j -> new IssuesChangesNotificationBuilder(Stream.concat(randomIssues(t -> t.setProject(project).setNewResolution(newResolution).setAssignee(subscriber2)), randomIssues(t -> t.setProject(project).setNewResolution(newResolution).setAssignee(subscriber1))).collect(toSet()), newUserChange(subscriber1))).collect(toSet());
// subscriber2 is the changeAuthor of some notifications with issues assigned to subscriber2 only
Set<IssuesChangesNotificationBuilder> subscriber2Notifications = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(j -> new IssuesChangesNotificationBuilder(randomIssues(t -> t.setProject(project).setNewResolution(newResolution).setAssignee(subscriber2)).collect(toSet()), newUserChange(subscriber2))).collect(toSet());
// subscriber2 is the changeAuthor of some notifications with issues assigned to subscriber2 and subscriber 3
Set<IssuesChangesNotificationBuilder> subscriber2And3Notifications = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(j -> new IssuesChangesNotificationBuilder(Stream.concat(randomIssues(t -> t.setProject(project).setNewResolution(newResolution).setAssignee(subscriber2)), randomIssues(t -> t.setProject(project).setNewResolution(newResolution).setAssignee(subscriber3))).collect(toSet()), newUserChange(subscriber2))).collect(toSet());
// subscriber3 is the changeAuthor of no notification
// otherChangeAuthor has some notifications
Set<IssuesChangesNotificationBuilder> otherChangeAuthorNotifications = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(j -> new IssuesChangesNotificationBuilder(randomIssues(t -> t.setProject(project).setNewResolution(newResolution)).collect(toSet()), newUserChange(otherChangeAuthor))).collect(toSet());
when(emailNotificationChannel.isActivated()).thenReturn(true);
Set<String> subscriberLogins = ImmutableSet.of(subscriber1.getLogin(), subscriber2.getLogin(), subscriber3.getLogin());
when(notificationManager.findSubscribedEmailRecipients(DO_NOT_FIX_ISSUE_CHANGE_DISPATCHER_KEY, project.getKey(), ALL_MUST_HAVE_ROLE_USER)).thenReturn(subscriberLogins.stream().map(FPOrWontFixNotificationHandlerTest::emailRecipientOf).collect(toSet()));
int deliveredCount = new Random().nextInt(200);
when(emailNotificationChannel.deliverAll(anySet())).thenReturn(deliveredCount).thenThrow(new IllegalStateException("deliver should be called only once"));
Set<IssuesChangesNotification> notifications = Stream.of(subscriber1Notifications.stream(), subscriber1and2Notifications.stream(), subscriber2Notifications.stream(), subscriber2And3Notifications.stream(), otherChangeAuthorNotifications.stream()).flatMap(t -> t).map(serializer::serialize).collect(toSet());
reset(serializer);
int deliver = underTest.deliver(notifications);
assertThat(deliver).isEqualTo(deliveredCount);
verify(notificationManager).findSubscribedEmailRecipients(DO_NOT_FIX_ISSUE_CHANGE_DISPATCHER_KEY, project.getKey(), ALL_MUST_HAVE_ROLE_USER);
verifyNoMoreInteractions(notificationManager);
verify(emailNotificationChannel).isActivated();
ArgumentCaptor<Set<EmailDeliveryRequest>> captor = ArgumentCaptor.forClass(requestSetType);
verify(emailNotificationChannel).deliverAll(captor.capture());
verifyNoMoreInteractions(emailNotificationChannel);
ListMultimap<String, EmailDeliveryRequest> requestsByRecipientEmail = captor.getValue().stream().collect(index(EmailDeliveryRequest::getRecipientEmail));
assertThat(requestsByRecipientEmail.get(emailOf(subscriber1.getLogin()))).containsOnly(Stream.of(subscriber2Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber1, toFpOrWontFix(newResolution))), subscriber2And3Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber1, toFpOrWontFix(newResolution))), otherChangeAuthorNotifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber1, toFpOrWontFix(newResolution)))).flatMap(t -> t).toArray(EmailDeliveryRequest[]::new));
assertThat(requestsByRecipientEmail.get(emailOf(subscriber2.getLogin()))).containsOnly(Stream.of(subscriber1Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber2, toFpOrWontFix(newResolution))), subscriber1and2Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber2, toFpOrWontFix(newResolution))), otherChangeAuthorNotifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber2, toFpOrWontFix(newResolution)))).flatMap(t -> t).toArray(EmailDeliveryRequest[]::new));
assertThat(requestsByRecipientEmail.get(emailOf(subscriber3.getLogin()))).containsOnly(Stream.of(subscriber1Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber3, toFpOrWontFix(newResolution))), subscriber1and2Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber3, toFpOrWontFix(newResolution))), subscriber2Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber3, toFpOrWontFix(newResolution))), subscriber2And3Notifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber3, toFpOrWontFix(newResolution))), otherChangeAuthorNotifications.stream().map(notif -> newEmailDeliveryRequest(notif, subscriber3, toFpOrWontFix(newResolution)))).flatMap(t -> t).toArray(EmailDeliveryRequest[]::new));
assertThat(requestsByRecipientEmail.get(emailOf(otherChangeAuthor.getLogin()))).isEmpty();
}
use of org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER in project sonarqube by SonarSource.
the class ChangesOnMyIssueNotificationHandlerTest method deliver_checks_by_projectKey_if_notifications_have_subscribed_assignee_to_ChangesOnMyIssues_notifications.
@Test
public void deliver_checks_by_projectKey_if_notifications_have_subscribed_assignee_to_ChangesOnMyIssues_notifications() {
when(emailNotificationChannel.isActivated()).thenReturn(true);
Project project = newProject();
Set<ChangedIssue> issues = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(i -> new ChangedIssue.Builder("issue_key_" + i).setNewStatus("foo").setAssignee(newUser("assignee_" + i)).setRule(newRule()).setProject(project).build()).collect(toSet());
IssuesChangesNotificationBuilder builder = new IssuesChangesNotificationBuilder(issues, new UserChange(new Random().nextLong(), new User("user_uuid", "user_login", null)));
int deliver = underTest.deliver(ImmutableSet.of(serializer.serialize(builder)));
assertThat(deliver).isZero();
Set<String> assigneeLogins = issues.stream().map(i -> i.getAssignee().get().getLogin()).collect(toSet());
verify(notificationManager).findSubscribedEmailRecipients(CHANGE_ON_MY_ISSUES_DISPATCHER_KEY, project.getKey(), assigneeLogins, ALL_MUST_HAVE_ROLE_USER);
verifyNoMoreInteractions(notificationManager);
verify(emailNotificationChannel).isActivated();
verifyNoMoreInteractions(emailNotificationChannel);
}
Aggregations