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.
21 package org.sonar.server.issue.notification;
23 import org.apache.commons.io.IOUtils;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.invocation.InvocationOnMock;
27 import org.mockito.stubbing.Answer;
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;
32 import org.sonar.server.user.index.UserDoc;
33 import org.sonar.server.user.index.UserIndex;
35 import java.nio.charset.StandardCharsets;
36 import java.util.Date;
37 import java.util.Locale;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.mockito.Matchers.any;
41 import static org.mockito.Matchers.anyString;
42 import static org.mockito.Matchers.eq;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.COMPONENT;
46 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.DEBT;
47 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.RULE;
48 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.SEVERITY;
49 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.TAG;
51 public class MyNewIssuesEmailTemplateTest {
53 MyNewIssuesEmailTemplate underTest;
60 EmailSettings settings = mock(EmailSettings.class);
61 when(settings.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
62 i18n = mock(DefaultI18n.class);
64 userIndex = mock(UserIndex.class);
65 // returns the login passed in parameter
66 when(userIndex.getNullableByLogin(anyString())).thenAnswer(new Answer<UserDoc>() {
68 public UserDoc answer(InvocationOnMock invocationOnMock) throws Throwable {
69 return new UserDoc().setName((String) invocationOnMock.getArguments()[0]);
72 when(i18n.message(any(Locale.class), eq("severity.BLOCKER"), anyString())).thenReturn("Blocker");
73 when(i18n.message(any(Locale.class), eq("severity.CRITICAL"), anyString())).thenReturn("Critical");
74 when(i18n.message(any(Locale.class), eq("severity.MAJOR"), anyString())).thenReturn("Major");
75 when(i18n.message(any(Locale.class), eq("severity.MINOR"), anyString())).thenReturn("Minor");
76 when(i18n.message(any(Locale.class), eq("severity.INFO"), anyString())).thenReturn("Info");
78 underTest = new MyNewIssuesEmailTemplate(settings, i18n);
82 public void no_format_if_not_the_correct_notif() {
83 Notification notification = new Notification("new-issues");
84 EmailMessage message = underTest.format(notification);
85 assertThat(message).isNull();
89 public void format_email_with_all_fields_filled() throws Exception {
90 Notification notification = newNotification();
91 addTags(notification);
92 addRules(notification);
93 addComponents(notification);
95 EmailMessage message = underTest.format(notification);
97 // TODO datetime to be completed when test is isolated from JVM timezone
98 String file = IOUtils.toString(getClass().getResource("MyNewIssuesEmailTemplateTest/email_with_all_details.txt"), StandardCharsets.UTF_8);
99 assertThat(message.getMessage()).startsWith(file);
103 public void message_id() {
104 Notification notification = newNotification();
106 EmailMessage message = underTest.format(notification);
108 assertThat(message.getMessageId()).isEqualTo("my-new-issues/org.apache:struts");
112 public void subject() {
113 Notification notification = newNotification();
115 EmailMessage message = underTest.format(notification);
117 assertThat(message.getSubject()).isEqualTo("You have 32 new issues on project Struts");
121 public void format_email_with_no_assignees_tags_nor_components() throws Exception {
122 Notification notification = newNotification();
124 EmailMessage message = underTest.format(notification);
126 // TODO datetime to be completed when test is isolated from JVM timezone
127 String file = IOUtils.toString(getClass().getResource("MyNewIssuesEmailTemplateTest/email_with_no_assignee_tags_components.txt"), StandardCharsets.UTF_8);
128 assertThat(message.getMessage()).startsWith(file);
132 public void do_not_add_footer_when_properties_missing() {
133 Notification notification = new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
134 .setFieldValue(SEVERITY + ".count", "32")
135 .setFieldValue("projectName", "Struts");
137 EmailMessage message = underTest.format(notification);
138 assertThat(message.getMessage()).doesNotContain("See it");
141 private Notification newNotification() {
142 return new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
143 .setFieldValue("projectName", "Struts")
144 .setFieldValue("projectKey", "org.apache:struts")
145 .setFieldValue("projectUuid", "ABCDE")
146 .setFieldValue("projectDate", "2010-05-18T14:50:45+0000")
147 .setFieldValue("assignee", "lo.gin")
148 .setFieldValue(DEBT + ".count", "1d3h")
149 .setFieldValue(SEVERITY + ".count", "32")
150 .setFieldValue(SEVERITY + ".INFO.count", "1")
151 .setFieldValue(SEVERITY + ".MINOR.count", "3")
152 .setFieldValue(SEVERITY + ".MAJOR.count", "10")
153 .setFieldValue(SEVERITY + ".CRITICAL.count", "5")
154 .setFieldValue(SEVERITY + ".BLOCKER.count", "0");
157 private void addTags(Notification notification) {
159 .setFieldValue(TAG + ".1.label", "oscar")
160 .setFieldValue(TAG + ".1.count", "3")
161 .setFieldValue(TAG + ".2.label", "cesar")
162 .setFieldValue(TAG + ".2.count", "10");
165 private void addComponents(Notification notification) {
167 .setFieldValue(COMPONENT + ".1.label", "/path/to/file")
168 .setFieldValue(COMPONENT + ".1.count", "3")
169 .setFieldValue(COMPONENT + ".2.label", "/path/to/directory")
170 .setFieldValue(COMPONENT + ".2.count", "7");
173 private void addRules(Notification notification) {
175 .setFieldValue(RULE + ".1.label", "Rule the Universe (Clojure)")
176 .setFieldValue(RULE + ".1.count", "42")
177 .setFieldValue(RULE + ".2.label", "Rule the World (Java)")
178 .setFieldValue(RULE + ".2.count", "5");