]> source.dussan.org Git - sonarqube.git/blob
b7d055214d75df704788bd25a2bb1c296431628e
[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 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;
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
34 public class BuiltInQualityProfilesNotificationTemplateTest {
35
36   private Server server = mock(Server.class);
37
38   private BuiltInQualityProfilesNotificationTemplate underTest = new BuiltInQualityProfilesNotificationTemplate(server);
39
40   @Before
41   public void setUp() throws Exception {
42     when(server.getPublicRootUrl()).thenReturn("http://" + randomAlphanumeric(10));
43   }
44
45   @Test
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)
55         .setNewRules(2)
56         .build());
57
58     EmailMessage emailMessage = underTest.format(notification.serialize());
59
60     assertMessage(emailMessage,
61       profileTitleText(profileName, languageKey, languageName) +
62         " 2 new rules\n");
63   }
64
65   @Test
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)
75         .setUpdatedRules(2)
76         .build());
77
78     EmailMessage emailMessage = underTest.format(notification.serialize());
79
80     assertMessage(emailMessage,
81       profileTitleText(profileName, languageKey, languageName) +
82         " 2 rules have been updated\n");
83   }
84
85   @Test
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)
95         .setRemovedRules(2)
96         .build());
97
98     EmailMessage emailMessage = underTest.format(notification.serialize());
99
100     assertMessage(emailMessage,
101       profileTitleText(profileName, languageKey, languageName) +
102         " 2 rules removed\n");
103   }
104
105   @Test
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)
115         .setNewRules(2)
116         .setUpdatedRules(3)
117         .setRemovedRules(4)
118         .build());
119
120     EmailMessage emailMessage = underTest.format(notification.serialize());
121
122     assertMessage(emailMessage,
123       profileTitleText(profileName, languageKey, languageName) +
124         " 2 new rules\n" +
125         " 3 rules have been updated\n" +
126         " 4 rules removed\n");
127   }
128
129   @Test
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)
142         .setNewRules(2)
143         .build())
144       .addProfile(Profile.newBuilder()
145         .setProfileName(profileName2)
146         .setLanguageKey(languageKey2)
147         .setLanguageName(languageName2)
148         .setNewRules(13)
149         .build());
150
151     EmailMessage emailMessage = underTest.format(notification.serialize());
152
153     assertMessage(emailMessage,
154       profileTitleText(profileName1, languageKey1, languageName1) +
155         " 2 new rules\n" +
156         profileTitleText(profileName2, languageKey2, languageName2) +
157         " 13 new rules\n");
158   }
159
160   @Test
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());
173
174     EmailMessage emailMessage = underTest.format(notification.serialize());
175
176     assertThat(emailMessage.getMessage()).containsSequence(
177       "\"" + profileName2 + "\" - " + languageName1,
178       "\"" + profileName1 + "\" - " + languageName2,
179       "\"" + profileName3 + "\" - " + languageName2);
180   }
181
182   @Test
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())
189         .build());
190
191     EmailMessage emailMessage = underTest.format(notification.serialize());
192
193     assertThat(emailMessage.getMessage()).contains(server.getPublicRootUrl() + "/profiles/changelog?language=java&name=Sonar+Way");
194   }
195
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);
201   }
202
203   private String profileTitleText(String profileName, String languageKey, String languageName) {
204     return "\"" + profileName + "\" - " + languageName + " " + server.getPublicRootUrl() + "/profiles/changelog?language=" + languageKey + "&name=" + profileName + "\n";
205   }
206
207   private static String newProfileName() {
208     return "profileName_" + randomAlphanumeric(20);
209   }
210
211   private static String newLanguageName() {
212     return "languageName_" + randomAlphanumeric(20);
213   }
214
215   private static String newLanguageKey() {
216     return "languageKey_" + randomAlphanumeric(20);
217   }
218 }