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("rule_type.CODE_SMELL"), anyString())).thenReturn("Code Smell");
65 when(i18n.message(any(Locale.class), eq("rule_type.VULNERABILITY"), anyString())).thenReturn("Vulnerability");
66 when(i18n.message(any(Locale.class), eq("rule_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.getMessage())
171 .contains("1 new issue (new debt: 1d3h)\n");
175 public void format_email_with_issue_on_branch() throws Exception {
176 Notification notification = newNotification(32)
177 .setFieldValue("branch", "feature1");
179 EmailMessage message = template.format(notification);
181 // TODO datetime to be completed when test is isolated from JVM timezone
182 assertThat(message.getMessage())
183 .startsWith("Project: Struts (feature1)\n" +
185 "32 new issues (new debt: 1d3h)\n" +
188 " Bug: 1 Vulnerability: 10 Code Smell: 3\n" +
190 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&branch=feature1&createdAt=2010-05-1");
194 public void format_email_with_issue_on_branch_with_version() throws Exception {
195 Notification notification = newNotification(32)
196 .setFieldValue("branch", "feature1")
197 .setFieldValue("projectVersion", "42.1.1");
199 EmailMessage message = template.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" +
204 "Version: 42.1.1\n" +
206 "32 new issues (new debt: 1d3h)\n" +
209 " Bug: 1 Vulnerability: 10 Code Smell: 3\n" +
211 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&branch=feature1&createdAt=2010-05-1");
215 public void do_not_add_footer_when_properties_missing() {
216 Notification notification = new Notification(NewIssuesNotification.TYPE)
217 .setFieldValue(RULE_TYPE + ".count", "32")
218 .setFieldValue("projectName", "Struts");
220 EmailMessage message = template.format(notification);
222 assertThat(message.getMessage()).doesNotContain("See it");
225 private Notification newNotification(int count) {
226 return new Notification(NewIssuesNotification.TYPE)
227 .setFieldValue("projectName", "Struts")
228 .setFieldValue("projectKey", "org.apache:struts")
229 .setFieldValue("projectUuid", "ABCDE")
230 .setFieldValue("projectDate", "2010-05-18T14:50:45+0000")
231 .setFieldValue(EFFORT + ".count", "1d3h")
232 .setFieldValue(RULE_TYPE + ".count", String.valueOf(count))
233 .setFieldValue(RULE_TYPE + ".BUG.count", "1")
234 .setFieldValue(RULE_TYPE + ".CODE_SMELL.count", "3")
235 .setFieldValue(RULE_TYPE + ".VULNERABILITY.count", "10");
238 private void addAssignees(Notification notification) {
240 .setFieldValue(ASSIGNEE + ".1.label", "robin.williams")
241 .setFieldValue(ASSIGNEE + ".1.count", "5")
242 .setFieldValue(ASSIGNEE + ".2.label", "al.pacino")
243 .setFieldValue(ASSIGNEE + ".2.count", "7");
246 private void addTags(Notification notification) {
248 .setFieldValue(TAG + ".1.label", "oscar")
249 .setFieldValue(TAG + ".1.count", "3")
250 .setFieldValue(TAG + ".2.label", "cesar")
251 .setFieldValue(TAG + ".2.count", "10");
254 private void addComponents(Notification notification) {
256 .setFieldValue(COMPONENT + ".1.label", "/path/to/file")
257 .setFieldValue(COMPONENT + ".1.count", "3")
258 .setFieldValue(COMPONENT + ".2.label", "/path/to/directory")
259 .setFieldValue(COMPONENT + ".2.count", "7");
262 private void addRules(Notification notification) {
264 .setFieldValue(RULE + ".1.label", "Rule the Universe (Clojure)")
265 .setFieldValue(RULE + ".1.count", "42")
266 .setFieldValue(RULE + ".2.label", "Rule the World (Java)")
267 .setFieldValue(RULE + ".2.count", "5");
270 private void assertStartsWithFile(String message, String resourcePath) throws IOException {
271 String fileContent = IOUtils.toString(getClass().getResource(resourcePath), StandardCharsets.UTF_8);
272 assertThat(sanitizeString(message)).startsWith(sanitizeString(fileContent));
276 * sanitize EOL and tabs if git clone is badly configured
278 private static String sanitizeString(String s) {
279 return s.replaceAll("\\r\\n|\\r|\\s+", "");