]> source.dussan.org Git - sonarqube.git/blob
1bd78367688aa2d07b5428addd701326712b7dae
[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.Test;
24 import org.sonar.plugins.emailnotifications.api.EmailMessage;
25 import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile;
26
27 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
28 import static org.assertj.core.api.Assertions.assertThat;
29
30 public class BuiltInQualityProfilesNotificationTemplateTest {
31
32   private BuiltInQualityProfilesNotificationTemplate underTest = new BuiltInQualityProfilesNotificationTemplate();
33
34   @Test
35   public void notification_contains_list_of_new_rules() {
36     String profileName = randomAlphanumeric(20);
37     String language = randomAlphanumeric(20);
38     BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
39       .addProfile(Profile.newBuilder(profileName, language)
40         .setNewRules(2)
41         .build());
42
43     EmailMessage emailMessage = underTest.format(notification.serialize());
44
45     assertThat(emailMessage.getMessage()).isEqualTo("Built-in quality profiles have been updated:\n" +
46       "\"" + profileName + "\" - " + language + "\n" +
47       " 2 new rules\n" +
48       "This is a good time to review your quality profiles and update them to benefit from the latest evolutions.");
49   }
50
51   @Test
52   public void notification_contains_list_of_updated_rules() {
53     String profileName = randomAlphanumeric(20);
54     String language = randomAlphanumeric(20);
55     BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
56       .addProfile(Profile.newBuilder(profileName, language)
57         .setUpdatedRules(2)
58         .build());
59
60     EmailMessage emailMessage = underTest.format(notification.serialize());
61
62     assertThat(emailMessage.getMessage()).isEqualTo("Built-in quality profiles have been updated:\n" +
63       "\"" + profileName + "\" - " + language + "\n" +
64       " 2 rules have been updated\n" +
65       "This is a good time to review your quality profiles and update them to benefit from the latest evolutions.");
66   }
67
68   @Test
69   public void notification_contains_list_of_removed_rules() {
70     String profileName = randomAlphanumeric(20);
71     String language = randomAlphanumeric(20);
72     BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
73       .addProfile(Profile.newBuilder(profileName, language)
74         .setRemovedRules(2)
75         .build());
76
77     EmailMessage emailMessage = underTest.format(notification.serialize());
78
79     assertThat(emailMessage.getMessage()).isEqualTo("Built-in quality profiles have been updated:\n" +
80       "\"" + profileName + "\" - " + language + "\n" +
81       " 2 rules removed\n" +
82       "This is a good time to review your quality profiles and update them to benefit from the latest evolutions.");
83   }
84
85   @Test
86   public void notification_contains_list_of_new_updated_and_removed_rules() {
87     String profileName = randomAlphanumeric(20);
88     String language = randomAlphanumeric(20);
89     BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
90       .addProfile(Profile.newBuilder(profileName, language)
91         .setNewRules(2)
92         .setUpdatedRules(3)
93         .setRemovedRules(4)
94         .build());
95
96     EmailMessage emailMessage = underTest.format(notification.serialize());
97
98     assertThat(emailMessage.getMessage()).isEqualTo("Built-in quality profiles have been updated:\n" +
99       "\"" + profileName + "\" - " + language + "\n" +
100       " 2 new rules\n" +
101       " 3 rules have been updated\n" +
102       " 4 rules removed\n" +
103       "This is a good time to review your quality profiles and update them to benefit from the latest evolutions.");
104   }
105
106   @Test
107   public void notification_contains_many_profiles() {
108     String profileName1 = "profile1_" + randomAlphanumeric(20);
109     String language1 = "lang1_" + randomAlphanumeric(20);
110     String profileName2 = "profile1_" + randomAlphanumeric(20);
111     String language2 = "lang2_" + randomAlphanumeric(20);
112     BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
113       .addProfile(Profile.newBuilder(profileName1, language1)
114         .setNewRules(2)
115         .build())
116       .addProfile(Profile.newBuilder(profileName2, language2)
117         .setNewRules(13)
118         .build());
119
120     EmailMessage emailMessage = underTest.format(notification.serialize());
121
122     assertThat(emailMessage.getMessage()).isEqualTo("Built-in quality profiles have been updated:\n" +
123       "\"" + profileName1 + "\" - " + language1 + "\n" +
124       " 2 new rules\n" +
125       "\"" + profileName2 + "\" - " + language2 + "\n" +
126       " 13 new rules\n" +
127       "This is a good time to review your quality profiles and update them to benefit from the latest evolutions.");
128   }
129
130   @Test
131   public void notification_contains_profiles_sorted_by_language_then_by_profile_name() {
132     String language1 = "lang1_" + randomAlphanumeric(20);
133     String language2 = "lang2_" + randomAlphanumeric(20);
134     String profileName1 = "profile1_" + randomAlphanumeric(20);
135     String profileName2 = "profile2_" + randomAlphanumeric(20);
136     String profileName3 = "profile3_" + randomAlphanumeric(20);
137     BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification()
138       .addProfile(Profile.newBuilder(profileName3, language2).build())
139       .addProfile(Profile.newBuilder(profileName2, language1).build())
140       .addProfile(Profile.newBuilder(profileName1, language2).build());
141
142     EmailMessage emailMessage = underTest.format(notification.serialize());
143
144     assertThat(emailMessage.getMessage()).containsSequence(
145       "\"" + profileName2 + "\" - " + language1,
146       "\"" + profileName1 + "\" - " + language2,
147       "\"" + profileName3 + "\" - " + language2);
148   }
149 }