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 "See it in SonarQube: 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);
145 EmailMessage message = underTest.format(notification);
147 // TODO datetime to be completed when test is isolated from JVM timezone
148 assertThat(message.getMessage())
149 .startsWith("Project: Struts\n" +
151 "32 new issues (new debt: 1d3h)\n" +
154 " Bug: 1 Vulnerability: 3 Code Smell: 0\n" +
156 "See it in SonarQube: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&assignees=lo.gin&createdAt=2010-05-18");
160 public void format_email_with_issue_on_branch() throws Exception {
161 Notification notification = newNotification(32)
162 .setFieldValue("branch", "feature1");
164 EmailMessage message = underTest.format(notification);
166 // TODO datetime to be completed when test is isolated from JVM timezone
167 assertThat(message.getMessage())
168 .startsWith("Project: Struts (feature1)\n" +
170 "32 new issues (new debt: 1d3h)\n" +
173 " Bug: 1 Vulnerability: 3 Code Smell: 0\n" +
175 "See it in SonarQube: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&assignees=lo.gin&branch=feature1&createdAt=2010-05-18");
179 public void format_email_supports_single_issue() {
180 Notification notification = newNotification(1);
182 EmailMessage message = underTest.format(notification);
184 assertThat(message.getMessage())
185 .contains("1 new issue (new debt: 1d3h)\n");
189 public void do_not_add_footer_when_properties_missing() {
190 Notification notification = new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
191 .setFieldValue(RULE_TYPE + ".count", "32")
192 .setFieldValue("projectName", "Struts");
194 EmailMessage message = underTest.format(notification);
195 assertThat(message.getMessage()).doesNotContain("See it");
198 private Notification newNotification(int count) {
199 return new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
200 .setFieldValue("projectName", "Struts")
201 .setFieldValue("projectKey", "org.apache:struts")
202 .setFieldValue("projectUuid", "ABCDE")
203 .setFieldValue("projectDate", "2010-05-18T14:50:45+0000")
204 .setFieldValue("assignee", "lo.gin")
205 .setFieldValue(EFFORT + ".count", "1d3h")
206 .setFieldValue(RULE_TYPE + ".count", String.valueOf(count))
207 .setFieldValue(RULE_TYPE + ".BUG.count", "1")
208 .setFieldValue(RULE_TYPE + ".VULNERABILITY.count", "3")
209 .setFieldValue(RULE_TYPE + ".CODE_SMELL.count", "0");
212 private void addTags(Notification notification) {
214 .setFieldValue(TAG + ".1.label", "oscar")
215 .setFieldValue(TAG + ".1.count", "3")
216 .setFieldValue(TAG + ".2.label", "cesar")
217 .setFieldValue(TAG + ".2.count", "10");
220 private void addComponents(Notification notification) {
222 .setFieldValue(COMPONENT + ".1.label", "/path/to/file")
223 .setFieldValue(COMPONENT + ".1.count", "3")
224 .setFieldValue(COMPONENT + ".2.label", "/path/to/directory")
225 .setFieldValue(COMPONENT + ".2.count", "7");
228 private void addRules(Notification notification) {
230 .setFieldValue(RULE + ".1.label", "Rule the Universe (Clojure)")
231 .setFieldValue(RULE + ".1.count", "42")
232 .setFieldValue(RULE + ".2.label", "Rule the World (Java)")
233 .setFieldValue(RULE + ".2.count", "5");
236 private void assertStartsWithFile(String message, String resourcePath) throws IOException {
237 String fileContent = IOUtils.toString(getClass().getResource(resourcePath), StandardCharsets.UTF_8);
238 assertThat(sanitizeString(message)).startsWith(sanitizeString(fileContent));
242 * sanitize EOL and tabs if git clone is badly configured
244 private static String sanitizeString(String s) {
245 return s.replaceAll("\\r\\n|\\r|\\s+", "");