use of org.sonar.server.issue.notification.EmailTemplate 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());
}
use of org.sonar.server.issue.notification.EmailTemplate 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());
}
Aggregations