3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.qualitygate.notification;
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;
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;
38 @RunWith(DataProviderRunner.class)
39 public class QGChangeEmailTemplateTest {
41 private QGChangeEmailTemplate template;
45 EmailSettings configuration = mock(EmailSettings.class);
46 when(configuration.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
47 template = new QGChangeEmailTemplate(configuration);
51 public void shouldNotFormatIfNotCorrectNotification() {
52 Notification notification = new Notification("other-notif");
53 EmailMessage message = template.format(notification);
54 assertThat(message, nullValue());
58 public void shouldFormatAlertWithSeveralMessages() {
59 Notification notification = createNotification("Failed", "violations > 4, coverage < 75%", "ERROR", "false");
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("" +
66 "Version: V1-SNAP\n" +
67 "Quality gate status: Failed\n" +
69 "Quality gate thresholds:\n" +
70 " - violations worse than D\n" +
71 " - coverage < 75%\n" +
73 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
77 public void shouldFormatAlertWithSeveralMessagesOnBranch() {
78 Notification notification = createNotification("Failed", "violations > 4, coverage < 75%", "ERROR", "false")
79 .setFieldValue("branch", "feature");
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("" +
87 "Version: V1-SNAP\n" +
88 "Quality gate status: Failed\n" +
90 "Quality gate thresholds:\n" +
91 " - violations worse than D\n" +
92 " - coverage < 75%\n" +
94 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo&branch=feature"));
98 public void shouldFormatNewAlertWithSeveralMessages() {
99 Notification notification = createNotification("Failed", "violations > 4, coverage < 75%", "ERROR", "true");
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("" +
106 "Version: V1-SNAP\n" +
107 "Quality gate status: Failed\n" +
109 "New quality gate thresholds:\n" +
110 " - violations worse than D\n" +
111 " - coverage < 75%\n" +
113 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
117 public void shouldFormatNewAlertWithOneMessage() {
118 Notification notification = createNotification("Failed", "violations > 4", "ERROR", "true");
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("" +
125 "Version: V1-SNAP\n" +
126 "Quality gate status: Failed\n" +
128 "New quality gate threshold: violations worse than D\n" +
130 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
134 public void shouldFormatNewAlertWithoutVersion() {
135 Notification notification = createNotification("Failed", "violations > 4", "ERROR", "true")
136 .setFieldValue("projectVersion", null);
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("" +
143 "Quality gate status: Failed\n" +
145 "New quality gate threshold: violations worse than D\n" +
147 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
151 public void shouldFormatBackToGreenMessage() {
152 Notification notification = createNotification("Passed", "", "OK", "false");
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("" +
159 "Version: V1-SNAP\n" +
160 "Quality gate status: Passed\n" +
163 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
167 public void shouldFormatNewAlertWithOneMessageOnBranch() {
168 Notification notification = createNotification("Failed", "violations > 4", "ERROR", "true")
169 .setFieldValue("branch", "feature");
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("" +
176 "Branch: feature\n" +
177 "Version: V1-SNAP\n" +
178 "Quality gate status: Failed\n" +
180 "New quality gate threshold: violations worse than D\n" +
182 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo&branch=feature"));
186 public void shouldFormatBackToGreenMessageOnBranch() {
187 Notification notification = createNotification("Passed", "", "OK", "false")
188 .setFieldValue("branch", "feature");
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("" +
195 "Branch: feature\n" +
196 "Version: V1-SNAP\n" +
197 "Quality gate status: Passed\n" +
200 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo&branch=feature"));
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"}
213 @UseDataProvider("alertTextAndFormattedText")
215 public void shouldFormatNewAlertWithThresholdProperlyFormatted(String alertText, String expectedFormattedAlertText) {
216 Notification notification = createNotification("Failed", alertText, "ERROR", "true");
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("" +
223 "Version: V1-SNAP\n" +
224 "Quality gate status: Failed\n" +
226 "New quality gate threshold: " + expectedFormattedAlertText + "\n" +
228 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
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);