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 com.google.common.io.Resources;
23 import java.nio.charset.StandardCharsets;
24 import org.apache.commons.lang.StringUtils;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.config.EmailSettings;
28 import org.sonar.api.config.internal.MapSettings;
29 import org.sonar.api.notifications.Notification;
30 import org.sonar.db.DbTester;
31 import org.sonar.plugins.emailnotifications.api.EmailMessage;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.sonar.api.CoreProperties.SERVER_BASE_URL;
35 import static org.sonar.db.user.UserTesting.newUserDto;
37 public class IssueChangesEmailTemplateTest {
40 public DbTester db = DbTester.create();
42 private MapSettings settings = new MapSettings().setProperty(SERVER_BASE_URL, "http://nemo.sonarsource.org");
44 private IssueChangesEmailTemplate underTest = new IssueChangesEmailTemplate(db.getDbClient(), new EmailSettings(settings.asConfig()));
47 public void should_ignore_non_issue_changes() {
48 Notification notification = new Notification("other");
49 EmailMessage message = underTest.format(notification);
50 assertThat(message).isNull();
54 public void email_should_display_assignee_change() throws Exception {
55 Notification notification = generateNotification()
56 .setFieldValue("old.assignee", "simon")
57 .setFieldValue("new.assignee", "louis");
59 EmailMessage email = underTest.format(notification);
60 assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
61 assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
63 String message = email.getMessage();
64 String expected = Resources.toString(Resources.getResource(
65 "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_assignee_change.txt"),
66 StandardCharsets.UTF_8);
67 expected = StringUtils.remove(expected, '\r');
68 assertThat(message).isEqualTo(expected);
69 assertThat(email.getFrom()).isNull();
73 public void email_should_display_plan_change() throws Exception {
74 Notification notification = generateNotification()
75 .setFieldValue("old.actionPlan", null)
76 .setFieldValue("new.actionPlan", "ABC 1.0");
78 EmailMessage email = underTest.format(notification);
79 assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
80 assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
82 String message = email.getMessage();
83 String expected = Resources.toString(Resources.getResource(
84 "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_action_plan_change.txt"),
85 StandardCharsets.UTF_8);
86 expected = StringUtils.remove(expected, '\r');
87 assertThat(message).isEqualTo(expected);
88 assertThat(email.getFrom()).isNull();
92 public void email_should_display_resolution_change() throws Exception {
93 Notification notification = generateNotification()
94 .setFieldValue("old.resolution", "FALSE-POSITIVE")
95 .setFieldValue("new.resolution", "FIXED");
97 EmailMessage email = underTest.format(notification);
98 assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
99 assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
101 String message = email.getMessage();
102 String expected = Resources.toString(Resources.getResource(
103 "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_should_display_resolution_change.txt"),
104 StandardCharsets.UTF_8);
105 expected = StringUtils.remove(expected, '\r');
106 assertThat(message).isEqualTo(expected);
107 assertThat(email.getFrom()).isNull();
111 public void display_component_key_if_no_component_name() throws Exception {
112 Notification notification = generateNotification()
113 .setFieldValue("componentName", null);
115 EmailMessage email = underTest.format(notification);
116 assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
117 assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
119 String message = email.getMessage();
120 String expected = Resources.toString(Resources.getResource(
121 "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/display_component_key_if_no_component_name.txt"),
122 StandardCharsets.UTF_8);
123 expected = StringUtils.remove(expected, '\r');
124 assertThat(message).isEqualTo(expected);
128 public void test_email_with_multiple_changes() throws Exception {
129 Notification notification = generateNotification()
130 .setFieldValue("comment", "How to fix it?")
131 .setFieldValue("old.assignee", "simon")
132 .setFieldValue("new.assignee", "louis")
133 .setFieldValue("new.resolution", "FALSE-POSITIVE")
134 .setFieldValue("new.status", "RESOLVED")
135 .setFieldValue("new.type", "BUG")
136 .setFieldValue("new.tags", "bug performance");
138 EmailMessage email = underTest.format(notification);
139 assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
140 assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
142 String message = email.getMessage();
143 String expected = Resources.toString(Resources.getResource(
144 "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_multiple_changes.txt"), StandardCharsets.UTF_8);
145 expected = StringUtils.remove(expected, '\r');
146 assertThat(message).isEqualTo(expected);
147 assertThat(email.getFrom()).isNull();
151 public void test_email_with_issue_on_branch() throws Exception {
152 Notification notification = generateNotification()
153 .setFieldValue("branch", "feature1");
155 EmailMessage email = underTest.format(notification);
156 assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
157 assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
159 String message = email.getMessage();
160 String expected = Resources.toString(Resources.getResource(
161 "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_issue_on_branch.txt"),
162 StandardCharsets.UTF_8);
163 expected = StringUtils.remove(expected, '\r');
164 assertThat(message).isEqualTo(expected);
168 public void notification_sender_should_be_the_author_of_change() {
169 db.users().insertUser(newUserDto().setLogin("simon").setName("Simon"));
171 Notification notification = new IssueChangeNotification()
172 .setChangeAuthorLogin("simon")
173 .setProject("Struts", "org.apache:struts", null, "");
175 EmailMessage message = underTest.format(notification);
176 assertThat(message.getFrom()).isEqualTo("Simon");
180 public void notification_contains_user_login_when_user_is_removed() {
181 db.users().insertUser(newUserDto().setLogin("simon").setName("Simon").setActive(false));
183 Notification notification = new IssueChangeNotification()
184 .setChangeAuthorLogin("simon")
185 .setProject("Struts", "org.apache:struts", null, "");
187 EmailMessage message = underTest.format(notification);
188 assertThat(message.getFrom()).isEqualTo("simon");
191 private static Notification generateNotification() {
192 return new IssueChangeNotification()
193 .setFieldValue("projectName", "Struts")
194 .setFieldValue("projectKey", "org.apache:struts")
195 .setFieldValue("componentName", "Action")
196 .setFieldValue("componentKey", "org.apache.struts.Action")
197 .setFieldValue("key", "ABCDE")
198 .setFieldValue("ruleName", "Avoid Cycles")
199 .setFieldValue("message", "Has 3 cycles");