use of org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest in project sonarqube by SonarSource.
the class EmailNotificationChannelTest method deliverAll_has_no_effect_if_smtp_host_is_null.
@Test
public void deliverAll_has_no_effect_if_smtp_host_is_null() {
EmailSettings emailSettings = mock(EmailSettings.class);
when(emailSettings.getSmtpHost()).thenReturn(null);
Set<EmailDeliveryRequest> requests = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> new EmailDeliveryRequest("foo" + i + "@moo", mock(Notification.class))).collect(toSet());
EmailNotificationChannel underTest = new EmailNotificationChannel(emailSettings, null, null);
int count = underTest.deliverAll(requests);
assertThat(count).isZero();
verify(emailSettings).getSmtpHost();
verifyNoMoreInteractions(emailSettings);
assertThat(smtpServer.getMessages()).isEmpty();
}
use of org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest in project sonarqube by SonarSource.
the class MyNewIssuesNotificationHandlerTest method deliver_ignores_notifications_which_assignee_has_no_subscribed_to_MyNewIssue_notifications.
@Test
public void deliver_ignores_notifications_which_assignee_has_no_subscribed_to_MyNewIssue_notifications() {
String projectKey = randomAlphabetic(5);
String assignee1 = randomAlphabetic(6);
String assignee2 = randomAlphabetic(7);
Set<String> assignees = of(assignee1, assignee2);
// assignee1 is not authorized
Set<MyNewIssuesNotification> assignee1Notifications = randomSetOfNotifications(projectKey, assignee1);
// assignee2 is authorized
Set<MyNewIssuesNotification> assignee2Notifications = randomSetOfNotifications(projectKey, assignee2);
when(emailNotificationChannel.isActivated()).thenReturn(true);
when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER)).thenReturn(of(emailRecipientOf(assignee2)));
Set<EmailDeliveryRequest> expectedRequests = assignee2Notifications.stream().map(t -> new EmailDeliveryRequest(emailOf(t.getAssignee()), t)).collect(toSet());
int deliveredCount = new Random().nextInt(expectedRequests.size());
when(emailNotificationChannel.deliverAll(expectedRequests)).thenReturn(deliveredCount);
int deliver = underTest.deliver(Stream.concat(assignee1Notifications.stream(), assignee2Notifications.stream()).collect(toSet()));
assertThat(deliver).isEqualTo(deliveredCount);
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.email.EmailNotificationChannel.EmailDeliveryRequest in project sonarqube by SonarSource.
the class FPOrWontFixNotificationHandlerTest method deliver_send_a_separated_email_request_for_FPs_and_Wont_Fix_issues.
@Test
@UseDataProvider("oneOrMoreProjectCounts")
public void deliver_send_a_separated_email_request_for_FPs_and_Wont_Fix_issues(int projectCount) {
Set<Project> projects = IntStream.range(0, projectCount).mapToObj(i -> newProject("prk_key_" + i)).collect(toSet());
User subscriber1 = newUser("subscriber1");
User changeAuthor = newUser("changeAuthor");
Set<ChangedIssue> fpIssues = projects.stream().flatMap(project -> randomIssues(t -> t.setProject(project).setNewResolution(RESOLUTION_FALSE_POSITIVE).setAssignee(subscriber1))).collect(toSet());
Set<ChangedIssue> wontFixIssues = projects.stream().flatMap(project -> randomIssues(t -> t.setProject(project).setNewResolution(RESOLUTION_WONT_FIX).setAssignee(subscriber1))).collect(toSet());
UserChange userChange = newUserChange(changeAuthor);
IssuesChangesNotificationBuilder fpAndWontFixNotifications = new IssuesChangesNotificationBuilder(Stream.concat(fpIssues.stream(), wontFixIssues.stream()).collect(toSet()), userChange);
when(emailNotificationChannel.isActivated()).thenReturn(true);
projects.forEach(project -> when(notificationManager.findSubscribedEmailRecipients(DO_NOT_FIX_ISSUE_CHANGE_DISPATCHER_KEY, project.getKey(), ALL_MUST_HAVE_ROLE_USER)).thenReturn(singleton(emailRecipientOf(subscriber1.getLogin()))));
int deliveredCount = new Random().nextInt(200);
when(emailNotificationChannel.deliverAll(anySet())).thenReturn(deliveredCount).thenThrow(new IllegalStateException("deliver should be called only once"));
Set<IssuesChangesNotification> notifications = singleton(serializer.serialize(fpAndWontFixNotifications));
reset(serializer);
int deliver = underTest.deliver(notifications);
assertThat(deliver).isEqualTo(deliveredCount);
projects.forEach(project -> 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(new EmailDeliveryRequest(emailOf(subscriber1.getLogin()), new FPOrWontFixNotification(userChange, wontFixIssues, FpOrWontFix.WONT_FIX)), new EmailDeliveryRequest(emailOf(subscriber1.getLogin()), new FPOrWontFixNotification(userChange, fpIssues, FpOrWontFix.FP)));
}
use of org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest in project sonarqube by SonarSource.
the class ChangesOnMyIssueNotificationHandlerTest method deliver_ignores_issues_which_assignee_is_the_changeAuthor.
@Test
@UseDataProvider("userOrAnalysisChange")
public void deliver_ignores_issues_which_assignee_is_the_changeAuthor(Change userOrAnalysisChange) {
when(emailNotificationChannel.isActivated()).thenReturn(true);
Project project1 = newProject();
Project project2 = newProject();
User assignee1 = newUser("assignee_1");
User assignee2 = newUser("assignee_2");
Set<ChangedIssue> assignee1Issues = IntStream.range(0, 10).mapToObj(i -> newChangedIssue("1_issue_key_" + i, assignee1, project1)).collect(toSet());
Set<ChangedIssue> assignee2Issues = IntStream.range(0, 10).mapToObj(i -> newChangedIssue("2_issue_key_" + i, assignee2, project2)).collect(toSet());
UserChange assignee2Change1 = new UserChange(new Random().nextLong(), assignee2);
Set<IssuesChangesNotification> notifications = Stream.of(// notification from assignee1 with issues from assignee1 only
new IssuesChangesNotificationBuilder(assignee1Issues.stream().limit(4).collect(toSet()), new UserChange(new Random().nextLong(), assignee1)), // notification from assignee2 with issues from assignee1 and assignee2
new IssuesChangesNotificationBuilder(Stream.concat(assignee1Issues.stream().skip(4).limit(2), assignee2Issues.stream().limit(4)).collect(toSet()), assignee2Change1), // notification from assignee2 with issues from assignee2 only
new IssuesChangesNotificationBuilder(assignee2Issues.stream().skip(4).limit(3).collect(toSet()), new UserChange(new Random().nextLong(), assignee2)), // notification from other change with issues from assignee1 and assignee2)
new IssuesChangesNotificationBuilder(Stream.concat(assignee1Issues.stream().skip(6), assignee2Issues.stream().skip(7)).collect(toSet()), userOrAnalysisChange)).map(t -> serializer.serialize(t)).collect(toSet());
when(notificationManager.findSubscribedEmailRecipients(CHANGE_ON_MY_ISSUES_DISPATCHER_KEY, project1.getKey(), ImmutableSet.of(assignee1.getLogin()), ALL_MUST_HAVE_ROLE_USER)).thenReturn(ImmutableSet.of(emailRecipientOf(assignee1.getLogin())));
when(notificationManager.findSubscribedEmailRecipients(CHANGE_ON_MY_ISSUES_DISPATCHER_KEY, project2.getKey(), ImmutableSet.of(assignee2.getLogin()), ALL_MUST_HAVE_ROLE_USER)).thenReturn(ImmutableSet.of(emailRecipientOf(assignee2.getLogin())));
int deliveredCount = new Random().nextInt(100);
when(emailNotificationChannel.deliverAll(anySet())).thenReturn(deliveredCount);
int deliver = underTest.deliver(notifications);
assertThat(deliver).isEqualTo(deliveredCount);
verify(notificationManager).findSubscribedEmailRecipients(CHANGE_ON_MY_ISSUES_DISPATCHER_KEY, project1.getKey(), ImmutableSet.of(assignee1.getLogin()), ALL_MUST_HAVE_ROLE_USER);
verify(notificationManager).findSubscribedEmailRecipients(CHANGE_ON_MY_ISSUES_DISPATCHER_KEY, project2.getKey(), ImmutableSet.of(assignee2.getLogin()), ALL_MUST_HAVE_ROLE_USER);
verifyNoMoreInteractions(notificationManager);
verify(emailNotificationChannel).isActivated();
verify(emailNotificationChannel).deliverAll(emailDeliveryRequestSetCaptor.capture());
verifyNoMoreInteractions(emailNotificationChannel);
Set<EmailDeliveryRequest> emailDeliveryRequests = emailDeliveryRequestSetCaptor.getValue();
assertThat(emailDeliveryRequests).hasSize(3);
ListMultimap<String, EmailDeliveryRequest> emailDeliveryRequestByEmail = emailDeliveryRequests.stream().collect(index(EmailDeliveryRequest::getRecipientEmail));
List<EmailDeliveryRequest> assignee1Requests = emailDeliveryRequestByEmail.get(emailOf(assignee1.getLogin()));
assertThat(assignee1Requests).hasSize(2).extracting(t -> (ChangesOnMyIssuesNotification) t.getNotification()).extracting(ChangesOnMyIssuesNotification::getChange).containsOnly(userOrAnalysisChange, assignee2Change1);
assertThat(assignee1Requests).extracting(t -> (ChangesOnMyIssuesNotification) t.getNotification()).extracting(ChangesOnMyIssuesNotification::getChangedIssues).containsOnly(assignee1Issues.stream().skip(4).limit(2).collect(unorderedIndex(t -> project1, t -> t)), assignee1Issues.stream().skip(6).collect(unorderedIndex(t -> project1, t -> t)));
List<EmailDeliveryRequest> assignee2Requests = emailDeliveryRequestByEmail.get(emailOf(assignee2.getLogin()));
assertThat(assignee2Requests).hasSize(1).extracting(t -> (ChangesOnMyIssuesNotification) t.getNotification()).extracting(ChangesOnMyIssuesNotification::getChange).containsOnly(userOrAnalysisChange);
assertThat(assignee2Requests).extracting(t -> (ChangesOnMyIssuesNotification) t.getNotification()).extracting(ChangesOnMyIssuesNotification::getChangedIssues).containsOnly(assignee2Issues.stream().skip(7).collect(unorderedIndex(t -> project2, t -> t)));
}
Aggregations