2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.plugins.emailnotifications.templates.alerts;
22 import org.sonar.plugins.emailnotifications.templates.alerts.AlertsEmailTemplate;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.config.EmailSettings;
27 import org.sonar.api.notifications.Notification;
28 import org.sonar.plugins.emailnotifications.api.EmailMessage;
30 import static org.hamcrest.Matchers.is;
31 import static org.hamcrest.Matchers.nullValue;
32 import static org.junit.Assert.assertThat;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
36 public class AlertsEmailTemplateTest {
38 private AlertsEmailTemplate template;
42 EmailSettings configuration = mock(EmailSettings.class);
43 when(configuration.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
44 template = new AlertsEmailTemplate(configuration);
48 public void shouldNotFormatIfNotCorrectNotification() {
49 Notification notification = new Notification("other-notif");
50 EmailMessage message = template.format(notification);
51 assertThat(message, nullValue());
55 public void shouldFormatAlertWithSeveralMessages() {
56 Notification notification = createNotification("Orange (was Red)", "violations > 4, coverage < 75%", "WARN", "false");
58 EmailMessage message = template.format(notification);
59 assertThat(message.getMessageId(), is("alerts/45"));
60 assertThat(message.getSubject(), is("Alert level changed on \"Foo\""));
61 assertThat(message.getMessage(), is("" +
63 "Alert level: Orange (was Red)\n" +
66 " - violations > 4\n" +
67 " - coverage < 75%\n" +
69 "See it in SonarQube: http://nemo.sonarsource.org/dashboard/index/org.sonar.foo:foo"));
73 public void shouldFormatNewAlertWithSeveralMessages() {
74 Notification notification = createNotification("Orange (was Red)", "violations > 4, coverage < 75%", "WARN", "true");
76 EmailMessage message = template.format(notification);
77 assertThat(message.getMessageId(), is("alerts/45"));
78 assertThat(message.getSubject(), is("New alert on \"Foo\""));
79 assertThat(message.getMessage(), is("" +
81 "Alert level: Orange (was Red)\n" +
84 " - violations > 4\n" +
85 " - coverage < 75%\n" +
87 "See it in SonarQube: http://nemo.sonarsource.org/dashboard/index/org.sonar.foo:foo"));
91 public void shouldFormatNewAlertWithOneMessage() {
92 Notification notification = createNotification("Orange (was Red)", "violations > 4", "WARN", "true");
94 EmailMessage message = template.format(notification);
95 assertThat(message.getMessageId(), is("alerts/45"));
96 assertThat(message.getSubject(), is("New alert on \"Foo\""));
97 assertThat(message.getMessage(), is("" +
99 "Alert level: Orange (was Red)\n" +
101 "New alert: violations > 4\n" +
103 "See it in SonarQube: http://nemo.sonarsource.org/dashboard/index/org.sonar.foo:foo"));
107 public void shouldFormatBackToGreenMessage() {
108 Notification notification = createNotification("Green (was Red)", "", "OK", "false");
110 EmailMessage message = template.format(notification);
111 assertThat(message.getMessageId(), is("alerts/45"));
112 assertThat(message.getSubject(), is("\"Foo\" is back to green"));
113 assertThat(message.getMessage(), is("" +
115 "Alert level: Green (was Red)\n" +
118 "See it in SonarQube: http://nemo.sonarsource.org/dashboard/index/org.sonar.foo:foo"));
121 private Notification createNotification(String alertName, String alertText, String alertLevel, String isNewAlert) {
122 Notification notification = new Notification("alerts")
123 .setFieldValue("projectName", "Foo")
124 .setFieldValue("projectKey", "org.sonar.foo:foo")
125 .setFieldValue("projectId", "45")
126 .setFieldValue("alertName", alertName)
127 .setFieldValue("alertText", alertText)
128 .setFieldValue("alertLevel", alertLevel)
129 .setFieldValue("isNewAlert", isNewAlert);