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;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Date;
26 import java.util.Locale;
27 import org.apache.commons.io.IOUtils;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.invocation.InvocationOnMock;
31 import org.mockito.stubbing.Answer;
32 import org.sonar.api.config.EmailSettings;
33 import org.sonar.api.notifications.Notification;
34 import org.sonar.core.i18n.DefaultI18n;
35 import org.sonar.plugins.emailnotifications.api.EmailMessage;
36 import org.sonar.server.user.index.UserDoc;
37 import org.sonar.server.user.index.UserIndex;
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 assertStartsWithFile(message.getMessage(), getClass().getResource("MyNewIssuesEmailTemplateTest/email_with_all_details.txt"));
102 public void message_id() {
103 Notification notification = newNotification();
105 EmailMessage message = underTest.format(notification);
107 assertThat(message.getMessageId()).isEqualTo("my-new-issues/org.apache:struts");
111 public void subject() {
112 Notification notification = newNotification();
114 EmailMessage message = underTest.format(notification);
116 assertThat(message.getSubject()).isEqualTo("You have 32 new issues on project Struts");
120 public void format_email_with_no_assignees_tags_nor_components() throws Exception {
121 Notification notification = newNotification();
123 EmailMessage message = underTest.format(notification);
125 // TODO datetime to be completed when test is isolated from JVM timezone
126 assertStartsWithFile(message.getMessage(), getClass().getResource("MyNewIssuesEmailTemplateTest/email_with_no_assignee_tags_components.txt"));
130 public void do_not_add_footer_when_properties_missing() {
131 Notification notification = new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
132 .setFieldValue(SEVERITY + ".count", "32")
133 .setFieldValue("projectName", "Struts");
135 EmailMessage message = underTest.format(notification);
136 assertThat(message.getMessage()).doesNotContain("See it");
139 private Notification newNotification() {
140 return new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
141 .setFieldValue("projectName", "Struts")
142 .setFieldValue("projectKey", "org.apache:struts")
143 .setFieldValue("projectUuid", "ABCDE")
144 .setFieldValue("projectDate", "2010-05-18T14:50:45+0000")
145 .setFieldValue("assignee", "lo.gin")
146 .setFieldValue(DEBT + ".count", "1d3h")
147 .setFieldValue(SEVERITY + ".count", "32")
148 .setFieldValue(SEVERITY + ".INFO.count", "1")
149 .setFieldValue(SEVERITY + ".MINOR.count", "3")
150 .setFieldValue(SEVERITY + ".MAJOR.count", "10")
151 .setFieldValue(SEVERITY + ".CRITICAL.count", "5")
152 .setFieldValue(SEVERITY + ".BLOCKER.count", "0");
155 private void addTags(Notification notification) {
157 .setFieldValue(TAG + ".1.label", "oscar")
158 .setFieldValue(TAG + ".1.count", "3")
159 .setFieldValue(TAG + ".2.label", "cesar")
160 .setFieldValue(TAG + ".2.count", "10");
163 private void addComponents(Notification notification) {
165 .setFieldValue(COMPONENT + ".1.label", "/path/to/file")
166 .setFieldValue(COMPONENT + ".1.count", "3")
167 .setFieldValue(COMPONENT + ".2.label", "/path/to/directory")
168 .setFieldValue(COMPONENT + ".2.count", "7");
171 private void addRules(Notification notification) {
173 .setFieldValue(RULE + ".1.label", "Rule the Universe (Clojure)")
174 .setFieldValue(RULE + ".1.count", "42")
175 .setFieldValue(RULE + ".2.label", "Rule the World (Java)")
176 .setFieldValue(RULE + ".2.count", "5");
179 private static void assertStartsWithFile(String message, URL file) throws IOException {
180 String fileContent = IOUtils.toString(file, StandardCharsets.UTF_8);
181 assertThat(sanitizeString(message)).startsWith(sanitizeString(fileContent));
185 * sanitize EOL and tabs if git clone is badly configured
187 private static String sanitizeString(String s) {
188 return s.replaceAll("\\r\\n|\\r|\\s+", "");