Search in sources :

Example 6 with EmailDeliveryRequest

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

the class NewIssuesNotificationHandlerTest method deliver_ignores_notification_without_projectKey.

@Test
public void deliver_ignores_notification_without_projectKey() {
    String projectKey = randomAlphabetic(10);
    Set<NewIssuesNotification> withProjectKey = IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> newNotification(projectKey)).collect(toSet());
    Set<NewIssuesNotification> noProjectKey = IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> newNotification(null)).collect(toSet());
    Set<EmailRecipient> emailRecipients = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> "user_" + i).map(login -> new EmailRecipient(login, emailOf(login))).collect(toSet());
    Set<EmailDeliveryRequest> expectedRequests = emailRecipients.stream().flatMap(emailRecipient -> withProjectKey.stream().map(notif -> new EmailDeliveryRequest(emailRecipient.getEmail(), notif))).collect(toSet());
    when(emailNotificationChannel.isActivated()).thenReturn(true);
    when(notificationManager.findSubscribedEmailRecipients(NEW_ISSUES_DISPATCHER_KEY, projectKey, ALL_MUST_HAVE_ROLE_USER)).thenReturn(emailRecipients);
    Set<NewIssuesNotification> notifications = Stream.of(withProjectKey.stream(), noProjectKey.stream()).flatMap(t -> t).collect(toSet());
    int deliver = underTest.deliver(notifications);
    assertThat(deliver).isZero();
    verify(notificationManager).findSubscribedEmailRecipients(NEW_ISSUES_DISPATCHER_KEY, projectKey, 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) 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 7 with EmailDeliveryRequest

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

the class EmailNotificationChannelTest method deliverAll_ignores_multiple_templates_by_notification_and_takes_the_first_one_only.

@Test
public void deliverAll_ignores_multiple_templates_by_notification_and_takes_the_first_one_only() throws MessagingException, IOException {
    String recipientEmail = "foo@donut";
    configure();
    Notification notification1 = mock(Notification.class);
    EmailTemplate template11 = mock(EmailTemplate.class);
    EmailTemplate template12 = mock(EmailTemplate.class);
    EmailMessage emailMessage11 = new EmailMessage().setTo(recipientEmail).setSubject("sub11").setPlainTextMessage("msg11");
    EmailMessage emailMessage12 = new EmailMessage().setTo(recipientEmail).setSubject("sub12").setPlainTextMessage("msg12");
    when(template11.format(notification1)).thenReturn(emailMessage11);
    when(template12.format(notification1)).thenReturn(emailMessage12);
    EmailDeliveryRequest request = new EmailDeliveryRequest(recipientEmail, notification1);
    EmailNotificationChannel underTest = new EmailNotificationChannel(configuration, new EmailTemplate[] { template11, template12 }, null);
    int count = underTest.deliverAll(Collections.singleton(request));
    assertThat(count).isOne();
    assertThat(smtpServer.getMessages()).hasSize(1);
    assertThat((String) smtpServer.getMessages().iterator().next().getMimeMessage().getContent()).contains(emailMessage11.getMessage());
}
Also used : EmailMessage(org.sonar.server.issue.notification.EmailMessage) EmailTemplate(org.sonar.server.issue.notification.EmailTemplate) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Notification(org.sonar.api.notifications.Notification) Test(org.junit.Test)

Example 8 with EmailDeliveryRequest

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

the class ReportAnalysisFailureNotificationHandlerTest method deliver_ignores_notification_without_projectKey.

@Test
public void deliver_ignores_notification_without_projectKey() {
    String projectKey = randomAlphabetic(10);
    Set<ReportAnalysisFailureNotification> withProjectKey = IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> newNotification(projectKey)).collect(toSet());
    Set<ReportAnalysisFailureNotification> noProjectKey = IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> newNotification(null)).collect(toSet());
    Set<EmailRecipient> emailRecipients = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> "user_" + i).map(login -> new EmailRecipient(login, emailOf(login))).collect(toSet());
    Set<EmailDeliveryRequest> expectedRequests = emailRecipients.stream().flatMap(emailRecipient -> withProjectKey.stream().map(notif -> new EmailDeliveryRequest(emailRecipient.getEmail(), notif))).collect(toSet());
    when(emailNotificationChannel.isActivated()).thenReturn(true);
    when(notificationManager.findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey, REQUIRED_SUBSCRIBER_PERMISSIONS)).thenReturn(emailRecipients);
    Set<ReportAnalysisFailureNotification> notifications = Stream.of(withProjectKey.stream(), noProjectKey.stream()).flatMap(t -> t).collect(toSet());
    int deliver = underTest.deliver(notifications);
    assertThat(deliver).isZero();
    verify(notificationManager).findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey, REQUIRED_SUBSCRIBER_PERMISSIONS);
    verifyNoMoreInteractions(notificationManager);
    verify(emailNotificationChannel).isActivated();
    verify(emailNotificationChannel).deliverAll(expectedRequests);
    verifyNoMoreInteractions(emailNotificationChannel);
}
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) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) Random(java.util.Random) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Test(org.junit.Test)

