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;
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;
// 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);
}
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();
.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(
}
}
+ 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) {