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 org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.api.platform.Server;
26 import org.sonar.plugins.emailnotifications.api.EmailMessage;
27 import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile;
29 import static org.apache.commons.lang.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;
34 public class BuiltInQualityProfilesNotificationTemplateTest {
36 private Server server = mock(Server.class);
38 private BuiltInQualityProfilesNotificationTemplate underTest = new BuiltInQualityProfilesNotificationTemplate(server);
41 public void setUp() throws Exception {
42 when(server.getPublicRootUrl()).thenReturn("http://" + randomAlphanumeric(10));
46 public void notification_contains_list_of_new_rules() {
47 String profileName = newProfileName();
48 String languageKey = newLanguageKey();
49 String languageName = newLanguageName();
50 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
51 .addProfile(Profile.newBuilder()
52 .setProfileName(profileName)
53 .setLanguageKey(languageKey)
54 .setLanguageName(languageName)
58 EmailMessage emailMessage = underTest.format(notification.serialize());
60 assertMessage(emailMessage,
61 profileTitleText(profileName, languageKey, languageName) +
66 public void notification_contains_list_of_updated_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,
81 profileTitleText(profileName, languageKey, languageName) +
82 " 2 rules have been updated\n");
86 public void notification_contains_list_of_removed_rules() {
87 String profileName = newProfileName();
88 String languageKey = newLanguageKey();
89 String languageName = newLanguageName();
90 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
91 .addProfile(Profile.newBuilder()
92 .setProfileName(profileName)
93 .setLanguageKey(languageKey)
94 .setLanguageName(languageName)
98 EmailMessage emailMessage = underTest.format(notification.serialize());
100 assertMessage(emailMessage,
101 profileTitleText(profileName, languageKey, languageName) +
102 " 2 rules removed\n");
106 public void notification_contains_list_of_new_updated_and_removed_rules() {
107 String profileName = newProfileName();
108 String languageKey = newLanguageKey();
109 String languageName = newLanguageName();
110 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
111 .addProfile(Profile.newBuilder()
112 .setProfileName(profileName)
113 .setLanguageKey(languageKey)
114 .setLanguageName(languageName)
120 EmailMessage emailMessage = underTest.format(notification.serialize());
122 assertMessage(emailMessage,
123 profileTitleText(profileName, languageKey, languageName) +
125 " 3 rules have been updated\n" +
126 " 4 rules removed\n");
130 public void notification_contains_many_profiles() {
131 String profileName1 = "profile1_" + randomAlphanumeric(20);
132 String languageKey1 = "langkey1_" + randomAlphanumeric(20);
133 String languageName1 = "langName1_" + randomAlphanumeric(20);
134 String profileName2 = "profile2_" + randomAlphanumeric(20);
135 String languageKey2 = "langkey2_" + randomAlphanumeric(20);
136 String languageName2 = "langName2_" + randomAlphanumeric(20);
137 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
138 .addProfile(Profile.newBuilder()
139 .setProfileName(profileName1)
140 .setLanguageKey(languageKey1)
141 .setLanguageName(languageName1)
144 .addProfile(Profile.newBuilder()
145 .setProfileName(profileName2)
146 .setLanguageKey(languageKey2)
147 .setLanguageName(languageName2)
151 EmailMessage emailMessage = underTest.format(notification.serialize());
153 assertMessage(emailMessage,
154 profileTitleText(profileName1, languageKey1, languageName1) +
156 profileTitleText(profileName2, languageKey2, languageName2) +
161 public void notification_contains_profiles_sorted_by_language_then_by_profile_name() {
162 String languageKey1 = "langkey1_" + randomAlphanumeric(20);
163 String languageName1 = "langName1_" + randomAlphanumeric(20);
164 String languageKey2 = "langKey2_" + randomAlphanumeric(20);
165 String languageName2 = "langName2_" + randomAlphanumeric(20);
166 String profileName1 = "profile1_" + randomAlphanumeric(20);
167 String profileName2 = "profile2_" + randomAlphanumeric(20);
168 String profileName3 = "profile3_" + randomAlphanumeric(20);
169 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
170 .addProfile(Profile.newBuilder().setProfileName(profileName3).setLanguageKey(languageKey2).setLanguageName(languageName2).build())
171 .addProfile(Profile.newBuilder().setProfileName(profileName2).setLanguageKey(languageKey1).setLanguageName(languageName1).build())
172 .addProfile(Profile.newBuilder().setProfileName(profileName1).setLanguageKey(languageKey2).setLanguageName(languageName2).build());
174 EmailMessage emailMessage = underTest.format(notification.serialize());
176 assertThat(emailMessage.getMessage()).containsSequence(
177 "\"" + profileName2 + "\" - " + languageName1,
178 "\"" + profileName1 + "\" - " + languageName2,
179 "\"" + profileName3 + "\" - " + languageName2);
183 public void notification_contains_encoded_profile_name() {
184 BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
185 .addProfile(Profile.newBuilder()
186 .setProfileName("Sonar Way")
187 .setLanguageKey("java")
188 .setLanguageName(newLanguageName())
191 EmailMessage emailMessage = underTest.format(notification.serialize());
193 assertThat(emailMessage.getMessage()).contains(server.getPublicRootUrl() + "/profiles/changelog?language=java&name=Sonar+Way");
196 private void assertMessage(EmailMessage emailMessage, String expectedProfileDetails) {
197 String expected = "Built-in quality profiles have been updated:\n" +
198 expectedProfileDetails +
199 "This is a good time to review your quality profiles and update them to benefit from the latest evolutions. " + server.getPublicRootUrl() + "/profiles";
200 assertThat(emailMessage.getMessage()).isEqualTo(expected);
203 private String profileTitleText(String profileName, String languageKey, String languageName) {
204 return "\"" + profileName + "\" - " + languageName + " " + server.getPublicRootUrl() + "/profiles/changelog?language=" + languageKey + "&name=" + profileName + "\n";
207 private static String newProfileName() {
208 return "profileName_" + randomAlphanumeric(20);
211 private static String newLanguageName() {
212 return "languageName_" + randomAlphanumeric(20);
215 private static String newLanguageKey() {
216 return "languageKey_" + randomAlphanumeric(20);