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.core.issue.notification;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.sonar.api.config.EmailSettings;
29 import org.sonar.api.notifications.Notification;
30 import org.sonar.core.i18n.DefaultI18n;
31 import org.sonar.plugins.emailnotifications.api.EmailMessage;
33 import java.util.Locale;
34 import java.util.TimeZone;
36 import static org.fest.assertions.Assertions.assertThat;
37 import static org.mockito.Matchers.*;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
41 @RunWith(MockitoJUnitRunner.class)
42 public class NewIssuesEmailTemplateTest {
44 NewIssuesEmailTemplate template;
45 TimeZone initialTimeZone = TimeZone.getDefault();
52 EmailSettings settings = mock(EmailSettings.class);
53 when(settings.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
54 template = new NewIssuesEmailTemplate(settings, i18n);
55 TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
59 public void tearDown() {
60 TimeZone.setDefault(initialTimeZone);
65 public void shouldNotFormatIfNotCorrectNotification() {
66 Notification notification = new Notification("other-notif");
67 EmailMessage message = template.format(notification);
68 assertThat(message).isNull();
73 * Subject: Project Struts, new issues
79 * See it in SonarQube: http://nemo.sonarsource.org/drilldown/measures/org.sonar.foo:foo?metric=new_violations
83 public void shouldFormatCommentAdded() {
84 Notification notification = new Notification("new-issues")
85 .setFieldValue("count", "32")
86 .setFieldValue("count-INFO", "1")
87 .setFieldValue("count-MINOR", "3")
88 .setFieldValue("count-MAJOR", "10")
89 .setFieldValue("count-CRITICAL", "5")
90 .setFieldValue("count-BLOCKER", "0")
91 .setFieldValue("projectName", "Struts")
92 .setFieldValue("projectKey", "org.apache:struts")
93 .setFieldValue("projectDate", "2010-05-18T14:50:45+0000");
95 when(i18n.message(any(Locale.class), eq("severity.BLOCKER"), anyString())).thenReturn("Blocker");
96 when(i18n.message(any(Locale.class), eq("severity.CRITICAL"), anyString())).thenReturn("Critical");
97 when(i18n.message(any(Locale.class), eq("severity.MAJOR"), anyString())).thenReturn("Major");
98 when(i18n.message(any(Locale.class), eq("severity.MINOR"), anyString())).thenReturn("Minor");
99 when(i18n.message(any(Locale.class), eq("severity.INFO"), anyString())).thenReturn("Info");
101 EmailMessage message = template.format(notification);
102 assertThat(message.getMessageId()).isEqualTo("new-issues/org.apache:struts");
103 assertThat(message.getSubject()).isEqualTo("Struts: new issues");
104 assertThat(message.getMessage()).isEqualTo("" +
105 "Project: Struts\n" +
109 " Blocker: 0 Critical: 5 Major: 10 Minor: 3 Info: 1\n" +
111 "See it in SonarQube: http://nemo.sonarsource.org/issues/search#componentRoots=org.apache%3Astruts|createdAt=2010-05-18T14%3A50%3A45%2B0000\n");
115 public void shouldNotAddFooterIfMissingProperties() {
116 Notification notification = new Notification("new-issues")
117 .setFieldValue("count", "32")
118 .setFieldValue("projectName", "Struts");
120 EmailMessage message = template.format(notification);
121 assertThat(message.getMessage()).doesNotContain("See it");