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();
82 EmailMessage message = template.format(notification);
84 assertThat(message.getMessageId()).isEqualTo("new-issues/org.apache:struts");
88 public void subject() {
89 Notification notification = newNotification();
91 EmailMessage message = template.format(notification);
93 assertThat(message.getSubject()).isEqualTo("Struts: 32 new issues (new debt: 1d3h)");
97 public void format_email_with_all_fields_filled() throws Exception {
98 Notification notification = newNotification();
99 addAssignees(notification);
100 addRules(notification);
101 addTags(notification);
102 addComponents(notification);
104 EmailMessage message = template.format(notification);
106 // TODO datetime to be completed when test is isolated from JVM timezone
107 assertThat(message.getMessage())
108 .startsWith("Project: Struts\n" +
110 "32 new issues (new debt: 1d3h)\n" +
113 " Bug: 1 Vulnerability: 10 Code Smell: 3\n" +
116 " robin.williams: 5\n" +
120 " Rule the Universe (Clojure): 42\n" +
121 " Rule the World (Java): 5\n" +
127 " Most impacted files\n" +
128 " /path/to/file: 3\n" +
129 " /path/to/directory: 7\n" +
131 "See it in SonarQube: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&createdAt=2010-05-1");
135 public void format_email_with_no_assignees_tags_nor_components() throws Exception {
136 Notification notification = newNotification();
138 EmailMessage message = template.format(notification);
140 // TODO datetime to be completed when test is isolated from JVM timezone
141 assertThat(message.getMessage())
142 .startsWith("Project: Struts\n" +
144 "32 new issues (new debt: 1d3h)\n" +
147 " Bug: 1 Vulnerability: 10 Code Smell: 3\n" +
149 "See it in SonarQube: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&createdAt=2010-05-1");
153 public void format_email_with_issue_on_branch() throws Exception {
154 Notification notification = newNotification()
155 .setFieldValue("branch", "feature1");
157 EmailMessage message = template.format(notification);
159 // TODO datetime to be completed when test is isolated from JVM timezone
160 assertThat(message.getMessage())
161 .startsWith("Project: Struts\n" +
163 "32 new issues (new debt: 1d3h)\n" +
166 " Bug: 1 Vulnerability: 10 Code Smell: 3\n" +
168 "See it in SonarQube: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&branch=feature1&createdAt=2010-05-1");
172 public void do_not_add_footer_when_properties_missing() {
173 Notification notification = new Notification(NewIssuesNotification.TYPE)
174 .setFieldValue(RULE_TYPE + ".count", "32")
175 .setFieldValue("projectName", "Struts");
177 EmailMessage message = template.format(notification);
179 assertThat(message.getMessage()).doesNotContain("See it");
182 private Notification newNotification() {
183 return new Notification(NewIssuesNotification.TYPE)
184 .setFieldValue("projectName", "Struts")
185 .setFieldValue("projectKey", "org.apache:struts")
186 .setFieldValue("projectUuid", "ABCDE")
187 .setFieldValue("projectDate", "2010-05-18T14:50:45+0000")
188 .setFieldValue(EFFORT + ".count", "1d3h")
189 .setFieldValue(RULE_TYPE + ".count", "32")
190 .setFieldValue(RULE_TYPE + ".BUG.count", "1")
191 .setFieldValue(RULE_TYPE + ".CODE_SMELL.count", "3")
192 .setFieldValue(RULE_TYPE + ".VULNERABILITY.count", "10");
195 private void addAssignees(Notification notification) {
197 .setFieldValue(ASSIGNEE + ".1.label", "robin.williams")
198 .setFieldValue(ASSIGNEE + ".1.count", "5")
199 .setFieldValue(ASSIGNEE + ".2.label", "al.pacino")
200 .setFieldValue(ASSIGNEE + ".2.count", "7");
203 private void addTags(Notification notification) {
205 .setFieldValue(TAG + ".1.label", "oscar")
206 .setFieldValue(TAG + ".1.count", "3")
207 .setFieldValue(TAG + ".2.label", "cesar")
208 .setFieldValue(TAG + ".2.count", "10");
211 private void addComponents(Notification notification) {
213 .setFieldValue(COMPONENT + ".1.label", "/path/to/file")
214 .setFieldValue(COMPONENT + ".1.count", "3")
215 .setFieldValue(COMPONENT + ".2.label", "/path/to/directory")
216 .setFieldValue(COMPONENT + ".2.count", "7");
219 private void addRules(Notification notification) {
221 .setFieldValue(RULE + ".1.label", "Rule the Universe (Clojure)")
222 .setFieldValue(RULE + ".1.count", "42")
223 .setFieldValue(RULE + ".2.label", "Rule the World (Java)")
224 .setFieldValue(RULE + ".2.count", "5");
227 private void assertStartsWithFile(String message, String resourcePath) throws IOException {
228 String fileContent = IOUtils.toString(getClass().getResource(resourcePath), StandardCharsets.UTF_8);
229 assertThat(sanitizeString(message)).startsWith(sanitizeString(fileContent));
233 * sanitize EOL and tabs if git clone is badly configured
235 private static String sanitizeString(String s) {
236 return s.replaceAll("\\r\\n|\\r|\\s+", "");