3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.qualityprofile.builtin;
22 import java.util.Date;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.api.platform.Server;
26 import org.sonar.server.issue.notification.EmailMessage;
27 import org.sonar.server.qualityprofile.builtin.BuiltInQPChangeNotificationBuilder.Profile;
29 import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33 import static org.sonar.api.utils.DateUtils.formatDate;
35 public class BuiltInQPChangeNotificationTemplateTest {
37 private Server server = mock(Server.class);
39 private BuiltInQPChangeNotificationTemplate underTest = new BuiltInQPChangeNotificationTemplate(server);
43 when(server.getPublicRootUrl()).thenReturn("http://" + randomAlphanumeric(10));
47 public void notification_contains_a_subject() {
48 String profileName = newProfileName();
49 String languageKey = newLanguageKey();
50 String languageName = newLanguageName();
51 BuiltInQPChangeNotificationBuilder notification = new BuiltInQPChangeNotificationBuilder()
52 .addProfile(Profile.newBuilder()
53 .setProfileName(profileName)
54 .setLanguageKey(languageKey)
55 .setLanguageName(languageName)
59 EmailMessage emailMessage = underTest.format(notification.build());
61 assertThat(emailMessage.getSubject()).isEqualTo("Built-in quality profiles have been updated");
65 public void notification_contains_count_of_new_rules() {
66 String profileName = newProfileName();
67 String languageKey = newLanguageKey();
68 String languageName = newLanguageName();
69 BuiltInQPChangeNotificationBuilder notification = new BuiltInQPChangeNotificationBuilder()
70 .addProfile(Profile.newBuilder()
71 .setProfileName(profileName)
72 .setLanguageKey(languageKey)
73 .setLanguageName(languageName)
77 EmailMessage emailMessage = underTest.format(notification.build());
79 assertMessage(emailMessage, "\n 2 new rules\n");
83 public void notification_contains_count_of_updated_rules() {
84 String profileName = newProfileName();
85 String languageKey = newLanguageKey();
86 String languageName = newLanguageName();
87 BuiltInQPChangeNotificationBuilder notification = new BuiltInQPChangeNotificationBuilder()
88 .addProfile(Profile.newBuilder()
89 .setProfileName(profileName)
90 .setLanguageKey(languageKey)
91 .setLanguageName(languageName)
95 EmailMessage emailMessage = underTest.format(notification.build());
97 assertMessage(emailMessage, "\n 2 rules have been updated\n");
101 public void notification_contains_count_of_removed_rules() {
102 String profileName = newProfileName();
103 String languageKey = newLanguageKey();
104 String languageName = newLanguageName();
105 BuiltInQPChangeNotificationBuilder notification = new BuiltInQPChangeNotificationBuilder()
106 .addProfile(Profile.newBuilder()
107 .setProfileName(profileName)
108 .setLanguageKey(languageKey)
109 .setLanguageName(languageName)
113 EmailMessage emailMessage = underTest.format(notification.build());
115 assertMessage(emailMessage, "\n 2 rules removed\n");
119 public void notification_supports_grammar_for_single_rule_added_removed_or_updated() {
120 String profileName = newProfileName();
121 String languageKey = newLanguageKey();
122 String languageName = newLanguageName();
123 BuiltInQPChangeNotificationBuilder notification = new BuiltInQPChangeNotificationBuilder()
124 .addProfile(Profile.newBuilder()
125 .setProfileName(profileName)
126 .setLanguageKey(languageKey)
127 .setLanguageName(languageName)
133 EmailMessage emailMessage = underTest.format(notification.build());
135 assertThat(emailMessage.getMessage())
136 .contains("\n 1 new rule\n")
137 .contains("\n 1 rule has been updated\n")
138 .contains("\n 1 rule removed\n");
142 public void notification_contains_list_of_new_updated_and_removed_rules() {
143 String profileName = newProfileName();
144 String languageKey = newLanguageKey();
145 String languageName = newLanguageName();
146 BuiltInQPChangeNotificationBuilder notification = new BuiltInQPChangeNotificationBuilder()
147 .addProfile(Profile.newBuilder()
148 .setProfileName(profileName)
149 .setLanguageKey(languageKey)
150 .setLanguageName(languageName)
156 EmailMessage emailMessage = underTest.format(notification.build());
158 assertMessage(emailMessage,
161 " 3 rules have been updated\n" +
162 " 4 rules removed\n");
166 public void notification_contains_many_profiles() {
167 String profileName1 = "profile1_" + randomAlphanumeric(20);
168 String languageKey1 = "langkey1_" + randomAlphanumeric(20);
169 String languageName1 = "langName1_" + randomAlphanumeric(20);
170 String profileName2 = "profile2_" + randomAlphanumeric(20);
171 String languageKey2 = "langkey2_" + randomAlphanumeric(20);
172 String languageName2 = "langName2_" + randomAlphanumeric(20);
173 BuiltInQPChangeNotificationBuilder notification = new BuiltInQPChangeNotificationBuilder()
174 .addProfile(Profile.newBuilder()
175 .setProfileName(profileName1)
176 .setLanguageKey(languageKey1)
177 .setLanguageName(languageName1)
180 .addProfile(Profile.newBuilder()
181 .setProfileName(profileName2)
182 .setLanguageKey(languageKey2)
183 .setLanguageName(languageName2)
187 EmailMessage emailMessage = underTest.format(notification.build());
189 assertThat(emailMessage.getMessage()).containsSubsequence("The following built-in profiles have been updated:\n",
190 profileTitleText(profileName1, languageKey1, languageName1),
192 profileTitleText(profileName2, languageKey2, languageName2),
194 "This is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles");
198 public void notification_contains_profiles_sorted_by_language_then_by_profile_name() {
199 String languageKey1 = "langkey1_" + randomAlphanumeric(20);
200 String languageName1 = "langName1_" + randomAlphanumeric(20);
201 String languageKey2 = "langKey2_" + randomAlphanumeric(20);
202 String languageName2 = "langName2_" + randomAlphanumeric(20);
203 String profileName1 = "profile1_" + randomAlphanumeric(20);
204 String profileName2 = "profile2_" + randomAlphanumeric(20);
205 String profileName3 = "profile3_" + randomAlphanumeric(20);
206 BuiltInQPChangeNotificationBuilder notification = new BuiltInQPChangeNotificationBuilder()
207 .addProfile(Profile.newBuilder().setProfileName(profileName3).setLanguageKey(languageKey2).setLanguageName(languageName2).build())
208 .addProfile(Profile.newBuilder().setProfileName(profileName2).setLanguageKey(languageKey1).setLanguageName(languageName1).build())
209 .addProfile(Profile.newBuilder().setProfileName(profileName1).setLanguageKey(languageKey2).setLanguageName(languageName2).build());
211 EmailMessage emailMessage = underTest.format(notification.build());
213 assertThat(emailMessage.getMessage()).containsSubsequence(
214 "\"" + profileName2 + "\" - " + languageName1,
215 "\"" + profileName1 + "\" - " + languageName2,
216 "\"" + profileName3 + "\" - " + languageName2);
220 public void notification_contains_encoded_profile_name() {
221 BuiltInQPChangeNotificationBuilder notification = new BuiltInQPChangeNotificationBuilder()
222 .addProfile(Profile.newBuilder()
223 .setProfileName("Sonar Way")
224 .setLanguageKey("java")
225 .setLanguageName(newLanguageName())
228 EmailMessage emailMessage = underTest.format(notification.build());
230 assertThat(emailMessage.getMessage()).contains(server.getPublicRootUrl() + "/profiles/changelog?language=java&name=Sonar+Way");
234 public void notification_contains_from_and_to_date() {
235 String profileName = newProfileName();
236 String languageKey = newLanguageKey();
237 String languageName = newLanguageName();
238 long startDate = 1_000_000_000_000L;
239 long endDate = startDate + 1_100_000_000_000L;
240 BuiltInQPChangeNotificationBuilder notification = new BuiltInQPChangeNotificationBuilder()
241 .addProfile(Profile.newBuilder()
242 .setProfileName(profileName)
243 .setLanguageKey(languageKey)
244 .setLanguageName(languageName)
245 .setStartDate(startDate)
249 EmailMessage emailMessage = underTest.format(notification.build());
251 assertMessage(emailMessage,
252 profileTitleText(profileName, languageKey, languageName, formatDate(new Date(startDate)), formatDate(new Date(endDate))));
255 private void assertMessage(EmailMessage emailMessage, String expectedProfileDetails) {
256 assertThat(emailMessage.getMessage())
257 .containsSubsequence(
258 "The following built-in profiles have been updated:\n\n",
259 expectedProfileDetails,
260 "\nThis is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles");
263 private String profileTitleText(String profileName, String languageKey, String languageName) {
264 return "\"" + profileName + "\" - " + languageName + ": " + server.getPublicRootUrl() + "/profiles/changelog?language=" + languageKey + "&name=" + profileName;
267 private String profileTitleText(String profileName, String languageKey, String languageName, String startDate, String endDate) {
268 return "\"" + profileName + "\" - " + languageName + ": " + server.getPublicRootUrl() + "/profiles/changelog?language=" + languageKey + "&name=" + profileName +
269 "&since=" + startDate + "&to=" + endDate + "\n";
272 private static String newProfileName() {
273 return "profileName_" + randomAlphanumeric(20);
276 private static String newLanguageName() {
277 return "languageName_" + randomAlphanumeric(20);
280 private static String newLanguageKey() {
281 return "languageKey_" + randomAlphanumeric(20);