]> source.dussan.org Git - sonarqube.git/blob
5a956a4df109ae283b06890603583f16361f5097
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 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.qualitygate.notification;
21
22 import com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.sonar.api.config.EmailSettings;
29 import org.sonar.api.notifications.Notification;
30 import org.sonar.server.issue.notification.EmailMessage;
31
32 import static org.hamcrest.CoreMatchers.is;
33 import static org.hamcrest.CoreMatchers.nullValue;
34 import static org.junit.Assert.assertThat;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
37
38 @RunWith(DataProviderRunner.class)
39 public class QGChangeEmailTemplateTest {
40
41   private QGChangeEmailTemplate template;
42
43   @Before
44   public void setUp() {
45     EmailSettings configuration = mock(EmailSettings.class);
46     when(configuration.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
47     template = new QGChangeEmailTemplate(configuration);
48   }
49
50   @Test
51   public void shouldNotFormatIfNotCorrectNotification() {
52     Notification notification = new Notification("other-notif");
53     EmailMessage message = template.format(notification);
54     assertThat(message, nullValue());
55   }
56
57   @Test
58   public void shouldFormatAlertWithSeveralMessages() {
59     Notification notification = createNotification("Failed", "violations > 4, coverage < 75%", "ERROR", "false");
60
61     EmailMessage message = template.format(notification);
62     assertThat(message.getMessageId(), is("alerts/45"));
63     assertThat(message.getSubject(), is("Quality gate status changed on \"Foo\""));
64     assertThat(message.getMessage(), is("" +
65       "Project: Foo\n" +
66       "Version: V1-SNAP\n" +
67       "Quality gate status: Failed\n" +
68       "\n" +
69       "Quality gate thresholds:\n" +
70       "  - violations worse than D\n" +
71       "  - coverage < 75%\n" +
72       "\n" +
73       "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
74   }
75
76   @Test
77   public void shouldFormatAlertWithSeveralMessagesOnBranch() {
78     Notification notification = createNotification("Failed", "violations > 4, coverage < 75%", "ERROR", "false")
79         .setFieldValue("branch", "feature");
80
81     EmailMessage message = template.format(notification);
82     assertThat(message.getMessageId(), is("alerts/45"));
83     assertThat(message.getSubject(), is("Quality gate status changed on \"Foo (feature)\""));
84     assertThat(message.getMessage(), is("" +
85       "Project: Foo\n" +
86       "Branch: feature\n" +
87       "Version: V1-SNAP\n" +
88       "Quality gate status: Failed\n" +
89       "\n" +
90       "Quality gate thresholds:\n" +
91       "  - violations worse than D\n" +
92       "  - coverage < 75%\n" +
93       "\n" +
94       "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo&branch=feature"));
95   }
96
97   @Test
98   public void shouldFormatNewAlertWithSeveralMessages() {
99     Notification notification = createNotification("Failed", "violations > 4, coverage < 75%", "ERROR", "true");
100
101     EmailMessage message = template.format(notification);
102     assertThat(message.getMessageId(), is("alerts/45"));
103     assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo\""));
104     assertThat(message.getMessage(), is("" +
105       "Project: Foo\n" +
106       "Version: V1-SNAP\n" +
107       "Quality gate status: Failed\n" +
108       "\n" +
109       "New quality gate thresholds:\n" +
110       "  - violations worse than D\n" +
111       "  - coverage < 75%\n" +
112       "\n" +
113       "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
114   }
115
116   @Test
117   public void shouldFormatNewAlertWithOneMessage() {
118     Notification notification = createNotification("Failed", "violations > 4", "ERROR", "true");
119
120     EmailMessage message = template.format(notification);
121     assertThat(message.getMessageId(), is("alerts/45"));
122     assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo\""));
123     assertThat(message.getMessage(), is("" +
124       "Project: Foo\n" +
125       "Version: V1-SNAP\n" +
126       "Quality gate status: Failed\n" +
127       "\n" +
128       "New quality gate threshold: violations worse than D\n" +
129       "\n" +
130       "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
131   }
132
133   @Test
134   public void shouldFormatNewAlertWithoutVersion() {
135     Notification notification = createNotification("Failed", "violations > 4", "ERROR", "true")
136         .setFieldValue("projectVersion", null);
137
138     EmailMessage message = template.format(notification);
139     assertThat(message.getMessageId(), is("alerts/45"));
140     assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo\""));
141     assertThat(message.getMessage(), is("" +
142       "Project: Foo\n" +
143       "Quality gate status: Failed\n" +
144       "\n" +
145       "New quality gate threshold: violations worse than D\n" +
146       "\n" +
147       "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
148   }
149
150   @Test
151   public void shouldFormatBackToGreenMessage() {
152     Notification notification = createNotification("Passed", "", "OK", "false");
153
154     EmailMessage message = template.format(notification);
155     assertThat(message.getMessageId(), is("alerts/45"));
156     assertThat(message.getSubject(), is("\"Foo\" is back to green"));
157     assertThat(message.getMessage(), is("" +
158       "Project: Foo\n" +
159       "Version: V1-SNAP\n" +
160       "Quality gate status: Passed\n" +
161       "\n" +
162       "\n" +
163       "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
164   }
165
166   @Test
167   public void shouldFormatNewAlertWithOneMessageOnBranch() {
168     Notification notification = createNotification("Failed", "violations > 4", "ERROR", "true")
169       .setFieldValue("branch", "feature");
170
171     EmailMessage message = template.format(notification);
172     assertThat(message.getMessageId(), is("alerts/45"));
173     assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo (feature)\""));
174     assertThat(message.getMessage(), is("" +
175       "Project: Foo\n" +
176       "Branch: feature\n" +
177       "Version: V1-SNAP\n" +
178       "Quality gate status: Failed\n" +
179       "\n" +
180       "New quality gate threshold: violations worse than D\n" +
181       "\n" +
182       "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo&branch=feature"));
183   }
184
185   @Test
186   public void shouldFormatBackToGreenMessageOnBranch() {
187     Notification notification = createNotification("Passed", "", "OK", "false")
188         .setFieldValue("branch", "feature");
189
190     EmailMessage message = template.format(notification);
191     assertThat(message.getMessageId(), is("alerts/45"));
192     assertThat(message.getSubject(), is("\"Foo (feature)\" is back to green"));
193     assertThat(message.getMessage(), is("" +
194       "Project: Foo\n" +
195       "Branch: feature\n" +
196       "Version: V1-SNAP\n" +
197       "Quality gate status: Passed\n" +
198       "\n" +
199       "\n" +
200       "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo&branch=feature"));
201   }
202
203   @DataProvider
204   public static Object[][] alertTextAndFormattedText() {
205     return new Object[][] {
206       {"violations > 1", "violations worse than A"},
207       {"violations > 4", "violations worse than D"},
208       {"Code Coverage < 50%", "Code Coverage < 50%"},
209       {"custom metric condition not met", "custom metric condition not met"}
210     };
211   }
212
213   @UseDataProvider("alertTextAndFormattedText")
214   @Test
215   public void shouldFormatNewAlertWithThresholdProperlyFormatted(String alertText, String expectedFormattedAlertText) {
216     Notification notification = createNotification("Failed", alertText, "ERROR", "true");
217
218     EmailMessage message = template.format(notification);
219     assertThat(message.getMessageId(), is("alerts/45"));
220     assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo\""));
221     assertThat(message.getMessage(), is("" +
222       "Project: Foo\n" +
223       "Version: V1-SNAP\n" +
224       "Quality gate status: Failed\n" +
225       "\n" +
226       "New quality gate threshold: " + expectedFormattedAlertText + "\n" +
227       "\n" +
228       "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
229   }
230
231
232   private Notification createNotification(String alertName, String alertText, String alertLevel, String isNewAlert) {
233     return new Notification("alerts")
234         .setFieldValue("projectName", "Foo")
235         .setFieldValue("projectKey", "org.sonar.foo:foo")
236         .setFieldValue("projectId", "45")
237         .setFieldValue("projectVersion", "V1-SNAP")
238         .setFieldValue("alertName", alertName)
239         .setFieldValue("alertText", alertText)
240         .setFieldValue("alertLevel", alertLevel)
241         .setFieldValue("isNewAlert", isNewAlert);
242   }
243
244 }