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.Locale;
25 import org.apache.commons.io.IOUtils;
26 import org.junit.Before;
27 import org.junit.Test;
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 static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Matchers.any;
38 import static org.mockito.Matchers.anyString;
39 import static org.mockito.Matchers.eq;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.ASSIGNEE;
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 NewIssuesEmailTemplateTest {
51 NewIssuesEmailTemplate template;
57 EmailSettings settings = mock(EmailSettings.class);
58 when(settings.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
59 i18n = mock(DefaultI18n.class);
60 userIndex = mock(UserIndex.class);
61 // returns the login passed in parameter
62 when(userIndex.getNullableByLogin(anyString()))
63 .thenAnswer((Answer<UserDoc>) invocationOnMock -> new UserDoc().setName((String) invocationOnMock.getArguments()[0]));
64 when(i18n.message(any(Locale.class), eq("issue.type.CODE_SMELL"), anyString())).thenReturn("Code Smell");
65 when(i18n.message(any(Locale.class), eq("issue.type.VULNERABILITY"), anyString())).thenReturn("Vulnerability");
66 when(i18n.message(any(Locale.class), eq("issue.type.BUG"), anyString())).thenReturn("Bug");
68 template = new NewIssuesEmailTemplate(settings, i18n);
72 public void no_format_is_not_the_correct_notification() {
73 Notification notification = new Notification("my-new-issues");
74 EmailMessage message = template.format(notification);
75 assertThat(message).isNull();
79 public void message_id() {
80 Notification notification = newNotification(32);
82 EmailMessage message = template.format(notification);
84 assertThat(message.getMessageId()).isEqualTo("new-issues/org.apache:struts");
88 public void subject() {
89 Notification notification = newNotification(32);
91 EmailMessage message = template.format(notification);
93 assertThat(message.getSubject()).isEqualTo("Struts: 32 new issues (new debt: 1d3h)");
97 public void subject_on_branch() throws Exception {
98 Notification notification = newNotification(32)
99 .setFieldValue("branch", "feature1");
101 EmailMessage message = template.format(notification);
103 assertThat(message.getSubject()).isEqualTo("Struts (feature1): 32 new issues (new debt: 1d3h)");
107 public void format_email_with_all_fields_filled() throws Exception {
108 Notification notification = newNotification(32)
109 .setFieldValue("projectVersion", "42.1.1");
110 addAssignees(notification);
111 addRules(notification);
112 addTags(notification);
113 addComponents(notification);
115 EmailMessage message = template.format(notification);
117 // TODO datetime to be completed when test is isolated from JVM timezone
118 assertThat(message.getMessage())
119 .startsWith("Project: Struts\n" +
120 "Version: 42.1.1\n" +
122 "32 new issues (new debt: 1d3h)\n" +
125 " Bug: 1 Vulnerability: 10 Code Smell: 3\n" +
128 " robin.williams: 5\n" +
132 " Rule the Universe (Clojure): 42\n" +
133 " Rule the World (Java): 5\n" +
139 " Most impacted files\n" +
140 " /path/to/file: 3\n" +
141 " /path/to/directory: 7\n" +
143 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&createdAt=2010-05-1");
147 public void format_email_with_no_assignees_tags_nor_components_nor_version() throws Exception {
148 Notification notification = newNotification(32);
150 EmailMessage message = template.format(notification);
152 // TODO datetime to be completed when test is isolated from JVM timezone
153 assertThat(message.getMessage())
154 .startsWith("Project: Struts\n" +
156 "32 new issues (new debt: 1d3h)\n" +
159 " Bug: 1 Vulnerability: 10 Code Smell: 3\n" +
161 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&createdAt=2010-05-1");
165 public void format_email_supports_single_issue() {
166 Notification notification = newNotification(1);
168 EmailMessage message = template.format(notification);
170 assertThat(message.getSubject())
171 .isEqualTo("Struts: 1 new issue (new debt: 1d3h)");
172 assertThat(message.getMessage())
173 .contains("1 new issue (new debt: 1d3h)\n");
177 public void format_email_with_issue_on_branch() throws Exception {
178 Notification notification = newNotification(32)
179 .setFieldValue("branch", "feature1");
181 EmailMessage message = template.format(notification);
183 // TODO datetime to be completed when test is isolated from JVM timezone
184 assertThat(message.getMessage())
185 .startsWith("Project: Struts (feature1)\n" +
187 "32 new issues (new debt: 1d3h)\n" +
190 " Bug: 1 Vulnerability: 10 Code Smell: 3\n" +
192 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&branch=feature1&createdAt=2010-05-1");
196 public void format_email_with_issue_on_branch_with_version() throws Exception {
197 Notification notification = newNotification(32)
198 .setFieldValue("branch", "feature1")
199 .setFieldValue("projectVersion", "42.1.1");
201 EmailMessage message = template.format(notification);
203 // TODO datetime to be completed when test is isolated from JVM timezone
204 assertThat(message.getMessage())
205 .startsWith("Project: Struts (feature1)\n" +
206 "Version: 42.1.1\n" +
208 "32 new issues (new debt: 1d3h)\n" +
211 " Bug: 1 Vulnerability: 10 Code Smell: 3\n" +
213 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&branch=feature1&createdAt=2010-05-1");
217 public void do_not_add_footer_when_properties_missing() {
218 Notification notification = new Notification(NewIssuesNotification.TYPE)
219 .setFieldValue(RULE_TYPE + ".count", "32")
220 .setFieldValue("projectName", "Struts");
222 EmailMessage message = template.format(notification);
224 assertThat(message.getMessage()).doesNotContain("See it");
227 private Notification newNotification(int count) {
228 return new Notification(NewIssuesNotification.TYPE)
229 .setFieldValue("projectName", "Struts")
230 .setFieldValue("projectKey", "org.apache:struts")
231 .setFieldValue("projectDate", "2010-05-18T14:50:45+0000")
232 .setFieldValue(EFFORT + ".count", "1d3h")
233 .setFieldValue(RULE_TYPE + ".count", String.valueOf(count))
234 .setFieldValue(RULE_TYPE + ".BUG.count", "1")
235 .setFieldValue(RULE_TYPE + ".CODE_SMELL.count", "3")
236 .setFieldValue(RULE_TYPE + ".VULNERABILITY.count", "10");
239 private void addAssignees(Notification notification) {
241 .setFieldValue(ASSIGNEE + ".1.label", "robin.williams")
242 .setFieldValue(ASSIGNEE + ".1.count", "5")
243 .setFieldValue(ASSIGNEE + ".2.label", "al.pacino")
244 .setFieldValue(ASSIGNEE + ".2.count", "7");
247 private void addTags(Notification notification) {
249 .setFieldValue(TAG + ".1.label", "oscar")
250 .setFieldValue(TAG + ".1.count", "3")
251 .setFieldValue(TAG + ".2.label", "cesar")
252 .setFieldValue(TAG + ".2.count", "10");
255 private void addComponents(Notification notification) {
257 .setFieldValue(COMPONENT + ".1.label", "/path/to/file")
258 .setFieldValue(COMPONENT + ".1.count", "3")
259 .setFieldValue(COMPONENT + ".2.label", "/path/to/directory")
260 .setFieldValue(COMPONENT + ".2.count", "7");
263 private void addRules(Notification notification) {
265 .setFieldValue(RULE + ".1.label", "Rule the Universe (Clojure)")
266 .setFieldValue(RULE + ".1.count", "42")
267 .setFieldValue(RULE + ".2.label", "Rule the World (Java)")
268 .setFieldValue(RULE + ".2.count", "5");
271 private void assertStartsWithFile(String message, String resourcePath) throws IOException {
272 String fileContent = IOUtils.toString(getClass().getResource(resourcePath), StandardCharsets.UTF_8);
273 assertThat(sanitizeString(message)).startsWith(sanitizeString(fileContent));
277 * sanitize EOL and tabs if git clone is badly configured
279 private static String sanitizeString(String s) {
280 return s.replaceAll("\\r\\n|\\r|\\s+", "");