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("rule_type.BUG"), anyString())).thenReturn("Bug");
67 when(i18n.message(any(Locale.class), eq("rule_type.CODE_SMELL"), anyString())).thenReturn("Code Smell");
68 when(i18n.message(any(Locale.class), eq("rule_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.getMessage())
189 .contains("1 new issue (new debt: 1d3h)\n");
193 public void format_supports_null_version() {
194 Notification notification = newNotification(32)
195 .setFieldValue("branch", "feature1");
197 EmailMessage message = underTest.format(notification);
199 // TODO datetime to be completed when test is isolated from JVM timezone
200 assertThat(message.getMessage())
201 .startsWith("Project: Struts (feature1)\n" +
203 "32 new issues (new debt: 1d3h)\n" +
206 " Bug: 1 Vulnerability: 3 Code Smell: 0\n" +
208 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&assignees=lo.gin&branch=feature1&createdAt=2010-05-18");
212 public void do_not_add_footer_when_properties_missing() {
213 Notification notification = new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
214 .setFieldValue(RULE_TYPE + ".count", "32")
215 .setFieldValue("projectName", "Struts");
217 EmailMessage message = underTest.format(notification);
218 assertThat(message.getMessage()).doesNotContain("See it");
221 private Notification newNotification(int count) {
222 return new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
223 .setFieldValue("projectName", "Struts")
224 .setFieldValue("projectKey", "org.apache:struts")
225 .setFieldValue("projectUuid", "ABCDE")
226 .setFieldValue("projectDate", "2010-05-18T14:50:45+0000")
227 .setFieldValue("assignee", "lo.gin")
228 .setFieldValue(EFFORT + ".count", "1d3h")
229 .setFieldValue(RULE_TYPE + ".count", String.valueOf(count))
230 .setFieldValue(RULE_TYPE + ".BUG.count", "1")
231 .setFieldValue(RULE_TYPE + ".VULNERABILITY.count", "3")
232 .setFieldValue(RULE_TYPE + ".CODE_SMELL.count", "0");
235 private void addTags(Notification notification) {
237 .setFieldValue(TAG + ".1.label", "oscar")
238 .setFieldValue(TAG + ".1.count", "3")
239 .setFieldValue(TAG + ".2.label", "cesar")
240 .setFieldValue(TAG + ".2.count", "10");
243 private void addComponents(Notification notification) {
245 .setFieldValue(COMPONENT + ".1.label", "/path/to/file")
246 .setFieldValue(COMPONENT + ".1.count", "3")
247 .setFieldValue(COMPONENT + ".2.label", "/path/to/directory")
248 .setFieldValue(COMPONENT + ".2.count", "7");
251 private void addRules(Notification notification) {
253 .setFieldValue(RULE + ".1.label", "Rule the Universe (Clojure)")
254 .setFieldValue(RULE + ".1.count", "42")
255 .setFieldValue(RULE + ".2.label", "Rule the World (Java)")
256 .setFieldValue(RULE + ".2.count", "5");
259 private void assertStartsWithFile(String message, String resourcePath) throws IOException {
260 String fileContent = IOUtils.toString(getClass().getResource(resourcePath), StandardCharsets.UTF_8);
261 assertThat(sanitizeString(message)).startsWith(sanitizeString(fileContent));
265 * sanitize EOL and tabs if git clone is badly configured
267 private static String sanitizeString(String s) {
268 return s.replaceAll("\\r\\n|\\r|\\s+", "");