]> source.dussan.org Git - sonarqube.git/blob
176a22aa7e747aa45aa076fbbdd00b4af4bfd286
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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
21 package org.sonar.server.qualityprofile;
22
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;
29
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;
35
36 public class BuiltInQualityProfilesNotificationTemplateTest {
37
38   private Server server = mock(Server.class);
39
40   private BuiltInQualityProfilesNotificationTemplate underTest = new BuiltInQualityProfilesNotificationTemplate(server);
41
42   @Before
43   public void setUp() throws Exception {
44     when(server.getPublicRootUrl()).thenReturn("http://" + randomAlphanumeric(10));
45   }
46
47   @Test
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)
57         .setNewRules(2)
58         .build());
59
60     EmailMessage emailMessage = underTest.format(notification.serialize());
61
62     assertThat(emailMessage.getSubject()).isEqualTo("Built-in quality profiles have been updated");
63   }
64
65   @Test
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)
75         .setNewRules(2)
76         .build());
77
78     EmailMessage emailMessage = underTest.format(notification.serialize());
79
80     assertMessage(emailMessage,
81         "\n 2 new rules\n");
82   }
83
84   @Test
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)
94         .setUpdatedRules(2)
95         .build());
96
97     EmailMessage emailMessage = underTest.format(notification.serialize());
98
99     assertMessage(emailMessage,
100       "\n" +
101         " 2 rules have been updated\n");
102   }
103
104   @Test
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)
114         .setRemovedRules(2)
115         .build());
116
117     EmailMessage emailMessage = underTest.format(notification.serialize());
118
119     assertMessage(emailMessage,
120       "\n" +
121         " 2 rules removed\n");
122   }
123
124   @Test
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)
134         .setNewRules(2)
135         .setUpdatedRules(3)
136         .setRemovedRules(4)
137         .build());
138
139     EmailMessage emailMessage = underTest.format(notification.serialize());
140
141     assertMessage(emailMessage,
142         "\n" +
143         " 2 new rules\n" +
144         " 3 rules have been updated\n" +
145         " 4 rules removed\n");
146   }
147
148   @Test
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)
161         .setNewRules(2)
162         .build())
163       .addProfile(Profile.newBuilder()
164         .setProfileName(profileName2)
165         .setLanguageKey(languageKey2)
166         .setLanguageName(languageName2)
167         .setNewRules(13)
168         .build());
169
170     EmailMessage emailMessage = underTest.format(notification.serialize());
171
172     assertThat(emailMessage.getMessage()).containsSequence("Built-in quality profiles have been updated:\n",
173       profileTitleText(profileName1, languageKey1, languageName1),
174         " 2 new rules\n",
175         profileTitleText(profileName2, languageKey2, languageName2),
176         " 13 new rules\n",
177       "This is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles");
178   }
179
180   @Test
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());
193
194     EmailMessage emailMessage = underTest.format(notification.serialize());
195
196     assertThat(emailMessage.getMessage()).containsSequence(
197       "\"" + profileName2 + "\" - " + languageName1,
198       "\"" + profileName1 + "\" - " + languageName2,
199       "\"" + profileName3 + "\" - " + languageName2);
200   }
201
202   @Test
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())
209         .build());
210
211     EmailMessage emailMessage = underTest.format(notification.serialize());
212
213     assertThat(emailMessage.getMessage()).contains(server.getPublicRootUrl() + "/profiles/changelog?language=java&name=Sonar+Way");
214   }
215
216   @Test
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)
229         .setEndDate(endDate)
230         .build());
231
232     EmailMessage emailMessage = underTest.format(notification.serialize());
233
234     assertMessage(emailMessage,
235       profileTitleText(profileName, languageKey, languageName, formatDate(new Date(startDate)), formatDate(new Date(endDate))));
236   }
237
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");
242   }
243
244   private String profileTitleText(String profileName, String languageKey, String languageName) {
245     return "\"" + profileName + "\" - " + languageName + ": " + server.getPublicRootUrl() + "/profiles/changelog?language=" + languageKey + "&name=" + profileName;
246   }
247
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";
251   }
252
253   private static String newProfileName() {
254     return "profileName_" + randomAlphanumeric(20);
255   }
256
257   private static String newLanguageName() {
258     return "languageName_" + randomAlphanumeric(20);
259   }
260
261   private static String newLanguageKey() {
262     return "languageKey_" + randomAlphanumeric(20);
263   }
264 }