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