]> source.dussan.org Git - sonarqube.git/blob
02e00bacb0a03236cc645a2188aacbe7cdc73ed2
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.plugins.core.issue.notification;
21
22 import com.google.common.io.Resources;
23 import org.apache.commons.codec.Charsets;
24 import org.apache.commons.lang.StringUtils;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.runners.MockitoJUnitRunner;
30 import org.sonar.api.config.EmailSettings;
31 import org.sonar.api.notifications.Notification;
32 import org.sonar.api.user.User;
33 import org.sonar.api.user.UserFinder;
34 import org.sonar.plugins.emailnotifications.api.EmailMessage;
35
36 import static org.fest.assertions.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
39
40 @RunWith(MockitoJUnitRunner.class)
41 public class IssueChangesEmailTemplateTest {
42
43   @Mock
44   UserFinder userFinder;
45
46   IssueChangesEmailTemplate template;
47
48   @Before
49   public void setUp() {
50     EmailSettings settings = mock(EmailSettings.class);
51     when(settings.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
52     template = new IssueChangesEmailTemplate(settings, userFinder);
53   }
54
55   @Test
56   public void should_ignore_non_issue_changes() {
57     Notification notification = new Notification("other");
58     EmailMessage message = template.format(notification);
59     assertThat(message).isNull();
60   }
61
62   @Test
63   public void email_should_display_assignee_change() throws Exception {
64     Notification notification = generateNotification()
65       .setFieldValue("old.assignee", "simon")
66       .setFieldValue("new.assignee", "louis");
67
68     EmailMessage email = template.format(notification);
69     assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
70     assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
71
72     String message = email.getMessage();
73     String expected = Resources.toString(Resources.getResource(
74         "org/sonar/plugins/core/issue/notification/IssueChangesEmailTemplateTest/email_with_assignee_change.txt"),
75       Charsets.UTF_8
76     );
77     expected = StringUtils.remove(expected, '\r');
78     assertThat(message).isEqualTo(expected);
79     assertThat(email.getFrom()).isNull();
80   }
81
82   @Test
83   public void email_should_display_plan_change() throws Exception {
84     Notification notification = generateNotification()
85       .setFieldValue("old.actionPlan", null)
86       .setFieldValue("new.actionPlan", "ABC 1.0");
87
88     EmailMessage email = template.format(notification);
89     assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
90     assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
91
92     String message = email.getMessage();
93     String expected = Resources.toString(Resources.getResource(
94         "org/sonar/plugins/core/issue/notification/IssueChangesEmailTemplateTest/email_with_action_plan_change.txt"),
95       Charsets.UTF_8
96     );
97     expected = StringUtils.remove(expected, '\r');
98     assertThat(message).isEqualTo(expected);
99     assertThat(email.getFrom()).isNull();
100   }
101
102   @Test
103   public void test_email_with_multiple_changes() throws Exception {
104     Notification notification = generateNotification()
105       .setFieldValue("comment", "How to fix it?")
106       .setFieldValue("old.assignee", "simon")
107       .setFieldValue("new.assignee", "louis")
108       .setFieldValue("new.resolution", "FALSE-POSITIVE")
109       .setFieldValue("new.status", "RESOLVED");
110
111     EmailMessage email = template.format(notification);
112     assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
113     assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
114
115     String message = email.getMessage();
116     String expected = Resources.toString(Resources.getResource(
117       "org/sonar/plugins/core/issue/notification/IssueChangesEmailTemplateTest/email_with_multiple_changes.txt"), Charsets.UTF_8);
118     expected = StringUtils.remove(expected, '\r');
119     assertThat(message).isEqualTo(expected);
120     assertThat(email.getFrom()).isNull();
121   }
122
123   @Test
124   public void notification_sender_should_be_the_author_of_change() {
125     User user = mock(User.class);
126     when(user.name()).thenReturn("Simon");
127     when(userFinder.findByLogin("simon")).thenReturn(user);
128
129     Notification notification = new Notification("issue-changes")
130       .setFieldValue("projectName", "Struts")
131       .setFieldValue("projectKey", "org.apache:struts")
132       .setFieldValue("changeAuthor", "simon");
133
134     EmailMessage message = template.format(notification);
135     assertThat(message.getFrom()).isEqualTo("Simon");
136   }
137
138   private Notification generateNotification() {
139     Notification notification = new Notification("issue-changes")
140       .setFieldValue("projectName", "Struts")
141       .setFieldValue("projectKey", "org.apache:struts")
142       .setFieldValue("componentName", "org.apache.struts.Action")
143       .setFieldValue("key", "ABCDE")
144       .setFieldValue("ruleName", "Avoid Cycles")
145       .setFieldValue("message", "Has 3 cycles");
146     return notification;
147   }
148 }