Search in sources :

Example 61 with Notification

use of org.sonar.api.notifications.Notification in project sonarqube by SonarSource.

the class AlertsEmailTemplateTest method shouldFormatBackToGreenMessage.

@Test
public void shouldFormatBackToGreenMessage() {
    Notification notification = createNotification("Green (was Red)", "", "OK", "false");
    EmailMessage message = template.format(notification);
    assertThat(message.getMessageId(), is("alerts/45"));
    assertThat(message.getSubject(), is("\"Foo\" is back to green"));
    assertThat(message.getMessage(), is("" + "Project: Foo\n" + "Quality gate status: Green (was Red)\n" + "\n" + "\n" + "See it in SonarQube: http://nemo.sonarsource.org/dashboard/index/org.sonar.foo:foo"));
}
Also used : EmailMessage(org.sonar.plugins.emailnotifications.api.EmailMessage) Notification(org.sonar.api.notifications.Notification) Test(org.junit.Test)

Example 62 with Notification

use of org.sonar.api.notifications.Notification in project sonarqube by SonarSource.

the class AlertsEmailTemplateTest method shouldFormatAlertWithSeveralMessages.

@Test
public void shouldFormatAlertWithSeveralMessages() {
    Notification notification = createNotification("Orange (was Red)", "violations > 4, coverage < 75%", "WARN", "false");
    EmailMessage message = template.format(notification);
    assertThat(message.getMessageId(), is("alerts/45"));
    assertThat(message.getSubject(), is("Quality gate status changed on \"Foo\""));
    assertThat(message.getMessage(), is("" + "Project: Foo\n" + "Quality gate status: Orange (was Red)\n" + "\n" + "Quality gate thresholds:\n" + "  - violations > 4\n" + "  - coverage < 75%\n" + "\n" + "See it in SonarQube: http://nemo.sonarsource.org/dashboard/index/org.sonar.foo:foo"));
}
Also used : EmailMessage(org.sonar.plugins.emailnotifications.api.EmailMessage) Notification(org.sonar.api.notifications.Notification) Test(org.junit.Test)

Example 63 with Notification

use of org.sonar.api.notifications.Notification 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 64 with Notification

use of org.sonar.api.notifications.Notification in project sonarqube by SonarSource.

the class NotificationTest method shouldEqual.

@Test
public void shouldEqual() {
    assertThat(notification.equals("")).isFalse();
    assertThat(notification.equals(null)).isFalse();
    assertThat(notification.equals(notification)).isTrue();
    Notification otherNotif = new Notification("alerts").setDefaultMessage("There are new alerts").setFieldValue("alertCount", "42");
    assertThat(otherNotif).isEqualTo(notification);
    otherNotif = new Notification("alerts").setDefaultMessage("There are new alerts").setFieldValue("alertCount", "15000");
    assertThat(otherNotif).isNotEqualTo(notification);
}
Also used : Notification(org.sonar.api.notifications.Notification) Test(org.junit.Test)

Example 65 with Notification

use of org.sonar.api.notifications.Notification in project sonarqube by SonarSource.

the class NotificationQueueDaoTest method should_findOldest.

@Test
public void should_findOldest() {
    when(system2.now()).thenAnswer(new Answer<Long>() {

        private long counter;

        @Override
        public Long answer(InvocationOnMock invocationOnMock) {
            counter++;
            return counter;
        }
    });
    List<NotificationQueueDto> notifs = IntStream.range(0, 5).mapToObj(i -> toNotificationQueueDto(new Notification("foo_" + i))).collect(toList());
    dao.insert(notifs);
    db.commit();
    assertThat(dao.selectOldest(3)).extracting(NotificationQueueDto::getUuid).containsExactlyElementsOf(Arrays.asList("1", "2", "3"));
}
Also used : IntStream(java.util.stream.IntStream) DbTester(org.sonar.db.DbTester) Arrays(java.util.Arrays) System2(org.sonar.api.utils.System2) Notification(org.sonar.api.notifications.Notification) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Answer(org.mockito.stubbing.Answer) List(java.util.List) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Collectors.toList(java.util.stream.Collectors.toList) NotificationQueueDto.toNotificationQueueDto(org.sonar.db.notification.NotificationQueueDto.toNotificationQueueDto) Rule(org.junit.Rule) Mockito.mock(org.mockito.Mockito.mock) NotificationQueueDto.toNotificationQueueDto(org.sonar.db.notification.NotificationQueueDto.toNotificationQueueDto) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Notification(org.sonar.api.notifications.Notification) Test(org.junit.Test)

Aggregations

Notification (org.sonar.api.notifications.Notification)90 Test (org.junit.Test)83 EmailMessage (org.sonar.plugins.emailnotifications.api.EmailMessage)14 EmailMessage (org.sonar.server.issue.notification.EmailMessage)13 NotificationChannel (org.sonar.api.notifications.NotificationChannel)12 List (java.util.List)7 IntStream (java.util.stream.IntStream)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Mockito.mock (org.mockito.Mockito.mock)5 Mockito.when (org.mockito.Mockito.when)5 ReportAnalysisFailureNotification (org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotification)5 Tuple (org.assertj.core.groups.Tuple)4 Languages (org.sonar.api.resources.Languages)4 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)4 Collections (java.util.Collections)3 Random (java.util.Random)3 Before (org.junit.Before)3 Mockito.verify (org.mockito.Mockito.verify)3 Mockito.verifyNoMoreInteractions (org.mockito.Mockito.verifyNoMoreInteractions)3 Mockito.verifyZeroInteractions (org.mockito.Mockito.verifyZeroInteractions)3