2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.plugins.core.issue.notification;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.sonar.api.config.EmailSettings;
28 import org.sonar.api.notifications.Notification;
29 import org.sonar.api.user.User;
30 import org.sonar.api.user.UserFinder;
31 import org.sonar.plugins.emailnotifications.api.EmailMessage;
33 import static org.fest.assertions.Assertions.assertThat;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
37 @RunWith(MockitoJUnitRunner.class)
38 public class IssueChangesEmailTemplateTest {
41 UserFinder userFinder;
43 IssueChangesEmailTemplate template;
47 EmailSettings settings = mock(EmailSettings.class);
48 when(settings.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
49 template = new IssueChangesEmailTemplate(settings, userFinder);
53 public void should_ignore_non_issue_changes() {
54 Notification notification = new Notification("other");
55 EmailMessage message = template.format(notification);
56 assertThat(message).isNull();
60 public void should_format_change() {
61 Notification notification = new Notification("issue-changes")
62 .setFieldValue("projectName", "Struts")
63 .setFieldValue("projectKey", "org.apache:struts")
64 .setFieldValue("key", "ABCDE")
65 .setFieldValue("ruleName", "Avoid Cycles")
66 .setFieldValue("message", "Has 3 cycles")
67 .setFieldValue("comment", "How to fix it?")
68 .setFieldValue("old.assignee", "simon")
69 .setFieldValue("new.assignee", "louis")
70 .setFieldValue("new.resolution", "FALSE-POSITIVE")
71 .setFieldValue("new.status", "RESOLVED");
73 EmailMessage message = template.format(notification);
74 assertThat(message.getMessageId()).isEqualTo("issue-changes/ABCDE");
75 assertThat(message.getSubject()).isEqualTo("Project Struts, change on issue #ABCDE");
76 assertThat(message.getMessage()).contains("Rule: Avoid Cycles");
77 assertThat(message.getMessage()).contains("Message: Has 3 cycles");
78 assertThat(message.getMessage()).contains("Comment: How to fix it?");
79 assertThat(message.getMessage()).contains("Assignee: louis (was simon)");
80 assertThat(message.getMessage()).contains("Resolution: FALSE-POSITIVE");
81 assertThat(message.getMessage()).contains("See it in SonarQube: http://nemo.sonarsource.org/issue/show/ABCDE");
82 assertThat(message.getFrom()).isNull();
86 public void notification_sender_should_be_the_author_of_change() {
87 User user = mock(User.class);
88 when(user.name()).thenReturn("Simon");
89 when(userFinder.findByLogin("simon")).thenReturn(user);
91 Notification notification = new Notification("issue-changes")
92 .setFieldValue("projectName", "Struts")
93 .setFieldValue("projectKey", "org.apache:struts")
94 .setFieldValue("changeAuthor", "simon");
96 EmailMessage message = template.format(notification);
97 assertThat(message.getFrom()).isEqualTo("Simon");