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 java.nio.charset.StandardCharsets;
24 import java.util.Date;
25 import java.util.Locale;
26 import org.apache.commons.io.IOUtils;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.stubbing.Answer;
30 import org.sonar.api.config.EmailSettings;
31 import org.sonar.api.notifications.Notification;
32 import org.sonar.core.i18n.DefaultI18n;
33 import org.sonar.plugins.emailnotifications.api.EmailMessage;
34 import org.sonar.server.user.index.UserDoc;
35 import org.sonar.server.user.index.UserIndex;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.Matchers.any;
39 import static org.mockito.Matchers.anyString;
40 import static org.mockito.Matchers.eq;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
43 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.COMPONENT;
44 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.EFFORT;
45 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.RULE;
46 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.RULE_TYPE;
47 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.TAG;
49 public class MyNewIssuesEmailTemplateTest {
51 MyNewIssuesEmailTemplate underTest;
58 EmailSettings settings = mock(EmailSettings.class);
59 when(settings.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
60 i18n = mock(DefaultI18n.class);
62 userIndex = mock(UserIndex.class);
63 // returns the login passed in parameter
64 when(userIndex.getNullableByLogin(anyString()))
65 .thenAnswer((Answer<UserDoc>) invocationOnMock -> new UserDoc().setName((String) invocationOnMock.getArguments()[0]));
66 when(i18n.message(any(Locale.class), eq("issue.type.BUG"), anyString())).thenReturn("Bug");
67 when(i18n.message(any(Locale.class), eq("issue.type.CODE_SMELL"), anyString())).thenReturn("Code Smell");
68 when(i18n.message(any(Locale.class), eq("issue.type.VULNERABILITY"), anyString())).thenReturn("Vulnerability");
70 underTest = new MyNewIssuesEmailTemplate(settings, i18n);
74 public void no_format_if_not_the_correct_notif() {
75 Notification notification = new Notification("new-issues");
76 EmailMessage message = underTest.format(notification);
77 assertThat(message).isNull();
81 public void format_email_with_all_fields_filled() throws Exception {
82 Notification notification = newNotification(32);
83 addTags(notification);
84 addRules(notification);
85 addComponents(notification);
87 EmailMessage message = underTest.format(notification);
89 // TODO datetime to be completed when test is isolated from JVM timezone
90 assertThat(message.getMessage()).startsWith(
93 "32 new issues (new debt: 1d3h)\n" +
96 " Bug: 1 Vulnerability: 3 Code Smell: 0\n" +
99 " Rule the Universe (Clojure): 42\n" +
100 " Rule the World (Java): 5\n" +
106 " Most impacted files\n" +
107 " /path/to/file: 3\n" +
108 " /path/to/directory: 7\n" +
110 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&assignees=lo.gin&createdAt=2010-05-18");
114 public void message_id() {
115 Notification notification = newNotification(32);
117 EmailMessage message = underTest.format(notification);
119 assertThat(message.getMessageId()).isEqualTo("my-new-issues/org.apache:struts");
123 public void subject() {
124 Notification notification = newNotification(32);
126 EmailMessage message = underTest.format(notification);
128 assertThat(message.getSubject()).isEqualTo("You have 32 new issues on project Struts");
132 public void subject_on_branch() {
133 Notification notification = newNotification(32)
134 .setFieldValue("branch", "feature1");
136 EmailMessage message = underTest.format(notification);
138 assertThat(message.getSubject()).isEqualTo("You have 32 new issues on project Struts (feature1)");
142 public void format_email_with_no_assignees_tags_nor_components() throws Exception {
143 Notification notification = newNotification(32)
144 .setFieldValue("projectVersion", "52.0");
146 EmailMessage message = underTest.format(notification);
148 // TODO datetime to be completed when test is isolated from JVM timezone
149 assertThat(message.getMessage())
150 .startsWith("Project: Struts\n" +
153 "32 new issues (new debt: 1d3h)\n" +
156 " Bug: 1 Vulnerability: 3 Code Smell: 0\n" +
158 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&assignees=lo.gin&createdAt=2010-05-18");
162 public void format_email_with_issue_on_branch() throws Exception {
163 Notification notification = newNotification(32)
164 .setFieldValue("projectVersion", "52.0")
165 .setFieldValue("branch", "feature1");
167 EmailMessage message = underTest.format(notification);
169 // TODO datetime to be completed when test is isolated from JVM timezone
170 assertThat(message.getMessage())
171 .startsWith("Project: Struts (feature1)\n" +
174 "32 new issues (new debt: 1d3h)\n" +
177 " Bug: 1 Vulnerability: 3 Code Smell: 0\n" +
179 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&assignees=lo.gin&branch=feature1&createdAt=2010-05-18");
183 public void format_email_supports_single_issue() {
184 Notification notification = newNotification(1);
186 EmailMessage message = underTest.format(notification);
188 assertThat(message.getSubject())
189 .isEqualTo("You have 1 new issue on project Struts");
190 assertThat(message.getMessage())
191 .contains("1 new issue (new debt: 1d3h)\n");
195 public void format_supports_null_version() {
196 Notification notification = newNotification(32)
197 .setFieldValue("branch", "feature1");
199 EmailMessage message = underTest.format(notification);
201 // TODO datetime to be completed when test is isolated from JVM timezone
202 assertThat(message.getMessage())
203 .startsWith("Project: Struts (feature1)\n" +
205 "32 new issues (new debt: 1d3h)\n" +
208 " Bug: 1 Vulnerability: 3 Code Smell: 0\n" +
210 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&assignees=lo.gin&branch=feature1&createdAt=2010-05-18");
214 public void do_not_add_footer_when_properties_missing() {
215 Notification notification = new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
216 .setFieldValue(RULE_TYPE + ".count", "32")
217 .setFieldValue("projectName", "Struts");
219 EmailMessage message = underTest.format(notification);
220 assertThat(message.getMessage()).doesNotContain("See it");
223 private Notification newNotification(int count) {
224 return new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
225 .setFieldValue("projectName", "Struts")
226 .setFieldValue("projectKey", "org.apache:struts")
227 .setFieldValue("projectUuid", "ABCDE")
228 .setFieldValue("projectDate", "2010-05-18T14:50:45+0000")
229 .setFieldValue("assignee", "lo.gin")
230 .setFieldValue(EFFORT + ".count", "1d3h")
231 .setFieldValue(RULE_TYPE + ".count", String.valueOf(count))
232 .setFieldValue(RULE_TYPE + ".BUG.count", "1")
233 .setFieldValue(RULE_TYPE + ".VULNERABILITY.count", "3")
234 .setFieldValue(RULE_TYPE + ".CODE_SMELL.count", "0");
237 private void addTags(Notification notification) {
239 .setFieldValue(TAG + ".1.label", "oscar")
240 .setFieldValue(TAG + ".1.count", "3")
241 .setFieldValue(TAG + ".2.label", "cesar")
242 .setFieldValue(TAG + ".2.count", "10");
245 private void addComponents(Notification notification) {
247 .setFieldValue(COMPONENT + ".1.label", "/path/to/file")
248 .setFieldValue(COMPONENT + ".1.count", "3")
249 .setFieldValue(COMPONENT + ".2.label", "/path/to/directory")
250 .setFieldValue(COMPONENT + ".2.count", "7");
253 private void addRules(Notification notification) {
255 .setFieldValue(RULE + ".1.label", "Rule the Universe (Clojure)")
256 .setFieldValue(RULE + ".1.count", "42")
257 .setFieldValue(RULE + ".2.label", "Rule the World (Java)")
258 .setFieldValue(RULE + ".2.count", "5");
261 private void assertStartsWithFile(String message, String resourcePath) throws IOException {
262 String fileContent = IOUtils.toString(getClass().getResource(resourcePath), StandardCharsets.UTF_8);
263 assertThat(sanitizeString(message)).startsWith(sanitizeString(fileContent));
267 * sanitize EOL and tabs if git clone is badly configured
269 private static String sanitizeString(String s) {
270 return s.replaceAll("\\r\\n|\\r|\\s+", "");