3 * Copyright (C) 2009-2017 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.issue.notification;
22 import org.apache.commons.io.IOUtils;
23 import org.apache.commons.lang.StringUtils;
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.Locale;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Matchers.any;
40 import static org.mockito.Matchers.anyString;
41 import static org.mockito.Matchers.eq;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.ASSIGNEE;
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 NewIssuesEmailTemplateTest {
53 NewIssuesEmailTemplate template;
59 EmailSettings settings = mock(EmailSettings.class);
60 when(settings.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
61 i18n = mock(DefaultI18n.class);
62 userIndex = mock(UserIndex.class);
63 // returns the login passed in parameter
64 when(userIndex.getNullableByLogin(anyString())).thenAnswer(new Answer<UserDoc>() {
66 public UserDoc answer(InvocationOnMock invocationOnMock) throws Throwable {
67 return new UserDoc().setName((String) invocationOnMock.getArguments()[0]);
70 when(i18n.message(any(Locale.class), eq("severity.BLOCKER"), anyString())).thenReturn("Blocker");
71 when(i18n.message(any(Locale.class), eq("severity.CRITICAL"), anyString())).thenReturn("Critical");
72 when(i18n.message(any(Locale.class), eq("severity.MAJOR"), anyString())).thenReturn("Major");
73 when(i18n.message(any(Locale.class), eq("severity.MINOR"), anyString())).thenReturn("Minor");
74 when(i18n.message(any(Locale.class), eq("severity.INFO"), anyString())).thenReturn("Info");
76 template = new NewIssuesEmailTemplate(settings, i18n);
80 public void no_format_is_not_the_correct_notification() {
81 Notification notification = new Notification("my-new-issues");
82 EmailMessage message = template.format(notification);
83 assertThat(message).isNull();
87 public void message_id() {
88 Notification notification = newNotification();
90 EmailMessage message = template.format(notification);
92 assertThat(message.getMessageId()).isEqualTo("new-issues/org.apache:struts");
96 public void subject() {
97 Notification notification = newNotification();
99 EmailMessage message = template.format(notification);
101 assertThat(message.getSubject()).isEqualTo("Struts: 32 new issues (new debt: 1d3h)");
105 public void format_email_with_all_fields_filled() throws Exception {
106 Notification notification = newNotification();
107 addAssignees(notification);
108 addRules(notification);
109 addTags(notification);
110 addComponents(notification);
112 EmailMessage message = template.format(notification);
114 // TODO datetime to be completed when test is isolated from JVM timezone
115 String expectedContent = IOUtils.toString(getClass().getResource("NewIssuesEmailTemplateTest/email_with_all_details.txt"), StandardCharsets.UTF_8);
116 assertThat(message.getMessage()).startsWith(StringUtils.remove(expectedContent, '\r'));
120 public void format_email_with_no_assignees_tags_nor_components() throws Exception {
121 Notification notification = newNotification();
123 EmailMessage message = template.format(notification);
125 // TODO datetime to be completed when test is isolated from JVM timezone
126 String expectedContent = IOUtils.toString(getClass().getResource("NewIssuesEmailTemplateTest/email_with_partial_details.txt"), StandardCharsets.UTF_8);
127 assertThat(message.getMessage()).startsWith(StringUtils.remove(expectedContent, '\r'));
131 public void do_not_add_footer_when_properties_missing() {
132 Notification notification = new Notification(NewIssuesNotification.TYPE)
133 .setFieldValue(SEVERITY + ".count", "32")
134 .setFieldValue("projectName", "Struts");
136 EmailMessage message = template.format(notification);
138 assertThat(message.getMessage()).doesNotContain("See it");
141 private Notification newNotification() {
142 return new Notification(NewIssuesNotification.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(DEBT + ".count", "1d3h")
148 .setFieldValue(SEVERITY + ".count", "32")
149 .setFieldValue(SEVERITY + ".INFO.count", "1")
150 .setFieldValue(SEVERITY + ".MINOR.count", "3")
151 .setFieldValue(SEVERITY + ".MAJOR.count", "10")
152 .setFieldValue(SEVERITY + ".CRITICAL.count", "5")
153 .setFieldValue(SEVERITY + ".BLOCKER.count", "0");
156 private void addAssignees(Notification notification) {
158 .setFieldValue(ASSIGNEE + ".1.label", "robin.williams")
159 .setFieldValue(ASSIGNEE + ".1.count", "5")
160 .setFieldValue(ASSIGNEE + ".2.label", "al.pacino")
161 .setFieldValue(ASSIGNEE + ".2.count", "7");
164 private void addTags(Notification notification) {
166 .setFieldValue(TAG + ".1.label", "oscar")
167 .setFieldValue(TAG + ".1.count", "3")
168 .setFieldValue(TAG + ".2.label", "cesar")
169 .setFieldValue(TAG + ".2.count", "10");
172 private void addComponents(Notification notification) {
174 .setFieldValue(COMPONENT + ".1.label", "/path/to/file")
175 .setFieldValue(COMPONENT + ".1.count", "3")
176 .setFieldValue(COMPONENT + ".2.label", "/path/to/directory")
177 .setFieldValue(COMPONENT + ".2.count", "7");
180 private void addRules(Notification notification) {
182 .setFieldValue(RULE + ".1.label", "Rule the Universe (Clojure)")
183 .setFieldValue(RULE + ".1.count", "42")
184 .setFieldValue(RULE + ".2.label", "Rule the World (Java)")
185 .setFieldValue(RULE + ".2.count", "5");