3 * Copyright (C) 2009-2020 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 org.junit.Before;
23 import org.junit.Test;
24 import org.sonar.api.config.EmailSettings;
25 import org.sonar.api.notifications.Notification;
26 import org.sonar.server.issue.notification.EmailMessage;
28 import static org.hamcrest.CoreMatchers.is;
29 import static org.hamcrest.CoreMatchers.nullValue;
30 import static org.junit.Assert.assertThat;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
34 public class QGChangeEmailTemplateTest {
36 private QGChangeEmailTemplate template;
40 EmailSettings configuration = mock(EmailSettings.class);
41 when(configuration.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
42 template = new QGChangeEmailTemplate(configuration);
46 public void shouldNotFormatIfNotCorrectNotification() {
47 Notification notification = new Notification("other-notif");
48 EmailMessage message = template.format(notification);
49 assertThat(message, nullValue());
53 public void shouldFormatAlertWithSeveralMessages() {
54 Notification notification = createNotification("Red (was Green)", "violations > 4, coverage < 75%", "ERROR", "false");
56 EmailMessage message = template.format(notification);
57 assertThat(message.getMessageId(), is("alerts/45"));
58 assertThat(message.getSubject(), is("Quality gate status changed on \"Foo\""));
59 assertThat(message.getMessage(), is("" +
61 "Version: V1-SNAP\n" +
62 "Quality gate status: Red (was Green)\n" +
64 "Quality gate thresholds:\n" +
65 " - violations > 4\n" +
66 " - coverage < 75%\n" +
68 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
72 public void shouldFormatAlertWithSeveralMessagesOnBranch() {
73 Notification notification = createNotification("Red (was Green)", "violations > 4, coverage < 75%", "ERROR", "false")
74 .setFieldValue("branch", "feature");
76 EmailMessage message = template.format(notification);
77 assertThat(message.getMessageId(), is("alerts/45"));
78 assertThat(message.getSubject(), is("Quality gate status changed on \"Foo (feature)\""));
79 assertThat(message.getMessage(), is("" +
82 "Version: V1-SNAP\n" +
83 "Quality gate status: Red (was Green)\n" +
85 "Quality gate thresholds:\n" +
86 " - violations > 4\n" +
87 " - coverage < 75%\n" +
89 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo&branch=feature"));
93 public void shouldFormatNewAlertWithSeveralMessages() {
94 Notification notification = createNotification("Red (was Green)", "violations > 4, coverage < 75%", "ERROR", "true");
96 EmailMessage message = template.format(notification);
97 assertThat(message.getMessageId(), is("alerts/45"));
98 assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo\""));
99 assertThat(message.getMessage(), is("" +
101 "Version: V1-SNAP\n" +
102 "Quality gate status: Red (was Green)\n" +
104 "New quality gate thresholds:\n" +
105 " - violations > 4\n" +
106 " - coverage < 75%\n" +
108 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
112 public void shouldFormatNewAlertWithOneMessage() {
113 Notification notification = createNotification("Red (was Green)", "violations > 4", "ERROR", "true");
115 EmailMessage message = template.format(notification);
116 assertThat(message.getMessageId(), is("alerts/45"));
117 assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo\""));
118 assertThat(message.getMessage(), is("" +
120 "Version: V1-SNAP\n" +
121 "Quality gate status: Red (was Green)\n" +
123 "New quality gate threshold: violations > 4\n" +
125 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
129 public void shouldFormatNewAlertWithoutVersion() {
130 Notification notification = createNotification("Red (was Green)", "violations > 4", "ERROR", "true")
131 .setFieldValue("projectVersion", null);
133 EmailMessage message = template.format(notification);
134 assertThat(message.getMessageId(), is("alerts/45"));
135 assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo\""));
136 assertThat(message.getMessage(), is("" +
138 "Quality gate status: Red (was Green)\n" +
140 "New quality gate threshold: violations > 4\n" +
142 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
146 public void shouldFormatNewAlertWithOneMessageOnBranch() {
147 Notification notification = createNotification("Red (was Green)", "violations > 4", "ERROR", "true")
148 .setFieldValue("branch", "feature");
150 EmailMessage message = template.format(notification);
151 assertThat(message.getMessageId(), is("alerts/45"));
152 assertThat(message.getSubject(), is("New quality gate threshold reached on \"Foo (feature)\""));
153 assertThat(message.getMessage(), is("" +
155 "Branch: feature\n" +
156 "Version: V1-SNAP\n" +
157 "Quality gate status: Red (was Green)\n" +
159 "New quality gate threshold: violations > 4\n" +
161 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo&branch=feature"));
165 public void shouldFormatBackToGreenMessage() {
166 Notification notification = createNotification("Green (was Red)", "", "OK", "false");
168 EmailMessage message = template.format(notification);
169 assertThat(message.getMessageId(), is("alerts/45"));
170 assertThat(message.getSubject(), is("\"Foo\" is back to green"));
171 assertThat(message.getMessage(), is("" +
173 "Version: V1-SNAP\n" +
174 "Quality gate status: Green (was Red)\n" +
177 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo"));
181 public void shouldFormatBackToGreenMessageOnBranch() {
182 Notification notification = createNotification("Green (was Red)", "", "OK", "false")
183 .setFieldValue("branch", "feature");
185 EmailMessage message = template.format(notification);
186 assertThat(message.getMessageId(), is("alerts/45"));
187 assertThat(message.getSubject(), is("\"Foo (feature)\" is back to green"));
188 assertThat(message.getMessage(), is("" +
190 "Branch: feature\n" +
191 "Version: V1-SNAP\n" +
192 "Quality gate status: Green (was Red)\n" +
195 "More details at: http://nemo.sonarsource.org/dashboard?id=org.sonar.foo:foo&branch=feature"));
198 private Notification createNotification(String alertName, String alertText, String alertLevel, String isNewAlert) {
199 return new Notification("alerts")
200 .setFieldValue("projectName", "Foo")
201 .setFieldValue("projectKey", "org.sonar.foo:foo")
202 .setFieldValue("projectId", "45")
203 .setFieldValue("projectVersion", "V1-SNAP")
204 .setFieldValue("alertName", alertName)
205 .setFieldValue("alertText", alertText)
206 .setFieldValue("alertLevel", alertLevel)
207 .setFieldValue("isNewAlert", isNewAlert);