aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core/src/test
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2013-05-27 18:53:06 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2013-05-27 19:06:01 +0200
commit26a782fc420625f2428e172c2f847ed73750ecde (patch)
tree30fff5e3accc5378dca1a3dd1d144bcac2e9db31 /sonar-core/src/test
parenta3a6550215e9a36fd8a04ba544f2a2379af6c0e7 (diff)
downloadsonarqube-26a782fc420625f2428e172c2f847ed73750ecde.tar.gz
sonarqube-26a782fc420625f2428e172c2f847ed73750ecde.zip
SONAR-4283 notifications on new issues and issue changes
Diffstat (limited to 'sonar-core/src/test')
-rw-r--r--sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueQueryResultTest.java40
-rw-r--r--sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java93
-rw-r--r--sonar-core/src/test/java/org/sonar/core/notification/DefaultNotificationManagerTest.java15
-rw-r--r--sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java30
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/findNotificationSubscribers.xml50
5 files changed, 224 insertions, 4 deletions
diff --git a/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueQueryResultTest.java b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueQueryResultTest.java
new file mode 100644
index 00000000000..c3241d569c6
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueQueryResultTest.java
@@ -0,0 +1,40 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.core.issue;
+
+import org.junit.Test;
+import org.sonar.api.issue.Issue;
+
+import java.util.Arrays;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DefaultIssueQueryResultTest {
+ @Test
+ public void test_first_issue() {
+ DefaultIssueQueryResult result = new DefaultIssueQueryResult();
+ assertThat(result.first()).isNull();
+
+ Issue first = new DefaultIssue();
+ Issue second = new DefaultIssue();
+ result.setIssues(Arrays.asList(first, second));
+ assertThat(result.first()).isSameAs(first);
+ }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java b/sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java
new file mode 100644
index 00000000000..42c404ada63
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java
@@ -0,0 +1,93 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.core.issue;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.api.component.Component;
+import org.sonar.api.issue.Issue;
+import org.sonar.api.notifications.Notification;
+import org.sonar.api.notifications.NotificationManager;
+import org.sonar.api.resources.Project;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.core.i18n.RuleI18nManager;
+
+import java.util.Arrays;
+import java.util.Date;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+@RunWith(MockitoJUnitRunner.class)
+public class IssueNotificationsTest {
+
+ @Mock
+ NotificationManager manager;
+
+ @Mock
+ RuleI18nManager ruleI18n;
+
+ IssueNotifications issueNotifications;
+
+ @Before
+ public void setUp() throws Exception {
+ issueNotifications = new IssueNotifications(manager, ruleI18n);
+ }
+
+ @Test
+ public void sendNewIssues() throws Exception {
+ Date date = DateUtils.parseDateTime("2013-05-18T00:00:03+0200");
+ Project project = new Project("struts").setAnalysisDate(date);
+ Notification notification = issueNotifications.sendNewIssues(project, 42);
+
+ assertThat(notification.getFieldValue("count")).isEqualTo("42");
+ assertThat(notification.getFieldValue("projectDate")).isEqualTo("2013-05-18T00:00:03+0200");
+ Mockito.verify(manager).scheduleForSending(notification);
+ }
+
+ @Test
+ public void sendChanges() throws Exception {
+ IssueChangeContext context = IssueChangeContext.createScan(new Date());
+ DefaultIssue issue = new DefaultIssue()
+ .setMessage("the message")
+ .setKey("ABCDE")
+ .setAssignee("freddy")
+ .setFieldDiff(context, "resolution", null, "FIXED")
+ .setFieldDiff(context, "status", "OPEN", "RESOLVED")
+ .setComponentKey("struts:Action")
+ .setProjectKey("struts");
+ DefaultIssueQueryResult queryResult = new DefaultIssueQueryResult();
+ queryResult.setIssues(Arrays.<Issue>asList(issue));
+ queryResult.addProjects(Arrays.<Component>asList(new Project("struts")));
+
+ Notification notification = issueNotifications.sendChanges(issue, context, queryResult);
+
+ assertThat(notification.getFieldValue("message")).isEqualTo("the message");
+ assertThat(notification.getFieldValue("key")).isEqualTo("ABCDE");
+ assertThat(notification.getFieldValue("old.resolution")).isNull();
+ assertThat(notification.getFieldValue("new.resolution")).isEqualTo("FIXED");
+ assertThat(notification.getFieldValue("old.status")).isEqualTo("OPEN");
+ assertThat(notification.getFieldValue("new.status")).isEqualTo("RESOLVED");
+ Mockito.verify(manager).scheduleForSending(notification);
+ }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/notification/DefaultNotificationManagerTest.java b/sonar-core/src/test/java/org/sonar/core/notification/DefaultNotificationManagerTest.java
index 22ae4261326..7953acd55aa 100644
--- a/sonar-core/src/test/java/org/sonar/core/notification/DefaultNotificationManagerTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/notification/DefaultNotificationManagerTest.java
@@ -116,7 +116,7 @@ public class DefaultNotificationManagerTest extends AbstractDbUnitTestCase {
when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", null)).thenReturn(Lists.newArrayList("user3"));
when(propertiesDao.findUsersForNotification("NewAlerts", "Twitter", null)).thenReturn(Lists.newArrayList("user4"));
- Multimap<String, NotificationChannel> multiMap = manager.findSubscribedRecipientsForDispatcher(dispatcher, null);
+ Multimap<String, NotificationChannel> multiMap = manager.findSubscribedRecipientsForDispatcher(dispatcher, (Integer)null);
assertThat(multiMap.entries()).hasSize(3);
Map<String, Collection<NotificationChannel>> map = multiMap.asMap();
@@ -126,4 +126,17 @@ public class DefaultNotificationManagerTest extends AbstractDbUnitTestCase {
assertThat(map.get("user4")).isNull();
}
+ @Test
+ public void findNotificationSubscribers() {
+ when(propertiesDao.findNotificationSubscribers("NewViolations", "Email", "struts")).thenReturn(Lists.newArrayList("user1", "user2"));
+ when(propertiesDao.findNotificationSubscribers("NewViolations", "Twitter", "struts")).thenReturn(Lists.newArrayList("user2"));
+
+ Multimap<String, NotificationChannel> multiMap = manager.findNotificationSubscribers(dispatcher, "struts");
+ assertThat(multiMap.entries()).hasSize(3);
+
+ Map<String, Collection<NotificationChannel>> map = multiMap.asMap();
+ assertThat(map.get("user1")).containsOnly(emailChannel);
+ assertThat(map.get("user2")).containsOnly(emailChannel, twitterChannel);
+ assertThat(map.get("other")).isNull();
+ }
}
diff --git a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
index dfef7d1810e..d6f98d5e1c5 100644
--- a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
@@ -49,10 +49,10 @@ public class PropertiesDaoTest extends AbstractDaoTestCase {
setupData("shouldFindUsersForNotification");
List<String> users = dao.findUsersForNotification("NewViolations", "Email", null);
- assertThat(users).hasSize(0);
+ assertThat(users).isEmpty();
users = dao.findUsersForNotification("NewViolations", "Email", 78L);
- assertThat(users).hasSize(0);
+ assertThat(users).isEmpty();
users = dao.findUsersForNotification("NewViolations", "Email", 45L);
assertThat(users).hasSize(1);
@@ -63,7 +63,7 @@ public class PropertiesDaoTest extends AbstractDaoTestCase {
assertThat(users).containsOnly("user3");
users = dao.findUsersForNotification("NewViolations", "Twitter", 78L);
- assertThat(users).hasSize(0);
+ assertThat(users).isEmpty();
users = dao.findUsersForNotification("NewViolations", "Twitter", 56L);
assertThat(users).hasSize(2);
@@ -71,6 +71,30 @@ public class PropertiesDaoTest extends AbstractDaoTestCase {
}
@Test
+ public void findNotificationSubscribers() {
+ setupData("findNotificationSubscribers");
+
+ // Nobody is subscribed
+ List<String> users = dao.findNotificationSubscribers("NotSexyDispatcher", "Email", "org.apache:struts");
+ assertThat(users).isEmpty();
+
+ // Global subscribers
+ users = dao.findNotificationSubscribers("DispatcherWithGlobalSubscribers", "Email", "org.apache:struts");
+ assertThat(users).containsOnly("simon");
+
+ users = dao.findNotificationSubscribers("DispatcherWithGlobalSubscribers", "Email", null);
+ assertThat(users).containsOnly("simon");
+
+ // Project subscribers
+ users = dao.findNotificationSubscribers("DispatcherWithProjectSubscribers", "Email", "org.apache:struts");
+ assertThat(users).containsOnly("eric");
+
+ // Global + Project subscribers
+ users = dao.findNotificationSubscribers("DispatcherWithGlobalAndProjectSubscribers", "Email", "org.apache:struts");
+ assertThat(users).containsOnly("eric", "simon");
+ }
+
+ @Test
public void selectGlobalProperties() {
setupData("selectGlobalProperties");
List<PropertyDto> properties = dao.selectGlobalProperties();
diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/findNotificationSubscribers.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/findNotificationSubscribers.xml
new file mode 100644
index 00000000000..04042d7900c
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/findNotificationSubscribers.xml
@@ -0,0 +1,50 @@
+<dataset>
+
+ <users
+ id="1"
+ login="eric"
+ />
+
+ <users
+ id="2"
+ login="simon"
+ />
+
+ <projects id="42" kee="org.apache:struts"/>
+
+ <properties
+ id="1"
+ prop_key="notification.DispatcherWithGlobalSubscribers.Email"
+ text_value="true"
+ resource_id="[null]"
+ user_id="2"/>
+
+ <properties
+ id="2"
+ prop_key="notification.DispatcherWithProjectSubscribers.Email"
+ text_value="true"
+ resource_id="42"
+ user_id="1"/>
+
+ <properties
+ id="3"
+ prop_key="notification.DispatcherWithGlobalAndProjectSubscribers.Email"
+ text_value="true"
+ resource_id="56"
+ user_id="1"/>
+
+ <properties
+ id="4"
+ prop_key="notification.DispatcherWithGlobalAndProjectSubscribers.Email"
+ text_value="true"
+ resource_id="42"
+ user_id="1"/>
+
+ <properties
+ id="5"
+ prop_key="notification.DispatcherWithGlobalAndProjectSubscribers.Email"
+ text_value="true"
+ resource_id="[null]"
+ user_id="2"/>
+
+</dataset>