You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BuiltInQualityProfilesNotificationTemplateTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.qualityprofile;
  21. import java.util.Date;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.sonar.api.platform.Server;
  25. import org.sonar.plugins.emailnotifications.api.EmailMessage;
  26. import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile;
  27. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.when;
  31. import static org.sonar.api.utils.DateUtils.formatDate;
  32. public class BuiltInQualityProfilesNotificationTemplateTest {
  33. private Server server = mock(Server.class);
  34. private BuiltInQualityProfilesNotificationTemplate underTest = new BuiltInQualityProfilesNotificationTemplate(server);
  35. @Before
  36. public void setUp() throws Exception {
  37. when(server.getPublicRootUrl()).thenReturn("http://" + randomAlphanumeric(10));
  38. }
  39. @Test
  40. public void notification_contains_a_subject() {
  41. String profileName = newProfileName();
  42. String languageKey = newLanguageKey();
  43. String languageName = newLanguageName();
  44. BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
  45. .addProfile(Profile.newBuilder()
  46. .setProfileName(profileName)
  47. .setLanguageKey(languageKey)
  48. .setLanguageName(languageName)
  49. .setNewRules(2)
  50. .build());
  51. EmailMessage emailMessage = underTest.format(notification.serialize());
  52. assertThat(emailMessage.getSubject()).isEqualTo("Built-in quality profiles have been updated");
  53. }
  54. @Test
  55. public void notification_contains_count_of_new_rules() {
  56. String profileName = newProfileName();
  57. String languageKey = newLanguageKey();
  58. String languageName = newLanguageName();
  59. BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
  60. .addProfile(Profile.newBuilder()
  61. .setProfileName(profileName)
  62. .setLanguageKey(languageKey)
  63. .setLanguageName(languageName)
  64. .setNewRules(2)
  65. .build());
  66. EmailMessage emailMessage = underTest.format(notification.serialize());
  67. assertMessage(emailMessage, "\n 2 new rules\n");
  68. }
  69. @Test
  70. public void notification_contains_count_of_updated_rules() {
  71. String profileName = newProfileName();
  72. String languageKey = newLanguageKey();
  73. String languageName = newLanguageName();
  74. BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
  75. .addProfile(Profile.newBuilder()
  76. .setProfileName(profileName)
  77. .setLanguageKey(languageKey)
  78. .setLanguageName(languageName)
  79. .setUpdatedRules(2)
  80. .build());
  81. EmailMessage emailMessage = underTest.format(notification.serialize());
  82. assertMessage(emailMessage, "\n 2 rules have been updated\n");
  83. }
  84. @Test
  85. public void notification_contains_count_of_removed_rules() {
  86. String profileName = newProfileName();
  87. String languageKey = newLanguageKey();
  88. String languageName = newLanguageName();
  89. BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
  90. .addProfile(Profile.newBuilder()
  91. .setProfileName(profileName)
  92. .setLanguageKey(languageKey)
  93. .setLanguageName(languageName)
  94. .setRemovedRules(2)
  95. .build());
  96. EmailMessage emailMessage = underTest.format(notification.serialize());
  97. assertMessage(emailMessage, "\n 2 rules removed\n");
  98. }
  99. @Test
  100. public void notification_supports_grammar_for_single_rule_added_removed_or_updated() {
  101. String profileName = newProfileName();
  102. String languageKey = newLanguageKey();
  103. String languageName = newLanguageName();
  104. BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
  105. .addProfile(Profile.newBuilder()
  106. .setProfileName(profileName)
  107. .setLanguageKey(languageKey)
  108. .setLanguageName(languageName)
  109. .setNewRules(1)
  110. .setUpdatedRules(1)
  111. .setRemovedRules(1)
  112. .build());
  113. EmailMessage emailMessage = underTest.format(notification.serialize());
  114. assertThat(emailMessage.getMessage())
  115. .contains("\n 1 new rule\n")
  116. .contains("\n 1 rule has been updated\n")
  117. .contains("\n 1 rule removed\n");
  118. }
  119. @Test
  120. public void notification_contains_list_of_new_updated_and_removed_rules() {
  121. String profileName = newProfileName();
  122. String languageKey = newLanguageKey();
  123. String languageName = newLanguageName();
  124. BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
  125. .addProfile(Profile.newBuilder()
  126. .setProfileName(profileName)
  127. .setLanguageKey(languageKey)
  128. .setLanguageName(languageName)
  129. .setNewRules(2)
  130. .setUpdatedRules(3)
  131. .setRemovedRules(4)
  132. .build());
  133. EmailMessage emailMessage = underTest.format(notification.serialize());
  134. assertMessage(emailMessage,
  135. "\n" +
  136. " 2 new rules\n" +
  137. " 3 rules have been updated\n" +
  138. " 4 rules removed\n");
  139. }
  140. @Test
  141. public void notification_contains_many_profiles() {
  142. String profileName1 = "profile1_" + randomAlphanumeric(20);
  143. String languageKey1 = "langkey1_" + randomAlphanumeric(20);
  144. String languageName1 = "langName1_" + randomAlphanumeric(20);
  145. String profileName2 = "profile2_" + randomAlphanumeric(20);
  146. String languageKey2 = "langkey2_" + randomAlphanumeric(20);
  147. String languageName2 = "langName2_" + randomAlphanumeric(20);
  148. BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
  149. .addProfile(Profile.newBuilder()
  150. .setProfileName(profileName1)
  151. .setLanguageKey(languageKey1)
  152. .setLanguageName(languageName1)
  153. .setNewRules(2)
  154. .build())
  155. .addProfile(Profile.newBuilder()
  156. .setProfileName(profileName2)
  157. .setLanguageKey(languageKey2)
  158. .setLanguageName(languageName2)
  159. .setNewRules(13)
  160. .build());
  161. EmailMessage emailMessage = underTest.format(notification.serialize());
  162. assertThat(emailMessage.getMessage()).containsSubsequence("The following built-in profiles have been updated:\n",
  163. profileTitleText(profileName1, languageKey1, languageName1),
  164. " 2 new rules\n",
  165. profileTitleText(profileName2, languageKey2, languageName2),
  166. " 13 new rules\n",
  167. "This is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles");
  168. }
  169. @Test
  170. public void notification_contains_profiles_sorted_by_language_then_by_profile_name() {
  171. String languageKey1 = "langkey1_" + randomAlphanumeric(20);
  172. String languageName1 = "langName1_" + randomAlphanumeric(20);
  173. String languageKey2 = "langKey2_" + randomAlphanumeric(20);
  174. String languageName2 = "langName2_" + randomAlphanumeric(20);
  175. String profileName1 = "profile1_" + randomAlphanumeric(20);
  176. String profileName2 = "profile2_" + randomAlphanumeric(20);
  177. String profileName3 = "profile3_" + randomAlphanumeric(20);
  178. BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
  179. .addProfile(Profile.newBuilder().setProfileName(profileName3).setLanguageKey(languageKey2).setLanguageName(languageName2).build())
  180. .addProfile(Profile.newBuilder().setProfileName(profileName2).setLanguageKey(languageKey1).setLanguageName(languageName1).build())
  181. .addProfile(Profile.newBuilder().setProfileName(profileName1).setLanguageKey(languageKey2).setLanguageName(languageName2).build());
  182. EmailMessage emailMessage = underTest.format(notification.serialize());
  183. assertThat(emailMessage.getMessage()).containsSubsequence(
  184. "\"" + profileName2 + "\" - " + languageName1,
  185. "\"" + profileName1 + "\" - " + languageName2,
  186. "\"" + profileName3 + "\" - " + languageName2);
  187. }
  188. @Test
  189. public void notification_contains_encoded_profile_name() {
  190. BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
  191. .addProfile(Profile.newBuilder()
  192. .setProfileName("Sonar Way")
  193. .setLanguageKey("java")
  194. .setLanguageName(newLanguageName())
  195. .build());
  196. EmailMessage emailMessage = underTest.format(notification.serialize());
  197. assertThat(emailMessage.getMessage()).contains(server.getPublicRootUrl() + "/profiles/changelog?language=java&name=Sonar+Way");
  198. }
  199. @Test
  200. public void notification_contains_from_and_to_date() {
  201. String profileName = newProfileName();
  202. String languageKey = newLanguageKey();
  203. String languageName = newLanguageName();
  204. long startDate = 1_000_000_000_000L;
  205. long endDate = startDate + 1_100_000_000_000L;
  206. BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
  207. .addProfile(Profile.newBuilder()
  208. .setProfileName(profileName)
  209. .setLanguageKey(languageKey)
  210. .setLanguageName(languageName)
  211. .setStartDate(startDate)
  212. .setEndDate(endDate)
  213. .build());
  214. EmailMessage emailMessage = underTest.format(notification.serialize());
  215. assertMessage(emailMessage,
  216. profileTitleText(profileName, languageKey, languageName, formatDate(new Date(startDate)), formatDate(new Date(endDate))));
  217. }
  218. private void assertMessage(EmailMessage emailMessage, String expectedProfileDetails) {
  219. assertThat(emailMessage.getMessage())
  220. .containsSubsequence(
  221. "The following built-in profiles have been updated:\n\n",
  222. expectedProfileDetails,
  223. "\nThis is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles");
  224. }
  225. private String profileTitleText(String profileName, String languageKey, String languageName) {
  226. return "\"" + profileName + "\" - " + languageName + ": " + server.getPublicRootUrl() + "/profiles/changelog?language=" + languageKey + "&name=" + profileName;
  227. }
  228. private String profileTitleText(String profileName, String languageKey, String languageName, String startDate, String endDate) {
  229. return "\"" + profileName + "\" - " + languageName + ": " + server.getPublicRootUrl() + "/profiles/changelog?language=" + languageKey + "&name=" + profileName +
  230. "&since=" + startDate + "&to=" + endDate + "\n";
  231. }
  232. private static String newProfileName() {
  233. return "profileName_" + randomAlphanumeric(20);
  234. }
  235. private static String newLanguageName() {
  236. return "languageName_" + randomAlphanumeric(20);
  237. }
  238. private static String newLanguageKey() {
  239. return "languageKey_" + randomAlphanumeric(20);
  240. }
  241. }