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 java.io.IOException;
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.invocation.InvocationOnMock;
28 import org.mockito.stubbing.Answer;
29 import org.sonar.api.config.EmailSettings;
30 import org.sonar.api.notifications.Notification;
31 import org.sonar.core.i18n.DefaultI18n;
32 import org.sonar.plugins.emailnotifications.api.EmailMessage;
33 import org.sonar.server.user.index.UserDoc;
34 import org.sonar.server.user.index.UserIndex;
36 import java.nio.charset.StandardCharsets;
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.ASSIGNEE;
46 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.COMPONENT;
47 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.DEBT;
48 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.RULE;
49 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.SEVERITY;
50 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.TAG;
52 public class NewIssuesEmailTemplateTest {
54 NewIssuesEmailTemplate template;
60 EmailSettings settings = mock(EmailSettings.class);
61 when(settings.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
62 i18n = mock(DefaultI18n.class);
63 userIndex = mock(UserIndex.class);
64 // returns the login passed in parameter
65 when(userIndex.getNullableByLogin(anyString())).thenAnswer(new Answer<UserDoc>() {
67 public UserDoc answer(InvocationOnMock invocationOnMock) throws Throwable {
68 return new UserDoc().setName((String) invocationOnMock.getArguments()[0]);
71 when(i18n.message(any(Locale.class), eq("severity.BLOCKER"), anyString())).thenReturn("Blocker");
72 when(i18n.message(any(Locale.class), eq("severity.CRITICAL"), anyString())).thenReturn("Critical");
73 when(i18n.message(any(Locale.class), eq("severity.MAJOR"), anyString())).thenReturn("Major");
74 when(i18n.message(any(Locale.class), eq("severity.MINOR"), anyString())).thenReturn("Minor");
75 when(i18n.message(any(Locale.class), eq("severity.INFO"), anyString())).thenReturn("Info");
77 template = new NewIssuesEmailTemplate(settings, i18n);
81 public void no_format_is_not_the_correct_notification() {
82 Notification notification = new Notification("my-new-issues");
83 EmailMessage message = template.format(notification);
84 assertThat(message).isNull();
88 public void message_id() {
89 Notification notification = newNotification();
91 EmailMessage message = template.format(notification);
93 assertThat(message.getMessageId()).isEqualTo("new-issues/org.apache:struts");
97 public void subject() {
98 Notification notification = newNotification();
100 EmailMessage message = template.format(notification);
102 assertThat(message.getSubject()).isEqualTo("Struts: 32 new issues (new debt: 1d3h)");
106 public void format_email_with_all_fields_filled() throws Exception {
107 Notification notification = newNotification();
108 addAssignees(notification);
109 addRules(notification);
110 addTags(notification);
111 addComponents(notification);
113 EmailMessage message = template.format(notification);
115 // TODO datetime to be completed when test is isolated from JVM timezone
116 assertStartsWithFile(message.getMessage(), "NewIssuesEmailTemplateTest/email_with_all_details.txt");
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 assertStartsWithFile(message.getMessage(), "NewIssuesEmailTemplateTest/email_with_partial_details.txt");
130 public void format_email_with_issue_on_branch() throws Exception {
131 Notification notification = newNotification()
132 .setFieldValue("branch", "feature1");
134 EmailMessage message = template.format(notification);
136 // TODO datetime to be completed when test is isolated from JVM timezone
137 assertStartsWithFile(message.getMessage(), "NewIssuesEmailTemplateTest/email_with_issue_on_branch.txt");
141 public void do_not_add_footer_when_properties_missing() {
142 Notification notification = new Notification(NewIssuesNotification.TYPE)
143 .setFieldValue(SEVERITY + ".count", "32")
144 .setFieldValue("projectName", "Struts");
146 EmailMessage message = template.format(notification);
148 assertThat(message.getMessage()).doesNotContain("See it");
151 private Notification newNotification() {
152 return new Notification(NewIssuesNotification.TYPE)
153 .setFieldValue("projectName", "Struts")
154 .setFieldValue("projectKey", "org.apache:struts")
155 .setFieldValue("projectUuid", "ABCDE")
156 .setFieldValue("projectDate", "2010-05-18T14:50:45+0000")
157 .setFieldValue(DEBT + ".count", "1d3h")
158 .setFieldValue(SEVERITY + ".count", "32")
159 .setFieldValue(SEVERITY + ".INFO.count", "1")
160 .setFieldValue(SEVERITY + ".MINOR.count", "3")
161 .setFieldValue(SEVERITY + ".MAJOR.count", "10")
162 .setFieldValue(SEVERITY + ".CRITICAL.count", "5")
163 .setFieldValue(SEVERITY + ".BLOCKER.count", "0");
166 private void addAssignees(Notification notification) {
168 .setFieldValue(ASSIGNEE + ".1.label", "robin.williams")
169 .setFieldValue(ASSIGNEE + ".1.count", "5")
170 .setFieldValue(ASSIGNEE + ".2.label", "al.pacino")
171 .setFieldValue(ASSIGNEE + ".2.count", "7");
174 private void addTags(Notification notification) {
176 .setFieldValue(TAG + ".1.label", "oscar")
177 .setFieldValue(TAG + ".1.count", "3")
178 .setFieldValue(TAG + ".2.label", "cesar")
179 .setFieldValue(TAG + ".2.count", "10");
182 private void addComponents(Notification notification) {
184 .setFieldValue(COMPONENT + ".1.label", "/path/to/file")
185 .setFieldValue(COMPONENT + ".1.count", "3")
186 .setFieldValue(COMPONENT + ".2.label", "/path/to/directory")
187 .setFieldValue(COMPONENT + ".2.count", "7");
190 private void addRules(Notification notification) {
192 .setFieldValue(RULE + ".1.label", "Rule the Universe (Clojure)")
193 .setFieldValue(RULE + ".1.count", "42")
194 .setFieldValue(RULE + ".2.label", "Rule the World (Java)")
195 .setFieldValue(RULE + ".2.count", "5");
198 private void assertStartsWithFile(String message, String resourcePath) throws IOException {
199 String fileContent = IOUtils.toString(getClass().getResource(resourcePath), StandardCharsets.UTF_8);
200 assertThat(sanitizeString(message)).startsWith(sanitizeString(fileContent));
204 * sanitize EOL and tabs if git clone is badly configured
206 private static String sanitizeString(String s) {
207 return s.replaceAll("\\r\\n|\\r|\\s+", "");