Search in sources :

Example 1 with EmailDeliveryRequest

use of org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest 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);
}
Also used : IntStream(java.util.stream.IntStream) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) NotificationManager(org.sonar.server.notification.NotificationManager) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet.of(com.google.common.collect.ImmutableSet.of) Collections.emptySet(java.util.Collections.emptySet) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Set(java.util.Set) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) PER_PROJECT_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Stream(java.util.stream.Stream) GLOBAL_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION) NotificationDispatcherMetadata(org.sonar.server.notification.NotificationDispatcherMetadata) EmailNotificationChannel(org.sonar.server.notification.email.EmailNotificationChannel) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) Random(java.util.Random) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Test(org.junit.Test)

Example 2 with EmailDeliveryRequest

use of org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest 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);
}
Also used : IntStream(java.util.stream.IntStream) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) NotificationManager(org.sonar.server.notification.NotificationManager) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet.of(com.google.common.collect.ImmutableSet.of) Collections.emptySet(java.util.Collections.emptySet) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Set(java.util.Set) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) PER_PROJECT_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Stream(java.util.stream.Stream) GLOBAL_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION) NotificationDispatcherMetadata(org.sonar.server.notification.NotificationDispatcherMetadata) EmailNotificationChannel(org.sonar.server.notification.email.EmailNotificationChannel) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) Random(java.util.Random) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Test(org.junit.Test)

Example 3 with EmailDeliveryRequest

use of org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest 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);
}
Also used : IntStream(java.util.stream.IntStream) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) NotificationManager(org.sonar.server.notification.NotificationManager) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet.of(com.google.common.collect.ImmutableSet.of) Collections.emptySet(java.util.Collections.emptySet) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Set(java.util.Set) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) PER_PROJECT_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Stream(java.util.stream.Stream) GLOBAL_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION) NotificationDispatcherMetadata(org.sonar.server.notification.NotificationDispatcherMetadata) EmailNotificationChannel(org.sonar.server.notification.email.EmailNotificationChannel) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) Random(java.util.Random) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Test(org.junit.Test)

Example 4 with EmailDeliveryRequest

use of org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest 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();
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) ListMultimap(com.google.common.collect.ListMultimap) RESOLUTION_WONT_FIX(org.sonar.api.issue.Issue.RESOLUTION_WONT_FIX) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Random(java.util.Random) Mockito.spy(org.mockito.Mockito.spy) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) FpOrWontFix(org.sonar.server.issue.notification.FPOrWontFixNotification.FpOrWontFix) ArgumentCaptor(org.mockito.ArgumentCaptor) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) MoreCollectors.index(org.sonar.core.util.stream.MoreCollectors.index) Collections.singleton(java.util.Collections.singleton) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) RESOLUTION_FALSE_POSITIVE(org.sonar.api.issue.Issue.RESOLUTION_FALSE_POSITIVE) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) Assert.fail(org.junit.Assert.fail) NotificationManager(org.sonar.server.notification.NotificationManager) IssuesChangesNotificationBuilderTesting.newProject(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.newProject) Collectors.toSet(java.util.stream.Collectors.toSet) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) ImmutableSet(com.google.common.collect.ImmutableSet) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Set(java.util.Set) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) PER_PROJECT_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule) Mockito(org.mockito.Mockito) Stream(java.util.stream.Stream) Issue(org.sonar.api.issue.Issue) GLOBAL_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION) Change(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Change) NotificationDispatcherMetadata(org.sonar.server.notification.NotificationDispatcherMetadata) EmailNotificationChannel(org.sonar.server.notification.email.EmailNotificationChannel) Mockito.reset(org.mockito.Mockito.reset) Mockito.mock(org.mockito.Mockito.mock) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) IssuesChangesNotificationBuilderTesting.newProject(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.newProject) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) Random(java.util.Random) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 5 with EmailDeliveryRequest

use of org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest in project sonarqube by SonarSource.

the class ChangesOnMyIssueNotificationHandlerTest method deliver_creates_a_notification_per_assignee_with_only_his_issues_on_the_single_project.

