From e58f19cefe30d06275e45ab44e6d01a69a794676 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 23 Jun 2017 09:06:41 +0200 Subject: [PATCH] SONAR-9442 Add a suject in email sent to quality profiles admin --- ...tInQualityProfilesNotificationTemplate.java | 6 +++--- ...ualityProfilesNotificationTemplateTest.java | 18 ++++++++++++++++++ ...BuiltInQualityProfilesNotificationTest.java | 12 ++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplate.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplate.java index 69025332316..e30294c2ada 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplate.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplate.java @@ -21,7 +21,6 @@ package org.sonar.server.qualityprofile; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; import java.util.Comparator; import java.util.Date; import org.sonar.api.notifications.Notification; @@ -29,6 +28,7 @@ import org.sonar.api.platform.Server; import org.sonar.plugins.emailnotifications.api.EmailMessage; import org.sonar.plugins.emailnotifications.api.EmailTemplate; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.sonar.api.utils.DateUtils.formatDate; import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile; import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.parse; @@ -88,13 +88,13 @@ public class BuiltInQualityProfilesNotificationTemplate extends EmailTemplate { // And finally return the email that will be sent return new EmailMessage() .setMessageId(BUILT_IN_QUALITY_PROFILES) - .setSubject("empty") + .setSubject("Built-in quality profiles have been updated") .setMessage(message.toString()); } public String encode(String text) { try { - return URLEncoder.encode(text, StandardCharsets.UTF_8.name()); + return URLEncoder.encode(text, UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(String.format("Cannot encode %s", text), e); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplateTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplateTest.java index 89f023a1645..176a22aa7e7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplateTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplateTest.java @@ -44,6 +44,24 @@ public class BuiltInQualityProfilesNotificationTemplateTest { when(server.getPublicRootUrl()).thenReturn("http://" + randomAlphanumeric(10)); } + @Test + public void notification_contains_a_subject() { + String profileName = newProfileName(); + String languageKey = newLanguageKey(); + String languageName = newLanguageName(); + BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification() + .addProfile(Profile.newBuilder() + .setProfileName(profileName) + .setLanguageKey(languageKey) + .setLanguageName(languageName) + .setNewRules(2) + .build()); + + EmailMessage emailMessage = underTest.format(notification.serialize()); + + assertThat(emailMessage.getSubject()).isEqualTo("Built-in quality profiles have been updated"); + } + @Test public void notification_contains_list_of_new_rules() { String profileName = newProfileName(); diff --git a/tests/src/test/java/org/sonarqube/tests/qualityProfile/BuiltInQualityProfilesNotificationTest.java b/tests/src/test/java/org/sonarqube/tests/qualityProfile/BuiltInQualityProfilesNotificationTest.java index a0d8d90d368..d11f252ddad 100644 --- a/tests/src/test/java/org/sonarqube/tests/qualityProfile/BuiltInQualityProfilesNotificationTest.java +++ b/tests/src/test/java/org/sonarqube/tests/qualityProfile/BuiltInQualityProfilesNotificationTest.java @@ -131,6 +131,10 @@ public class BuiltInQualityProfilesNotificationTest { .extracting(this::getMimeMessage) .extracting(this::getAllRecipients) .containsOnly("<" + profileAdmin1.getEmail() + ">", "<" + profileAdmin2.getEmail() + ">"); + assertThat(messages) + .extracting(this::getMimeMessage) + .extracting(this::getSubject) + .containsOnly("[SONARQUBE] Built-in quality profiles have been updated"); String url = orchestrator.getServer().getUrl(); assertThat(messages.get(0).getMimeMessage().getContent().toString()) .containsSequence( @@ -196,6 +200,14 @@ public class BuiltInQualityProfilesNotificationTest { } } + private String getSubject(MimeMessage mimeMessage) { + try { + return mimeMessage.getSubject(); + } catch (MessagingException e) { + throw new RuntimeException(e); + } + } + private static void waitUntilAllNotificationsAreDelivered(int expectedNumberOfEmails, int pollNumber, int pollMillis) throws InterruptedException { for (int i = 0; i < pollNumber; i++) { if (smtpServer.getMessages().size() == expectedNumberOfEmails) { -- 2.39.5