]> source.dussan.org Git - sonarqube.git/blob
7814c072c732d9abafdd573eb418bd542a95f519
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2013 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 org.apache.commons.lang.StringUtils;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.sonar.api.config.EmailSettings;
29 import org.sonar.api.notifications.Notification;
30 import org.sonar.api.user.User;
31 import org.sonar.api.user.UserFinder;
32 import org.sonar.plugins.emailnotifications.api.EmailMessage;
33 import org.sonar.test.TestUtils;
34
35 import static org.fest.assertions.Assertions.assertThat;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class IssueChangesEmailTemplateTest {
41
42   @Mock
43   UserFinder userFinder;
44
45   IssueChangesEmailTemplate template;
46
47   @Before
48   public void setUp() {
49     EmailSettings settings = mock(EmailSettings.class);
50     when(settings.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
51     template = new IssueChangesEmailTemplate(settings, userFinder);
52   }
53
54   @Test
55   public void should_ignore_non_issue_changes() {
56     Notification notification = new Notification("other");
57     EmailMessage message = template.format(notification);
58     assertThat(message).isNull();
59   }
60
61   @Test
62   public void test_email_with_changes() {
63     Notification notification = new Notification("issue-changes")
64       .setFieldValue("projectName", "Struts")
65       .setFieldValue("projectKey", "org.apache:struts")
66       .setFieldValue("componentName", "org.apache.struts.Action")
67       .setFieldValue("key", "ABCDE")
68       .setFieldValue("ruleName", "Avoid Cycles")
69       .setFieldValue("message", "Has 3 cycles")
70       .setFieldValue("comment", "How to fix it?")
71       .setFieldValue("old.assignee", "simon")
72       .setFieldValue("new.assignee", "louis")
73       .setFieldValue("new.resolution", "FALSE-POSITIVE")
74       .setFieldValue("new.status", "RESOLVED");
75
76     EmailMessage email = template.format(notification);
77     assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
78     assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
79
80     String message = email.getMessage();
81     String expectedMessage = TestUtils.getResourceContent("/org/sonar/plugins/core/issue/notification/IssueChangesEmailTemplateTest/email_with_changes.txt");
82     expectedMessage = StringUtils.remove(expectedMessage, '\r');
83     assertThat(message).isEqualTo(expectedMessage);
84     assertThat(email.getFrom()).isNull();
85   }
86
87   @Test
88   public void test_email_with_issue_message_same_than_rule_name() {
89     Notification notification = new Notification("issue-changes")
90       .setFieldValue("projectName", "Struts")
91       .setFieldValue("projectKey", "org.apache:struts")
92       .setFieldValue("componentName", "org.apache.struts.Action")
93       .setFieldValue("key", "ABCDE")
94       .setFieldValue("new.assignee", "louis")
95
96       // same rule name and issue msg -> display once
97       .setFieldValue("ruleName", "Avoid Cycles")
98       .setFieldValue("message", "Avoid Cycles");
99
100     EmailMessage email = template.format(notification);
101     String message = email.getMessage();
102     String expectedMessage = TestUtils.getResourceContent("/org/sonar/plugins/core/issue/notification/IssueChangesEmailTemplateTest/email_with_issue_message_same_than_rule_name.txt");
103     expectedMessage = StringUtils.remove(expectedMessage, '\r');
104     assertThat(message).isEqualTo(expectedMessage);
105   }
106
107   @Test
108   public void notification_sender_should_be_the_author_of_change() {
109     User user = mock(User.class);
110     when(user.name()).thenReturn("Simon");
111     when(userFinder.findByLogin("simon")).thenReturn(user);
112
113     Notification notification = new Notification("issue-changes")
114       .setFieldValue("projectName", "Struts")
115       .setFieldValue("projectKey", "org.apache:struts")
116       .setFieldValue("changeAuthor", "simon");
117
118     EmailMessage message = template.format(notification);
119     assertThat(message.getFrom()).isEqualTo("Simon");
120   }
121
122 }