3 * Copyright (C) 2009-2019 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 org.apache.commons.io.IOUtils;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.config.EmailSettings;
29 import org.sonar.api.impl.config.MapSettings;
30 import org.sonar.api.notifications.Notification;
31 import org.sonar.api.platform.Server;
32 import org.sonar.server.l18n.I18nRule;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
37 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.COMPONENT;
38 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.EFFORT;
39 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.RULE;
40 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.RULE_TYPE;
41 import static org.sonar.server.issue.notification.NewIssuesStatistics.Metric.TAG;
43 public class MyNewIssuesEmailTemplateTest {
46 public I18nRule i18n = new I18nRule()
47 .put("issue.type.BUG", "Bug")
48 .put("issue.type.CODE_SMELL", "Code Smell")
49 .put("issue.type.VULNERABILITY", "Vulnerability");
50 private MapSettings settings = new MapSettings();
52 private Server server = mock(Server.class);
53 private MyNewIssuesEmailTemplate underTest = new MyNewIssuesEmailTemplate(new EmailSettings(settings.asConfig(), server), i18n);
56 public void setUp() throws Exception {
57 when(server.getPublicRootUrl()).thenReturn("http://nemo.sonarsource.org");
61 public void no_format_if_not_the_correct_notif() {
62 Notification notification = new Notification("new-issues");
63 EmailMessage message = underTest.format(notification);
64 assertThat(message).isNull();
68 public void format_email_with_all_fields_filled() {
69 Notification notification = newNotification(32);
70 addTags(notification);
71 addRules(notification);
72 addComponents(notification);
74 EmailMessage message = underTest.format(notification);
76 // TODO datetime to be completed when test is isolated from JVM timezone
77 assertThat(message.getMessage()).startsWith(
80 "32 new issues (new debt: 1d3h)\n" +
83 " Bug: 1 Vulnerability: 3 Code Smell: 0\n" +
86 " Rule the Universe (Clojure): 42\n" +
87 " Rule the World (Java): 5\n" +
93 " Most impacted files\n" +
94 " /path/to/file: 3\n" +
95 " /path/to/directory: 7\n" +
97 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&assignees=lo.gin&createdAt=2010-05-18");
101 public void message_id() {
102 Notification notification = newNotification(32);
104 EmailMessage message = underTest.format(notification);
106 assertThat(message.getMessageId()).isEqualTo("my-new-issues/org.apache:struts");
110 public void subject() {
111 Notification notification = newNotification(32);
113 EmailMessage message = underTest.format(notification);
115 assertThat(message.getSubject()).isEqualTo("You have 32 new issues on project Struts");
119 public void subject_on_branch() {
120 Notification notification = newNotification(32)
121 .setFieldValue("branch", "feature1");
123 EmailMessage message = underTest.format(notification);
125 assertThat(message.getSubject()).isEqualTo("You have 32 new issues on project Struts (feature1)");
129 public void format_email_with_no_assignees_tags_nor_components() {
130 Notification notification = newNotification(32)
131 .setFieldValue("projectVersion", "52.0");
133 EmailMessage message = underTest.format(notification);
135 // TODO datetime to be completed when test is isolated from JVM timezone
136 assertThat(message.getMessage())
137 .startsWith("Project: Struts\n" +
140 "32 new issues (new debt: 1d3h)\n" +
143 " Bug: 1 Vulnerability: 3 Code Smell: 0\n" +
145 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&assignees=lo.gin&createdAt=2010-05-18");
149 public void format_email_with_issue_on_branch() {
150 Notification notification = newNotification(32)
151 .setFieldValue("projectVersion", "52.0")
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" +
159 "Branch: feature1\n" +
162 "32 new issues (new debt: 1d3h)\n" +
165 " Bug: 1 Vulnerability: 3 Code Smell: 0\n" +
167 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&assignees=lo.gin&branch=feature1&createdAt=2010-05-18");
171 public void format_email_supports_single_issue() {
172 Notification notification = newNotification(1);
174 EmailMessage message = underTest.format(notification);
176 assertThat(message.getSubject())
177 .isEqualTo("You have 1 new issue on project Struts");
178 assertThat(message.getMessage())
179 .contains("1 new issue (new debt: 1d3h)\n");
183 public void format_supports_null_version() {
184 Notification notification = newNotification(32)
185 .setFieldValue("branch", "feature1");
187 EmailMessage message = underTest.format(notification);
189 // TODO datetime to be completed when test is isolated from JVM timezone
190 assertThat(message.getMessage())
191 .startsWith("Project: Struts\n" +
192 "Branch: feature1\n" +
194 "32 new issues (new debt: 1d3h)\n" +
197 " Bug: 1 Vulnerability: 3 Code Smell: 0\n" +
199 "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&assignees=lo.gin&branch=feature1&createdAt=2010-05-18");
203 public void do_not_add_footer_when_properties_missing() {
204 Notification notification = new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
205 .setFieldValue(RULE_TYPE + ".count", "32")
206 .setFieldValue("projectName", "Struts");
208 EmailMessage message = underTest.format(notification);
209 assertThat(message.getMessage()).doesNotContain("See it");
212 private Notification newNotification(int count) {
213 return new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
214 .setFieldValue("projectName", "Struts")
215 .setFieldValue("projectKey", "org.apache:struts")
216 .setFieldValue("projectDate", "2010-05-18T14:50:45+0000")
217 .setFieldValue("assignee", "lo.gin")
218 .setFieldValue(EFFORT + ".count", "1d3h")
219 .setFieldValue(RULE_TYPE + ".count", String.valueOf(count))
220 .setFieldValue(RULE_TYPE + ".BUG.count", "1")
221 .setFieldValue(RULE_TYPE + ".VULNERABILITY.count", "3")
222 .setFieldValue(RULE_TYPE + ".CODE_SMELL.count", "0");
225 private void addTags(Notification notification) {
227 .setFieldValue(TAG + ".1.label", "oscar")
228 .setFieldValue(TAG + ".1.count", "3")
229 .setFieldValue(TAG + ".2.label", "cesar")
230 .setFieldValue(TAG + ".2.count", "10");
233 private void addComponents(Notification notification) {
235 .setFieldValue(COMPONENT + ".1.label", "/path/to/file")
236 .setFieldValue(COMPONENT + ".1.count", "3")
237 .setFieldValue(COMPONENT + ".2.label", "/path/to/directory")
238 .setFieldValue(COMPONENT + ".2.count", "7");
241 private void addRules(Notification notification) {
243 .setFieldValue(RULE + ".1.label", "Rule the Universe (Clojure)")
244 .setFieldValue(RULE + ".1.count", "42")
245 .setFieldValue(RULE + ".2.label", "Rule the World (Java)")
246 .setFieldValue(RULE + ".2.count", "5");
249 private void assertStartsWithFile(String message, String resourcePath) throws IOException {
250 String fileContent = IOUtils.toString(getClass().getResource(resourcePath), StandardCharsets.UTF_8);
251 assertThat(sanitizeString(message)).startsWith(sanitizeString(fileContent));
255 * sanitize EOL and tabs if git clone is badly configured
257 private static String sanitizeString(String s) {
258 return s.replaceAll("\\r\\n|\\r|\\s+", "");