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.MapSettings;
29 import org.sonar.api.config.Settings;
30 import org.sonar.api.notifications.Notification;
31 import org.sonar.db.DbTester;
32 import org.sonar.plugins.emailnotifications.api.EmailMessage;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.sonar.api.CoreProperties.SERVER_BASE_URL;
36 import static org.sonar.db.user.UserTesting.newUserDto;
38 public class IssueChangesEmailTemplateTest {
41 public DbTester db = DbTester.create();
43 private Settings settings = new MapSettings().setProperty(SERVER_BASE_URL, "http://nemo.sonarsource.org");
45 private IssueChangesEmailTemplate underTest = new IssueChangesEmailTemplate(db.getDbClient(), new EmailSettings(settings));
48 public void should_ignore_non_issue_changes() {
49 Notification notification = new Notification("other");
50 EmailMessage message = underTest.format(notification);
51 assertThat(message).isNull();
55 public void email_should_display_assignee_change() throws Exception {
56 Notification notification = generateNotification()
57 .setFieldValue("old.assignee", "simon")
58 .setFieldValue("new.assignee", "louis");
60 EmailMessage email = underTest.format(notification);
61 assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
62 assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
64 String message = email.getMessage();
65 String expected = Resources.toString(Resources.getResource(
66 "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_assignee_change.txt"),
67 StandardCharsets.UTF_8);
68 expected = StringUtils.remove(expected, '\r');
69 assertThat(message).isEqualTo(expected);
70 assertThat(email.getFrom()).isNull();
74 public void email_should_display_plan_change() throws Exception {
75 Notification notification = generateNotification()
76 .setFieldValue("old.actionPlan", null)
77 .setFieldValue("new.actionPlan", "ABC 1.0");
79 EmailMessage email = underTest.format(notification);
80 assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
81 assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
83 String message = email.getMessage();
84 String expected = Resources.toString(Resources.getResource(
85 "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_action_plan_change.txt"),
86 StandardCharsets.UTF_8);
87 expected = StringUtils.remove(expected, '\r');
88 assertThat(message).isEqualTo(expected);
89 assertThat(email.getFrom()).isNull();
93 public void email_should_display_resolution_change() throws Exception {
94 Notification notification = generateNotification()
95 .setFieldValue("old.resolution", "FALSE-POSITIVE")
96 .setFieldValue("new.resolution", "FIXED");
98 EmailMessage email = underTest.format(notification);
99 assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
100 assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
102 String message = email.getMessage();
103 String expected = Resources.toString(Resources.getResource(
104 "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_should_display_resolution_change.txt"),
105 StandardCharsets.UTF_8);
106 expected = StringUtils.remove(expected, '\r');
107 assertThat(message).isEqualTo(expected);
108 assertThat(email.getFrom()).isNull();
112 public void display_component_key_if_no_component_name() throws Exception {
113 Notification notification = generateNotification()
114 .setFieldValue("componentName", null);
116 EmailMessage email = underTest.format(notification);
117 assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
118 assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
120 String message = email.getMessage();
121 String expected = Resources.toString(Resources.getResource(
122 "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/display_component_key_if_no_component_name.txt"),
123 StandardCharsets.UTF_8);
124 expected = StringUtils.remove(expected, '\r');
125 assertThat(message).isEqualTo(expected);
129 public void test_email_with_multiple_changes() throws Exception {
130 Notification notification = generateNotification()
131 .setFieldValue("comment", "How to fix it?")
132 .setFieldValue("old.assignee", "simon")
133 .setFieldValue("new.assignee", "louis")
134 .setFieldValue("new.resolution", "FALSE-POSITIVE")
135 .setFieldValue("new.status", "RESOLVED")
136 .setFieldValue("new.type", "BUG")
137 .setFieldValue("new.tags", "bug performance");
139 EmailMessage email = underTest.format(notification);
140 assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
141 assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
143 String message = email.getMessage();
144 String expected = Resources.toString(Resources.getResource(
145 "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_with_multiple_changes.txt"), StandardCharsets.UTF_8);
146 expected = StringUtils.remove(expected, '\r');
147 assertThat(message).isEqualTo(expected);
148 assertThat(email.getFrom()).isNull();
152 public void notification_sender_should_be_the_author_of_change() {
153 db.users().insertUser(newUserDto().setLogin("simon").setName("Simon"));
155 Notification notification = new IssueChangeNotification()
156 .setChangeAuthorLogin("simon")
157 .setProject("Struts", "org.apache:struts");
159 EmailMessage message = underTest.format(notification);
160 assertThat(message.getFrom()).isEqualTo("Simon");
164 public void notification_contains_user_login_when_user_is_removed() {
165 db.users().insertUser(newUserDto().setLogin("simon").setName("Simon").setActive(false));
167 Notification notification = new IssueChangeNotification()
168 .setChangeAuthorLogin("simon")
169 .setProject("Struts", "org.apache:struts");
171 EmailMessage message = underTest.format(notification);
172 assertThat(message.getFrom()).isEqualTo("simon");
175 private static Notification generateNotification() {
176 return new IssueChangeNotification()
177 .setFieldValue("projectName", "Struts")
178 .setFieldValue("projectKey", "org.apache:struts")
179 .setFieldValue("componentName", "Action")
180 .setFieldValue("componentKey", "org.apache.struts.Action")
181 .setFieldValue("key", "ABCDE")
182 .setFieldValue("ruleName", "Avoid Cycles")
183 .setFieldValue("message", "Has 3 cycles");