3 * Copyright (C) 2009-2017 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.
21 package org.sonar.server.qualityprofile;
23 import java.util.Date;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.platform.Server;
27 import org.sonar.plugins.emailnotifications.api.EmailMessage;
28 import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile;
30 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
34 import static org.sonar.api.utils.DateUtils.formatDate;
36 public class BuiltInQualityProfilesNotificationTemplateTest {
38 private Server server = mock(Server.class);
40 private BuiltInQualityProfilesNotificationTemplate underTest = new BuiltInQualityProfilesNotificationTemplate(server);
43 public void setUp() throws Exception {
44 when(server.getPublicRootUrl()).thenReturn("http://" + randomAlphanumeric(10));
48 public void notification_contains_a_subject() {
49 String profileName = newProfileName();
50 String languageKey = newLanguageKey();
51 String languageName = newLanguageName();
52 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
53 .addProfile(Profile.newBuilder()
54 .setProfileName(profileName)
55 .setLanguageKey(languageKey)
56 .setLanguageName(languageName)
60 EmailMessage emailMessage = underTest.format(notification.serialize());
62 assertThat(emailMessage.getSubject()).isEqualTo("Built-in quality profiles have been updated");
66 public void notification_contains_list_of_new_rules() {
67 String profileName = newProfileName();
68 String languageKey = newLanguageKey();
69 String languageName = newLanguageName();
70 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
71 .addProfile(Profile.newBuilder()
72 .setProfileName(profileName)
73 .setLanguageKey(languageKey)
74 .setLanguageName(languageName)
78 EmailMessage emailMessage = underTest.format(notification.serialize());
80 assertMessage(emailMessage,
85 public void notification_contains_list_of_updated_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)
97 EmailMessage emailMessage = underTest.format(notification.serialize());
99 assertMessage(emailMessage,
101 " 2 rules have been updated\n");
105 public void notification_contains_list_of_removed_rules() {
106 String profileName = newProfileName();
107 String languageKey = newLanguageKey();
108 String languageName = newLanguageName();
109 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
110 .addProfile(Profile.newBuilder()
111 .setProfileName(profileName)
112 .setLanguageKey(languageKey)
113 .setLanguageName(languageName)
117 EmailMessage emailMessage = underTest.format(notification.serialize());
119 assertMessage(emailMessage,
121 " 2 rules removed\n");
125 public void notification_contains_list_of_new_updated_and_removed_rules() {
126 String profileName = newProfileName();
127 String languageKey = newLanguageKey();
128 String languageName = newLanguageName();
129 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
130 .addProfile(Profile.newBuilder()
131 .setProfileName(profileName)
132 .setLanguageKey(languageKey)
133 .setLanguageName(languageName)
139 EmailMessage emailMessage = underTest.format(notification.serialize());
141 assertMessage(emailMessage,
144 " 3 rules have been updated\n" +
145 " 4 rules removed\n");
149 public void notification_contains_many_profiles() {
150 String profileName1 = "profile1_" + randomAlphanumeric(20);
151 String languageKey1 = "langkey1_" + randomAlphanumeric(20);
152 String languageName1 = "langName1_" + randomAlphanumeric(20);
153 String profileName2 = "profile2_" + randomAlphanumeric(20);
154 String languageKey2 = "langkey2_" + randomAlphanumeric(20);
155 String languageName2 = "langName2_" + randomAlphanumeric(20);
156 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
157 .addProfile(Profile.newBuilder()
158 .setProfileName(profileName1)
159 .setLanguageKey(languageKey1)
160 .setLanguageName(languageName1)
163 .addProfile(Profile.newBuilder()
164 .setProfileName(profileName2)
165 .setLanguageKey(languageKey2)
166 .setLanguageName(languageName2)
170 EmailMessage emailMessage = underTest.format(notification.serialize());
172 assertThat(emailMessage.getMessage()).containsSequence("Built-in quality profiles have been updated:\n",
173 profileTitleText(profileName1, languageKey1, languageName1),
175 profileTitleText(profileName2, languageKey2, languageName2),
177 "This is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles");
181 public void notification_contains_profiles_sorted_by_language_then_by_profile_name() {
182 String languageKey1 = "langkey1_" + randomAlphanumeric(20);
183 String languageName1 = "langName1_" + randomAlphanumeric(20);
184 String languageKey2 = "langKey2_" + randomAlphanumeric(20);
185 String languageName2 = "langName2_" + randomAlphanumeric(20);
186 String profileName1 = "profile1_" + randomAlphanumeric(20);
187 String profileName2 = "profile2_" + randomAlphanumeric(20);
188 String profileName3 = "profile3_" + randomAlphanumeric(20);
189 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
190 .addProfile(Profile.newBuilder().setProfileName(profileName3).setLanguageKey(languageKey2).setLanguageName(languageName2).build())
191 .addProfile(Profile.newBuilder().setProfileName(profileName2).setLanguageKey(languageKey1).setLanguageName(languageName1).build())
192 .addProfile(Profile.newBuilder().setProfileName(profileName1).setLanguageKey(languageKey2).setLanguageName(languageName2).build());
194 EmailMessage emailMessage = underTest.format(notification.serialize());
196 assertThat(emailMessage.getMessage()).containsSequence(
197 "\"" + profileName2 + "\" - " + languageName1,
198 "\"" + profileName1 + "\" - " + languageName2,
199 "\"" + profileName3 + "\" - " + languageName2);
203 public void notification_contains_encoded_profile_name() {
204 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
205 .addProfile(Profile.newBuilder()
206 .setProfileName("Sonar Way")
207 .setLanguageKey("java")
208 .setLanguageName(newLanguageName())
211 EmailMessage emailMessage = underTest.format(notification.serialize());
213 assertThat(emailMessage.getMessage()).contains(server.getPublicRootUrl() + "/profiles/changelog?language=java&name=Sonar+Way");
217 public void notification_contains_from_and_to_date() {
218 String profileName = newProfileName();
219 String languageKey = newLanguageKey();
220 String languageName = newLanguageName();
221 long startDate = 1_000_000_000_000L;
222 long endDate = startDate + 1_100_000_000_000L;
223 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
224 .addProfile(Profile.newBuilder()
225 .setProfileName(profileName)
226 .setLanguageKey(languageKey)
227 .setLanguageName(languageName)
228 .setStartDate(startDate)
232 EmailMessage emailMessage = underTest.format(notification.serialize());
234 assertMessage(emailMessage,
235 profileTitleText(profileName, languageKey, languageName, formatDate(new Date(startDate)), formatDate(new Date(endDate))));
238 private void assertMessage(EmailMessage emailMessage, String expectedProfileDetails) {
239 assertThat(emailMessage.getMessage()).containsSequence("Built-in quality profiles have been updated:\n",
240 expectedProfileDetails,
241 "This is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles");
244 private String profileTitleText(String profileName, String languageKey, String languageName) {
245 return "\"" + profileName + "\" - " + languageName + ": " + server.getPublicRootUrl() + "/profiles/changelog?language=" + languageKey + "&name=" + profileName;
248 private String profileTitleText(String profileName, String languageKey, String languageName, String startDate, String endDate) {
249 return "\"" + profileName + "\" - " + languageName + ": " + server.getPublicRootUrl() + "/profiles/changelog?language=" + languageKey + "&name=" + profileName +
250 "&since=" + startDate + "&to=" + endDate + "\n";
253 private static String newProfileName() {
254 return "profileName_" + randomAlphanumeric(20);
257 private static String newLanguageName() {
258 return "languageName_" + randomAlphanumeric(20);
261 private static String newLanguageKey() {
262 return "languageKey_" + randomAlphanumeric(20);