]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9725 grammar support for single rule in Builtin QP notification
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 14 Sep 2017 09:19:17 +0000 (11:19 +0200)
committerEric Hartmann <hartmann.eric@gmail.Com>
Mon, 2 Oct 2017 11:03:35 +0000 (13:03 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplate.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplateTest.java
tests/src/test/java/org/sonarqube/tests/qualityProfile/BuiltInQualityProfilesNotificationTest.java

index 8572d30e8a154e3651f128deec748ed6573656fc..641b18d5ded9f7880ec5b7ec364d85c9fe610cde 100644 (file)
@@ -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());
index c3514e636a226730ae333cce840b37c7d3aa4b2b..62d9955f83333cad88bba5f8c331706db45781bf 100644 (file)
@@ -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) {
index 2ab31f1f1de61e68d595bfedc635c41dd57b31a3..f97dd1b7ba288d6d82c471b684b5e7cfbe9a8f01 100644 (file)
@@ -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());
   }