From a94a39eb02cc3064d9d13f539fcef01b9879fecc Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 14 Sep 2017 11:19:17 +0200 Subject: [PATCH] SONAR-9725 grammar support for single rule in Builtin QP notification --- ...InQualityProfilesNotificationTemplate.java | 16 +++++- ...alityProfilesNotificationTemplateTest.java | 56 +++++++++++++------ ...uiltInQualityProfilesNotificationTest.java | 4 +- 3 files changed, 53 insertions(+), 23 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 8572d30e8a1..641b18d5ded 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 @@ -69,15 +69,21 @@ public class BuiltInQualityProfilesNotificationTemplate extends EmailTemplate { .append("\n"); int newRules = profile.getNewRules(); if (newRules > 0) { - message.append(" ").append(newRules).append(" new rules\n"); + message.append(" ").append(newRules).append(" new rule") + .append(plural(newRules)) + .append('\n'); } int updatedRules = profile.getUpdatedRules(); if (updatedRules > 0) { - message.append(" ").append(updatedRules).append(" rules have been updated\n"); + message.append(" ").append(updatedRules).append(" rule") + .append(updatedRules > 1 ? "s have been updated" : " has been updated") + .append("\n"); } int removedRules = profile.getRemovedRules(); if (removedRules > 0) { - message.append(" ").append(removedRules).append(" rules removed\n"); + message.append(" ").append(removedRules).append(" rule") + .append(plural(removedRules)) + .append(" removed\n"); } message.append("\n"); }); @@ -92,6 +98,10 @@ public class BuiltInQualityProfilesNotificationTemplate extends EmailTemplate { .setMessage(message.toString()); } + private static String plural(int count) { + return count > 1 ? "s" : ""; + } + public String encode(String text) { try { return URLEncoder.encode(text, UTF_8.name()); 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 c3514e636a2..62d9955f833 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 @@ -63,7 +63,7 @@ public class BuiltInQualityProfilesNotificationTemplateTest { } @Test - public void notification_contains_list_of_new_rules() { + public void notification_contains_count_of_new_rules() { String profileName = newProfileName(); String languageKey = newLanguageKey(); String languageName = newLanguageName(); @@ -77,12 +77,11 @@ public class BuiltInQualityProfilesNotificationTemplateTest { EmailMessage emailMessage = underTest.format(notification.serialize()); - assertMessage(emailMessage, - "\n 2 new rules\n"); + assertMessage(emailMessage, "\n 2 new rules\n"); } @Test - public void notification_contains_list_of_updated_rules() { + public void notification_contains_count_of_updated_rules() { String profileName = newProfileName(); String languageKey = newLanguageKey(); String languageName = newLanguageName(); @@ -96,13 +95,11 @@ public class BuiltInQualityProfilesNotificationTemplateTest { EmailMessage emailMessage = underTest.format(notification.serialize()); - assertMessage(emailMessage, - "\n" + - " 2 rules have been updated\n"); + assertMessage(emailMessage, "\n 2 rules have been updated\n"); } @Test - public void notification_contains_list_of_removed_rules() { + public void notification_contains_count_of_removed_rules() { String profileName = newProfileName(); String languageKey = newLanguageKey(); String languageName = newLanguageName(); @@ -116,9 +113,30 @@ public class BuiltInQualityProfilesNotificationTemplateTest { EmailMessage emailMessage = underTest.format(notification.serialize()); - assertMessage(emailMessage, - "\n" + - " 2 rules removed\n"); + assertMessage(emailMessage, "\n 2 rules removed\n"); + } + + @Test + public void notification_supports_grammar_for_single_rule_added_removed_or_updated() { + String profileName = newProfileName(); + String languageKey = newLanguageKey(); + String languageName = newLanguageName(); + BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification() + .addProfile(Profile.newBuilder() + .setProfileName(profileName) + .setLanguageKey(languageKey) + .setLanguageName(languageName) + .setNewRules(1) + .setUpdatedRules(1) + .setRemovedRules(1) + .build()); + + EmailMessage emailMessage = underTest.format(notification.serialize()); + + assertThat(emailMessage.getMessage()) + .contains("\n 1 new rule\n") + .contains("\n 1 rule has been updated\n") + .contains("\n 1 rule removed\n"); } @Test @@ -139,7 +157,7 @@ public class BuiltInQualityProfilesNotificationTemplateTest { EmailMessage emailMessage = underTest.format(notification.serialize()); assertMessage(emailMessage, - "\n" + + "\n" + " 2 new rules\n" + " 3 rules have been updated\n" + " 4 rules removed\n"); @@ -171,9 +189,9 @@ public class BuiltInQualityProfilesNotificationTemplateTest { assertThat(emailMessage.getMessage()).containsSequence("The following built-in profiles have been updated:\n", profileTitleText(profileName1, languageKey1, languageName1), - " 2 new rules\n", - profileTitleText(profileName2, languageKey2, languageName2), - " 13 new rules\n", + " 2 new rules\n", + profileTitleText(profileName2, languageKey2, languageName2), + " 13 new rules\n", "This is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles"); } @@ -236,9 +254,11 @@ public class BuiltInQualityProfilesNotificationTemplateTest { } private void assertMessage(EmailMessage emailMessage, String expectedProfileDetails) { - assertThat(emailMessage.getMessage()).containsSequence("The following built-in profiles have been updated:\n\n", - expectedProfileDetails, - "\nThis is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles"); + assertThat(emailMessage.getMessage()) + .containsSequence( + "The following built-in profiles have been updated:\n\n", + expectedProfileDetails, + "\nThis is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles"); } private String profileTitleText(String profileName, String languageKey, String languageName) { 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 2ab31f1f1de..f97dd1b7ba2 100644 --- a/tests/src/test/java/org/sonarqube/tests/qualityProfile/BuiltInQualityProfilesNotificationTest.java +++ b/tests/src/test/java/org/sonarqube/tests/qualityProfile/BuiltInQualityProfilesNotificationTest.java @@ -136,9 +136,9 @@ public class BuiltInQualityProfilesNotificationTest { .containsSequence( "The following built-in profiles have been updated:", "\"Basic\" - Foo: " + url + "/profiles/changelog?language=foo&name=Basic&since=", "&to=", - " 1 new rules", + " 1 new rule", " 3 rules have been updated", - " 1 rules removed", + " 1 rule removed", "This is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + url + "/profiles") .isEqualTo(messages.get(1).getMimeMessage().getContent().toString()); } -- 2.39.5