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 addAssignees(notification);
110 addRules(notification);
111 addTags(notification);
112 addComponents(notification);
114 EmailMessage message = template.format(notification);
116 // TODO datetime to be completed when test is isolated from JVM timezone
117 assertThat(message.getMessage())
118 .startsWith("Project: Struts\n" +
120 "32 new issues (new debt: 1d3h)\n" +
123 " Bug: 1 Vulnerability: 10 Code Smell: 3\n" +
126 " robin.williams: 5\n" +
130 " Rule the Universe (Clojure): 42\n" +
131 " Rule the World (Java): 5\n" +
137 " Most impacted files\n" +
138 " /path/to/file: 3\n" +
139 " /path/to/directory: 7\n" +
141 "See it in SonarQube: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&createdAt=2010-05-1");
145 public void format_email_with_no_assignees_tags_nor_components() throws Exception {
146 Notification notification = newNotification(32);
148 EmailMessage message = template.format(notification);
150 // TODO datetime to be completed when test is isolated from JVM timezone
151 assertThat(message.getMessage())
152 .startsWith("Project: Struts\n" +
154 "32 new issues (new debt: 1d3h)\n" +
157 " Bug: 1 Vulnerability: 10 Code Smell: 3\n" +
159 "See it in SonarQube: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&createdAt=2010-05-1");
163 public void format_email_supports_single_issue() {
164 Notification notification = newNotification(1);
166 EmailMessage message = template.format(notification);
168 assertThat(message.getMessage())
169 .contains("1 new issue (new debt: 1d3h)\n");
173 public void format_email_with_issue_on_branch() throws Exception {
174 Notification notification = newNotification(32)
175 .setFieldValue("branch", "feature1");
177 EmailMessage message = template.format(notification);
179 // TODO datetime to be completed when test is isolated from JVM timezone
180 assertThat(message.getMessage())
181 .startsWith("Project: Struts (feature1)\n" +
183 "32 new issues (new debt: 1d3h)\n" +
186 " Bug: 1 Vulnerability: 10 Code Smell: 3\n" +
188 "See it in SonarQube: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&branch=feature1&createdAt=2010-05-1");
192 public void do_not_add_footer_when_properties_missing() {
193 Notification notification = new Notification(NewIssuesNotification.TYPE)
194 .setFieldValue(RULE_TYPE + ".count", "32")
195 .setFieldValue("projectName", "Struts");
197 EmailMessage message = template.format(notification);
199 assertThat(message.getMessage()).doesNotContain("See it");
202 private Notification newNotification(int count) {
203 return new Notification(NewIssuesNotification.TYPE)
204 .setFieldValue("projectName", "Struts")
205 .setFieldValue("projectKey", "org.apache:struts")
206 .setFieldValue("projectUuid", "ABCDE")
207 .setFieldValue("projectDate", "2010-05-18T14:50:45+0000")
208 .setFieldValue(EFFORT + ".count", "1d3h")
209 .setFieldValue(RULE_TYPE + ".count", String.valueOf(count))
210 .setFieldValue(RULE_TYPE + ".BUG.count", "1")
211 .setFieldValue(RULE_TYPE + ".CODE_SMELL.count", "3")
212 .setFieldValue(RULE_TYPE + ".VULNERABILITY.count", "10");
215 private void addAssignees(Notification notification) {
217 .setFieldValue(ASSIGNEE + ".1.label", "robin.williams")
218 .setFieldValue(ASSIGNEE + ".1.count", "5")
219 .setFieldValue(ASSIGNEE + ".2.label", "al.pacino")
220 .setFieldValue(ASSIGNEE + ".2.count", "7");
223 private void addTags(Notification notification) {
225 .setFieldValue(TAG + ".1.label", "oscar")
226 .setFieldValue(TAG + ".1.count", "3")
227 .setFieldValue(TAG + ".2.label", "cesar")
228 .setFieldValue(TAG + ".2.count", "10");
231 private void addComponents(Notification notification) {
233 .setFieldValue(COMPONENT + ".1.label", "/path/to/file")
234 .setFieldValue(COMPONENT + ".1.count", "3")
235 .setFieldValue(COMPONENT + ".2.label", "/path/to/directory")
236 .setFieldValue(COMPONENT + ".2.count", "7");
239 private void addRules(Notification notification) {
241 .setFieldValue(RULE + ".1.label", "Rule the Universe (Clojure)")
242 .setFieldValue(RULE + ".1.count", "42")
243 .setFieldValue(RULE + ".2.label", "Rule the World (Java)")
244 .setFieldValue(RULE + ".2.count", "5");
247 private void assertStartsWithFile(String message, String resourcePath) throws IOException {
248 String fileContent = IOUtils.toString(getClass().getResource(resourcePath), StandardCharsets.UTF_8);
249 assertThat(sanitizeString(message)).startsWith(sanitizeString(fileContent));
253 * sanitize EOL and tabs if git clone is badly configured
255 private static String sanitizeString(String s) {
256 return s.replaceAll("\\r\\n|\\r|\\s+", "");