Search in sources :

Example 1 with EmailNotificationChannel

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

the class ReportAnalysisFailureNotificationHandlerTest method deliver_has_no_effect_if_emailNotificationChannel_is_disabled.

@Test
public void deliver_has_no_effect_if_emailNotificationChannel_is_disabled() {
    when(emailNotificationChannel.isActivated()).thenReturn(false);
    Set<ReportAnalysisFailureNotification> notifications = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> mock(ReportAnalysisFailureNotification.class)).collect(toSet());
    int deliver = underTest.deliver(notifications);
    assertThat(deliver).isZero();
    verifyZeroInteractions(notificationManager);
    verify(emailNotificationChannel).isActivated();
    verifyNoMoreInteractions(emailNotificationChannel);
    notifications.forEach(Mockito::verifyZeroInteractions);
}
Also used : IntStream(java.util.stream.IntStream) 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) 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) UserRole(org.sonar.api.web.UserRole) 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) SubscriberPermissionsOnProject(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject) Mockito.mock(org.mockito.Mockito.mock) Random(java.util.Random) Mockito(org.mockito.Mockito) Test(org.junit.Test)

Example 2 with EmailNotificationChannel

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

the class ReportAnalysisFailureNotificationHandlerTest method deliver_has_no_effect_if_no_notification_has_projectKey.

@Test
public void deliver_has_no_effect_if_no_notification_has_projectKey() {
    when(emailNotificationChannel.isActivated()).thenReturn(true);
    Set<ReportAnalysisFailureNotification> notifications = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> newNotification(null)).collect(toSet());
    int deliver = underTest.deliver(notifications);
    assertThat(deliver).isZero();
    verifyZeroInteractions(notificationManager);
    verify(emailNotificationChannel).isActivated();
    verifyNoMoreInteractions(emailNotificationChannel);
    notifications.forEach(notification -> {
        verify(notification).getProjectKey();
        verifyNoMoreInteractions(notification);
    });
}
Also used : IntStream(java.util.stream.IntStream) 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) 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) UserRole(org.sonar.api.web.UserRole) 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) SubscriberPermissionsOnProject(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject) Mockito.mock(org.mockito.Mockito.mock) Random(java.util.Random) Test(org.junit.Test)

Example 3 with EmailNotificationChannel

use of org.sonar.server.notification.email.EmailNotificationChannel 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 4 with EmailNotificationChannel

use of org.sonar.server.notification.email.EmailNotificationChannel 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 5 with EmailNotificationChannel

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

the class MyNewIssuesNotificationHandlerTest method deliver_has_no_effect_if_no_notification_has_projectKey.

@Test
public void deliver_has_no_effect_if_no_notification_has_projectKey() {
    when(emailNotificationChannel.isActivated()).thenReturn(true);
    Set<MyNewIssuesNotification> notifications = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> newNotification(null, null)).collect(toSet());
    int deliver = underTest.deliver(notifications);
    assertThat(deliver).isZero();
    verifyZeroInteractions(notificationManager);
    verify(emailNotificationChannel).isActivated();
    verifyNoMoreInteractions(emailNotificationChannel);
    notifications.forEach(notification -> {
        verify(notification).getProjectKey();
        verifyNoMoreInteractions(notification);
    });
}
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) Test(org.junit.Test)

Aggregations

Random (java.util.Random)34 Set (java.util.Set)34 Collectors.toSet (java.util.stream.Collectors.toSet)34 IntStream (java.util.stream.IntStream)34 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)34 Test (org.junit.Test)34 Mockito (org.mockito.Mockito)34 Mockito.mock (org.mockito.Mockito.mock)34 Mockito.verify (org.mockito.Mockito.verify)34 Mockito.verifyNoMoreInteractions (org.mockito.Mockito.verifyNoMoreInteractions)34 Mockito.verifyZeroInteractions (org.mockito.Mockito.verifyZeroInteractions)34 Mockito.when (org.mockito.Mockito.when)34 EmailNotificationChannel (org.sonar.server.notification.email.EmailNotificationChannel)34 Stream (java.util.stream.Stream)31 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)31 NotificationDispatcherMetadata (org.sonar.server.notification.NotificationDispatcherMetadata)31 GLOBAL_NOTIFICATION (org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION)31 PER_PROJECT_NOTIFICATION (org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION)31 NotificationManager (org.sonar.server.notification.NotificationManager)31 ALL_MUST_HAVE_ROLE_USER (org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER)28