Example 9 with EmailDeliveryRequest

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

the class EmailNotificationChannelTest method deliverAll_returns_count_of_request_for_which_at_least_one_formatter_accept_it.

@Test
public void deliverAll_returns_count_of_request_for_which_at_least_one_formatter_accept_it() throws MessagingException, IOException {
    String recipientEmail = "foo@donut";
    configure();
    Notification notification1 = mock(Notification.class);
    Notification notification2 = mock(Notification.class);
    Notification notification3 = mock(Notification.class);
    EmailTemplate template1 = mock(EmailTemplate.class);
    EmailTemplate template3 = mock(EmailTemplate.class);
    EmailMessage emailMessage1 = new EmailMessage().setTo(recipientEmail).setSubject("sub11").setPlainTextMessage("msg11");
    EmailMessage emailMessage3 = new EmailMessage().setTo(recipientEmail).setSubject("sub3").setPlainTextMessage("msg3");
    when(template1.format(notification1)).thenReturn(emailMessage1);
    when(template3.format(notification3)).thenReturn(emailMessage3);
    Set<EmailDeliveryRequest> requests = Stream.of(notification1, notification2, notification3).map(t -> new EmailDeliveryRequest(recipientEmail, t)).collect(toSet());
    EmailNotificationChannel underTest = new EmailNotificationChannel(configuration, new EmailTemplate[] { template1, template3 }, null);
    int count = underTest.deliverAll(requests);
    assertThat(count).isEqualTo(2);
    assertThat(smtpServer.getMessages()).hasSize(2);
    Map<String, MimeMessage> messagesBySubject = smtpServer.getMessages().stream().map(t -> {
        try {
            return t.getMimeMessage();
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }).collect(toMap(t -> {
        try {
            return t.getSubject();
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }, t -> t));
    assertThat((String) messagesBySubject.get(SUBJECT_PREFIX + " " + emailMessage1.getSubject()).getContent()).contains(emailMessage1.getMessage());
    assertThat((String) messagesBySubject.get(SUBJECT_PREFIX + " " + emailMessage3.getSubject()).getContent()).contains(emailMessage3.getMessage());
}
Also used : IntStream(java.util.stream.IntStream) Assert.fail(junit.framework.Assert.fail) EmailTemplate(org.sonar.server.issue.notification.EmailTemplate) Wiser(org.subethamail.wiser.Wiser) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) MessagingException(javax.mail.MessagingException) Random(java.util.Random) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) EmailSettings(org.sonar.api.config.EmailSettings) Collectors.toMap(java.util.stream.Collectors.toMap) EmailMessage(org.sonar.server.issue.notification.EmailMessage) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Map(java.util.Map) After(org.junit.After) RandomStringUtils.random(org.apache.commons.lang.RandomStringUtils.random) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) Notification(org.sonar.api.notifications.Notification) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Set(java.util.Set) WiserMessage(org.subethamail.wiser.WiserMessage) IOException(java.io.IOException) Test(org.junit.Test) MimeMessage(javax.mail.internet.MimeMessage) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) List(java.util.List) Stream(java.util.stream.Stream) EmailException(org.apache.commons.mail.EmailException) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) EmailMessage(org.sonar.server.issue.notification.EmailMessage) MessagingException(javax.mail.MessagingException) Notification(org.sonar.api.notifications.Notification) MimeMessage(javax.mail.internet.MimeMessage) EmailTemplate(org.sonar.server.issue.notification.EmailTemplate) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Test(org.junit.Test)

Example 10 with EmailDeliveryRequest

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

the class EmailNotificationChannelTest method deliverAll_ignores_requests_which_recipient_is_empty.

@Test
@UseDataProvider("emptyStrings")
public void deliverAll_ignores_requests_which_recipient_is_empty(String emptyString) {
    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(emptyString, 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();
}
Also used : IntStream(java.util.stream.IntStream) Assert.fail(junit.framework.Assert.fail) EmailTemplate(org.sonar.server.issue.notification.EmailTemplate) Wiser(org.subethamail.wiser.Wiser) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) MessagingException(javax.mail.MessagingException) Random(java.util.Random) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) EmailSettings(org.sonar.api.config.EmailSettings) Collectors.toMap(java.util.stream.Collectors.toMap) EmailMessage(org.sonar.server.issue.notification.EmailMessage) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Map(java.util.Map) After(org.junit.After) RandomStringUtils.random(org.apache.commons.lang.RandomStringUtils.random) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) Notification(org.sonar.api.notifications.Notification) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Set(java.util.Set) WiserMessage(org.subethamail.wiser.WiserMessage) IOException(java.io.IOException) Test(org.junit.Test) MimeMessage(javax.mail.internet.MimeMessage) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) List(java.util.List) Stream(java.util.stream.Stream) EmailException(org.apache.commons.mail.EmailException) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) Random(java.util.Random) EmailSettings(org.sonar.api.config.EmailSettings) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Notification(org.sonar.api.notifications.Notification) 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