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();
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();
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();
126 EmailMessage message = underTest.format(notification);
128 assertThat(message.getSubject()).isEqualTo("You have 32 new issues on project Struts");
132 public void format_email_with_no_assignees_tags_nor_components() throws Exception {
133 Notification notification = newNotification();
135 EmailMessage message = underTest.format(notification);
137 // TODO datetime to be completed when test is isolated from JVM timezone
138 assertThat(message.getMessage())
139 .startsWith("Project: Struts\n" +
141 "32 new issues (new debt: 1d3h)\n" +
144 " Bug: 1 Vulnerability: 3 Code Smell: 0\n" +
146 "See it in SonarQube: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&assignees=lo.gin&createdAt=2010-05-18");
150 public void format_email_with_issue_on_branch() throws Exception {
151 Notification notification = newNotification()
152 .setFieldValue("branch", "feature1");
154 EmailMessage message = underTest.format(notification);
156 // TODO datetime to be completed when test is isolated from JVM timezone
157 assertThat(message.getMessage())
158 .startsWith("Project: Struts\n" +
160 "32 new issues (new debt: 1d3h)\n" +
163 " Bug: 1 Vulnerability: 3 Code Smell: 0\n" +
165 "See it in SonarQube: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&assignees=lo.gin&branch=feature1&createdAt=2010-05-18");
169 public void do_not_add_footer_when_properties_missing() {
170 Notification notification = new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
171 .setFieldValue(RULE_TYPE + ".count", "32")
172 .setFieldValue("projectName", "Struts");
174 EmailMessage message = underTest.format(notification);
175 assertThat(message.getMessage()).doesNotContain("See it");
178 private Notification newNotification() {
179 return new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
180 .setFieldValue("projectName", "Struts")
181 .setFieldValue("projectKey", "org.apache:struts")
182 .setFieldValue("projectUuid", "ABCDE")
183 .setFieldValue("projectDate", "2010-05-18T14:50:45+0000")
184 .setFieldValue("assignee", "lo.gin")
185 .setFieldValue(EFFORT + ".count", "1d3h")
186 .setFieldValue(RULE_TYPE + ".count", "32")
187 .setFieldValue(RULE_TYPE + ".BUG.count", "1")
188 .setFieldValue(RULE_TYPE + ".VULNERABILITY.count", "3")
189 .setFieldValue(RULE_TYPE + ".CODE_SMELL.count", "0");
192 private void addTags(Notification notification) {
194 .setFieldValue(TAG + ".1.label", "oscar")
195 .setFieldValue(TAG + ".1.count", "3")
196 .setFieldValue(TAG + ".2.label", "cesar")
197 .setFieldValue(TAG + ".2.count", "10");
200 private void addComponents(Notification notification) {
202 .setFieldValue(COMPONENT + ".1.label", "/path/to/file")
203 .setFieldValue(COMPONENT + ".1.count", "3")
204 .setFieldValue(COMPONENT + ".2.label", "/path/to/directory")
205 .setFieldValue(COMPONENT + ".2.count", "7");
208 private void addRules(Notification notification) {
210 .setFieldValue(RULE + ".1.label", "Rule the Universe (Clojure)")
211 .setFieldValue(RULE + ".1.count", "42")
212 .setFieldValue(RULE + ".2.label", "Rule the World (Java)")
213 .setFieldValue(RULE + ".2.count", "5");
216 private void assertStartsWithFile(String message, String resourcePath) throws IOException {
217 String fileContent = IOUtils.toString(getClass().getResource(resourcePath), StandardCharsets.UTF_8);
218 assertThat(sanitizeString(message)).startsWith(sanitizeString(fileContent));
222 * sanitize EOL and tabs if git clone is badly configured
224 private static String sanitizeString(String s) {
225 return s.replaceAll("\\r\\n|\\r|\\s+", "");