@Test
@UseDataProvider("userOrAnalysisChange")
public void deliver_creates_a_notification_per_assignee_with_only_his_issues_on_the_single_project(Change userOrAnalysisChange) {
    when(emailNotificationChannel.isActivated()).thenReturn(true);
    Project project = 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, project)).collect(toSet());
    Set<ChangedIssue> assignee2Issues = IntStream.range(0, 10).mapToObj(i -> newChangedIssue("2_issue_key_" + i, assignee2, project)).collect(toSet());
    Set<IssuesChangesNotification> notifications = Stream.of(// notification with only assignee1 5 notifications
    new IssuesChangesNotificationBuilder(assignee1Issues.stream().limit(5).collect(toSet()), userOrAnalysisChange), // notification with only assignee2 6 notifications
    new IssuesChangesNotificationBuilder(assignee2Issues.stream().limit(6).collect(toSet()), userOrAnalysisChange), // notification with 4 assignee1 and 3 assignee2 notifications
    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, project.getKey(), ImmutableSet.of(assignee1.getLogin(), assignee2.getLogin()), ALL_MUST_HAVE_ROLE_USER)).thenReturn(ImmutableSet.of(emailRecipientOf(assignee1.getLogin()), 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, project.getKey(), ImmutableSet.of(assignee1.getLogin(), 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(4);
    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);
    assertThat(assignee1Requests).extracting(t -> (ChangesOnMyIssuesNotification) t.getNotification()).extracting(ChangesOnMyIssuesNotification::getChangedIssues).containsOnly(assignee1Issues.stream().limit(5).collect(unorderedIndex(t -> project, t -> t)), assignee1Issues.stream().skip(6).collect(unorderedIndex(t -> project, t -> t)));
    List<EmailDeliveryRequest> assignee2Requests = emailDeliveryRequestByEmail.get(emailOf(assignee2.getLogin()));
    assertThat(assignee2Requests).hasSize(2).extracting(t -> (ChangesOnMyIssuesNotification) t.getNotification()).extracting(ChangesOnMyIssuesNotification::getChange).containsOnly(userOrAnalysisChange);
    assertThat(assignee2Requests).extracting(t -> (ChangesOnMyIssuesNotification) t.getNotification()).extracting(ChangesOnMyIssuesNotification::getChangedIssues).containsOnly(assignee2Issues.stream().limit(6).collect(unorderedIndex(t -> project, t -> t)), assignee2Issues.stream().skip(7).collect(unorderedIndex(t -> project, t -> t)));
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) ListMultimap(com.google.common.collect.ListMultimap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Random(java.util.Random) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) ArgumentCaptor(org.mockito.ArgumentCaptor) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) MoreCollectors.index(org.sonar.core.util.stream.MoreCollectors.index) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) AnalysisChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange) NotificationManager(org.sonar.server.notification.NotificationManager) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) MoreCollectors.unorderedIndex(org.sonar.core.util.stream.MoreCollectors.unorderedIndex) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) ImmutableSet(com.google.common.collect.ImmutableSet) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Set(java.util.Set) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) PER_PROJECT_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION) Mockito.verify(org.mockito.Mockito.verify) IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule) Mockito(org.mockito.Mockito) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule) List(java.util.List) Stream(java.util.stream.Stream) GLOBAL_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION) Change(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Change) NotificationDispatcherMetadata(org.sonar.server.notification.NotificationDispatcherMetadata) EmailNotificationChannel(org.sonar.server.notification.email.EmailNotificationChannel) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) Random(java.util.Random) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

Test (org.junit.Test)14 EmailDeliveryRequest (org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest)14 Random (java.util.Random)13 Set (java.util.Set)13 Collectors.toSet (java.util.stream.Collectors.toSet)13 IntStream (java.util.stream.IntStream)13 Stream (java.util.stream.Stream)13 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 Mockito.mock (org.mockito.Mockito.mock)13 Mockito.verify (org.mockito.Mockito.verify)13 Mockito.verifyNoMoreInteractions (org.mockito.Mockito.verifyNoMoreInteractions)13 Mockito.verifyZeroInteractions (org.mockito.Mockito.verifyZeroInteractions)13 Mockito.when (org.mockito.Mockito.when)13 Collections (java.util.Collections)11 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)10 Mockito (org.mockito.Mockito)10 NotificationDispatcherMetadata (org.sonar.server.notification.NotificationDispatcherMetadata)10 GLOBAL_NOTIFICATION (org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION)10 PER_PROJECT_NOTIFICATION (org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION)10 NotificationManager (org.sonar.server.notification.NotificationManager)10