]> source.dussan.org Git - sonarqube.git/blob
18257a07d4ff568e88136b1acba2b941ea8506bb
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.builtin;
21
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;
28
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;
33 import static org.sonar.api.utils.DateUtils.formatDate;
34
35 public class BuiltInQPChangeNotificationTemplateTest {
36
37   private Server server = mock(Server.class);
38
39   private BuiltInQPChangeNotificationTemplate underTest = new BuiltInQPChangeNotificationTemplate(server);
40
41   @Before
42   public void setUp() {
43     when(server.getPublicRootUrl()).thenReturn("http://" + randomAlphanumeric(10));
44   }
45
46   @Test
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)
56         .setNewRules(2)
57         .build());
58
59     EmailMessage emailMessage = underTest.format(notification.build());
60
61     assertThat(emailMessage.getSubject()).isEqualTo("Built-in quality profiles have been updated");
62   }
63
64   @Test
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)
74         .setNewRules(2)
75         .build());
76
77     EmailMessage emailMessage = underTest.format(notification.build());
78
79     assertMessage(emailMessage, "\n 2 new rules\n");
80   }
81
82   @Test
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)
92         .setUpdatedRules(2)
93         .build());
94
95     EmailMessage emailMessage = underTest.format(notification.build());
96
97     assertMessage(emailMessage, "\n 2 rules have been updated\n");
98   }
99
100   @Test
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)
110         .setRemovedRules(2)
111         .build());
112
113     EmailMessage emailMessage = underTest.format(notification.build());
114
115     assertMessage(emailMessage, "\n 2 rules removed\n");
116   }
117
118   @Test
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)
128             .setNewRules(1)
129             .setUpdatedRules(1)
130             .setRemovedRules(1)
131             .build());
132
133     EmailMessage emailMessage = underTest.format(notification.build());
134
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");
139   }
140
141   @Test
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)
151         .setNewRules(2)
152         .setUpdatedRules(3)
153         .setRemovedRules(4)
154         .build());
155
156     EmailMessage emailMessage = underTest.format(notification.build());
157
158     assertMessage(emailMessage,
159       "\n" +
160         " 2 new rules\n" +
161         " 3 rules have been updated\n" +
162         " 4 rules removed\n");
163   }
164
165   @Test
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)
178         .setNewRules(2)
179         .build())
180       .addProfile(Profile.newBuilder()
181         .setProfileName(profileName2)
182         .setLanguageKey(languageKey2)
183         .setLanguageName(languageName2)
184         .setNewRules(13)
185         .build());
186
187     EmailMessage emailMessage = underTest.format(notification.build());
188
189     assertThat(emailMessage.getMessage()).containsSubsequence("The following built-in profiles have been updated:\n",
190       profileTitleText(profileName1, languageKey1, languageName1),
191       " 2 new rules\n",
192       profileTitleText(profileName2, languageKey2, languageName2),
193       " 13 new rules\n",
194       "This is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles");
195   }
196
197   @Test
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());
210
211     EmailMessage emailMessage = underTest.format(notification.build());
212
213     assertThat(emailMessage.getMessage()).containsSubsequence(
214       "\"" + profileName2 + "\" - " + languageName1,
215       "\"" + profileName1 + "\" - " + languageName2,
216       "\"" + profileName3 + "\" - " + languageName2);
217   }
218
219   @Test
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())
226         .build());
227
228     EmailMessage emailMessage = underTest.format(notification.build());
229
230     assertThat(emailMessage.getMessage()).contains(server.getPublicRootUrl() + "/profiles/changelog?language=java&name=Sonar+Way");
231   }
232
233   @Test
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)
246         .setEndDate(endDate)
247         .build());
248
249     EmailMessage emailMessage = underTest.format(notification.build());
250
251     assertMessage(emailMessage,
252       profileTitleText(profileName, languageKey, languageName, formatDate(new Date(startDate)), formatDate(new Date(endDate))));
253   }
254
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");
261   }
262
263   private String profileTitleText(String profileName, String languageKey, String languageName) {
264     return "\"" + profileName + "\" - " + languageName + ": " + server.getPublicRootUrl() + "/profiles/changelog?language=" + languageKey + "&name=" + profileName;
265   }
266
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";
270   }
271
272   private static String newProfileName() {
273     return "profileName_" + randomAlphanumeric(20);
274   }
275
276   private static String newLanguageName() {
277     return "languageName_" + randomAlphanumeric(20);
278   }
279
280   private static String newLanguageKey() {
281     return "languageKey_" + randomAlphanumeric(20);
282   }
283 }