use of org.sonar.server.issue.notification.EmailMessage 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());
}
use of org.sonar.server.issue.notification.EmailMessage in project sonarqube by SonarSource.
the class EmailNotificationChannelTest method shouldNotSendEmailWhenHostnameNotConfigured.
@Test
public void shouldNotSendEmailWhenHostnameNotConfigured() {
EmailMessage emailMessage = new EmailMessage().setTo("user@nowhere").setSubject("Foo").setPlainTextMessage("Bar");
boolean delivered = underTest.deliver(emailMessage);
assertThat(smtpServer.getMessages()).isEmpty();
assertThat(delivered).isFalse();
}
use of org.sonar.server.issue.notification.EmailMessage in project sonarqube by SonarSource.
the class EmailNotificationChannelTest method shouldSendNonThreadedEmail.
@Test
public void shouldSendNonThreadedEmail() throws Exception {
configure();
EmailMessage emailMessage = new EmailMessage().setTo("user@nowhere").setSubject("Foo").setPlainTextMessage("Bar");
boolean delivered = underTest.deliver(emailMessage);
List<WiserMessage> messages = smtpServer.getMessages();
assertThat(messages).hasSize(1);
MimeMessage email = messages.get(0).getMimeMessage();
assertThat(email.getHeader("Content-Type", null)).isEqualTo("text/plain; charset=UTF-8");
assertThat(email.getHeader("In-Reply-To", null)).isNull();
assertThat(email.getHeader("References", null)).isNull();
assertThat(email.getHeader("List-ID", null)).isEqualTo("SonarQube <sonar.nemo.sonarsource.org>");
assertThat(email.getHeader("List-Archive", null)).isEqualTo("http://nemo.sonarsource.org");
assertThat(email.getHeader("From", null)).isEqualTo("SonarQube from NoWhere <server@nowhere>");
assertThat(email.getHeader("To", null)).isEqualTo("<user@nowhere>");
assertThat(email.getHeader("Subject", null)).isEqualTo("[SONARQUBE] Foo");
assertThat((String) email.getContent()).startsWith("Bar");
assertThat(delivered).isTrue();
}
use of org.sonar.server.issue.notification.EmailMessage in project sonarqube by SonarSource.
the class EmailNotificationChannelTest method shouldSendThreadedEmail.
@Test
public void shouldSendThreadedEmail() throws Exception {
configure();
EmailMessage emailMessage = new EmailMessage().setMessageId("reviews/view/1").setFrom("Full Username").setTo("user@nowhere").setSubject("Review #3").setPlainTextMessage("I'll take care of this violation.");
boolean delivered = underTest.deliver(emailMessage);
List<WiserMessage> messages = smtpServer.getMessages();
assertThat(messages).hasSize(1);
MimeMessage email = messages.get(0).getMimeMessage();
assertThat(email.getHeader("Content-Type", null)).isEqualTo("text/plain; charset=UTF-8");
assertThat(email.getHeader("In-Reply-To", null)).isEqualTo("<reviews/view/1@nemo.sonarsource.org>");
assertThat(email.getHeader("References", null)).isEqualTo("<reviews/view/1@nemo.sonarsource.org>");
assertThat(email.getHeader("List-ID", null)).isEqualTo("SonarQube <sonar.nemo.sonarsource.org>");
assertThat(email.getHeader("List-Archive", null)).isEqualTo("http://nemo.sonarsource.org");
assertThat(email.getHeader("From", ",")).isEqualTo("\"Full Username (SonarQube from NoWhere)\" <server@nowhere>");
assertThat(email.getHeader("To", null)).isEqualTo("<user@nowhere>");
assertThat(email.getHeader("Subject", null)).isEqualTo("[SONARQUBE] Review #3");
assertThat((String) email.getContent()).startsWith("I'll take care of this violation.");
assertThat(delivered).isTrue();
}
use of org.sonar.server.issue.notification.EmailMessage in project sonarqube by SonarSource.
the class QGChangeEmailTemplate method format.
@Override
@CheckForNull
public EmailMessage format(Notification notification) {
if (!"alerts".equals(notification.getType())) {
return null;
}
// Retrieve useful values
String projectId = notification.getFieldValue("projectId");
String projectKey = notification.getFieldValue("projectKey");
String projectName = notification.getFieldValue("projectName");
String projectVersion = notification.getFieldValue("projectVersion");
String branchName = notification.getFieldValue("branch");
String alertName = notification.getFieldValue("alertName");
String alertText = notification.getFieldValue("alertText");
String alertLevel = notification.getFieldValue("alertLevel");
String ratingMetricsInOneString = notification.getFieldValue("ratingMetrics");
boolean isNewAlert = Boolean.parseBoolean(notification.getFieldValue("isNewAlert"));
String fullProjectName = computeFullProjectName(projectName, branchName);
// Generate text
String subject = generateSubject(fullProjectName, alertLevel, isNewAlert);
String messageBody = generateMessageBody(projectName, projectKey, projectVersion, branchName, alertName, alertText, isNewAlert, ratingMetricsInOneString);
// And finally return the email that will be sent
return new EmailMessage().setMessageId("alerts/" + projectId).setSubject(subject).setPlainTextMessage(messageBody);
}
Aggregations