diff options
author | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2013-01-29 17:37:57 +0100 |
---|---|---|
committer | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2013-01-29 18:01:15 +0100 |
commit | 8d9eb9edf1ea931aabd94a4e6fe1cbe5343e93a3 (patch) | |
tree | ee90db58acf7dda6be3772d1845d954b31122dea /plugins | |
parent | 6d28b25606431eef5b911c4900a7ea4fd764b3df (diff) | |
download | sonarqube-8d9eb9edf1ea931aabd94a4e6fe1cbe5343e93a3.tar.gz sonarqube-8d9eb9edf1ea931aabd94a4e6fe1cbe5343e93a3.zip |
SONAR-3959 Update NotifDispatchers to use new NotifManager method
Diffstat (limited to 'plugins')
7 files changed, 183 insertions, 44 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewNotifications.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewNotifications.java index 5122703ed7d..96a3a064e4b 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewNotifications.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewNotifications.java @@ -59,6 +59,7 @@ public class ReviewNotifications implements BatchExtension { return new Notification("review-changed") .setFieldValue("reviewId", String.valueOf(review.getId())) .setFieldValue("project", project.getRoot().getLongName()) + .setFieldValue("projectId", String.valueOf(project.getId())) .setFieldValue("resource", resource != null ? resource.getLongName() : null) .setFieldValue("title", review.getTitle()) .setFieldValue("creator", getCreator(review)) diff --git a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/alerts/AlertsOnMyFavouriteProject.java b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/alerts/AlertsOnMyFavouriteProject.java index 5b073afdd79..49c7da72e02 100644 --- a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/alerts/AlertsOnMyFavouriteProject.java +++ b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/alerts/AlertsOnMyFavouriteProject.java @@ -19,10 +19,14 @@ */ package org.sonar.plugins.emailnotifications.alerts; +import com.google.common.collect.Multimap; import org.sonar.api.notifications.Notification; +import org.sonar.api.notifications.NotificationChannel; import org.sonar.api.notifications.NotificationDispatcher; +import org.sonar.api.notifications.NotificationManager; import org.sonar.core.properties.PropertiesDao; +import java.util.Collection; import java.util.List; /** @@ -32,19 +36,26 @@ import java.util.List; */ public class AlertsOnMyFavouriteProject extends NotificationDispatcher { + private NotificationManager notificationManager; private PropertiesDao propertiesDao; - public AlertsOnMyFavouriteProject(PropertiesDao propertiesDao) { + public AlertsOnMyFavouriteProject(NotificationManager notificationManager, PropertiesDao propertiesDao) { super("alerts"); + this.notificationManager = notificationManager; this.propertiesDao = propertiesDao; } @Override public void dispatch(Notification notification, Context context) { - Long projectId = Long.parseLong(notification.getFieldValue("projectId")); - List<String> userLogins = propertiesDao.findUserIdsForFavouriteResource(projectId); + // "null" is passed as a 2nd argument because this dispatcher is not a per-project dispatcher + Multimap<String, NotificationChannel> subscribedRecipients = notificationManager.findSubscribedRecipientsForDispatcher(this, null); + + List<String> userLogins = propertiesDao.findUserIdsForFavouriteResource(Long.parseLong(notification.getFieldValue("projectId"))); for (String userLogin : userLogins) { - context.addUser(userLogin); + Collection<NotificationChannel> channels = subscribedRecipients.get(userLogin); + for (NotificationChannel channel : channels) { + context.addUser(userLogin, channel); + } } } diff --git a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsOnMyFavouriteProject.java b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsOnMyFavouriteProject.java index cb101e4a023..1125bb02dee 100644 --- a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsOnMyFavouriteProject.java +++ b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsOnMyFavouriteProject.java @@ -19,10 +19,14 @@ */ package org.sonar.plugins.emailnotifications.newviolations; +import com.google.common.collect.Multimap; import org.sonar.api.notifications.Notification; +import org.sonar.api.notifications.NotificationChannel; import org.sonar.api.notifications.NotificationDispatcher; +import org.sonar.api.notifications.NotificationManager; import org.sonar.core.properties.PropertiesDao; +import java.util.Collection; import java.util.List; /** @@ -32,19 +36,26 @@ import java.util.List; */ public class NewViolationsOnMyFavouriteProject extends NotificationDispatcher { + private NotificationManager notificationManager; private PropertiesDao propertiesDao; - public NewViolationsOnMyFavouriteProject(PropertiesDao propertiesDao) { + public NewViolationsOnMyFavouriteProject(NotificationManager notificationManager, PropertiesDao propertiesDao) { super("new-violations"); + this.notificationManager = notificationManager; this.propertiesDao = propertiesDao; } @Override public void dispatch(Notification notification, Context context) { - Long projectId = Long.parseLong(notification.getFieldValue("projectId")); - List<String> userLogins = propertiesDao.findUserIdsForFavouriteResource(projectId); + // "null" is passed as a 2nd argument because this dispatcher is not a per-project dispatcher + Multimap<String, NotificationChannel> subscribedRecipients = notificationManager.findSubscribedRecipientsForDispatcher(this, null); + + List<String> userLogins = propertiesDao.findUserIdsForFavouriteResource(Long.parseLong(notification.getFieldValue("projectId"))); for (String userLogin : userLogins) { - context.addUser(userLogin); + Collection<NotificationChannel> channels = subscribedRecipients.get(userLogin); + for (NotificationChannel channel : channels) { + context.addUser(userLogin, channel); + } } } diff --git a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/reviews/ChangesInReviewAssignedToMeOrCreatedByMe.java b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/reviews/ChangesInReviewAssignedToMeOrCreatedByMe.java index 582c21d7367..31b1952b1b3 100644 --- a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/reviews/ChangesInReviewAssignedToMeOrCreatedByMe.java +++ b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/reviews/ChangesInReviewAssignedToMeOrCreatedByMe.java @@ -19,9 +19,14 @@ */ package org.sonar.plugins.emailnotifications.reviews; +import com.google.common.collect.Multimap; import org.apache.commons.lang.StringUtils; import org.sonar.api.notifications.Notification; +import org.sonar.api.notifications.NotificationChannel; import org.sonar.api.notifications.NotificationDispatcher; +import org.sonar.api.notifications.NotificationManager; + +import java.util.Collection; /** * This dispatcher means: "notify me when someone changes review assigned to me or created by me". @@ -30,24 +35,37 @@ import org.sonar.api.notifications.NotificationDispatcher; */ public class ChangesInReviewAssignedToMeOrCreatedByMe extends NotificationDispatcher { - public ChangesInReviewAssignedToMeOrCreatedByMe() { + private NotificationManager notificationManager; + + public ChangesInReviewAssignedToMeOrCreatedByMe(NotificationManager notificationManager) { super("review-changed"); + this.notificationManager = notificationManager; } @Override public void dispatch(Notification notification, Context context) { + int projectId = Integer.parseInt(notification.getFieldValue("projectId")); + Multimap<String, NotificationChannel> subscribedRecipients = notificationManager.findSubscribedRecipientsForDispatcher(this, projectId); + String author = notification.getFieldValue("author"); String creator = notification.getFieldValue("creator"); String oldAssignee = notification.getFieldValue("old.assignee"); String assignee = notification.getFieldValue("assignee"); if (creator != null && !StringUtils.equals(author, creator)) { - context.addUser(creator); + addUserToContextIfSubscribed(context, creator, subscribedRecipients); } if (oldAssignee != null && !StringUtils.equals(author, oldAssignee)) { - context.addUser(oldAssignee); + addUserToContextIfSubscribed(context, oldAssignee, subscribedRecipients); } if (assignee != null && !StringUtils.equals(author, assignee)) { - context.addUser(assignee); + addUserToContextIfSubscribed(context, assignee, subscribedRecipients); + } + } + + private void addUserToContextIfSubscribed(Context context, String user, Multimap<String, NotificationChannel> subscribedRecipients) { + Collection<NotificationChannel> channels = subscribedRecipients.get(user); + for (NotificationChannel channel : channels) { + context.addUser(user, channel); } } diff --git a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/alerts/AlertsOnMyFavouriteProjectTest.java b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/alerts/AlertsOnMyFavouriteProjectTest.java index 7bf49f0c2ad..01887396b05 100644 --- a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/alerts/AlertsOnMyFavouriteProjectTest.java +++ b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/alerts/AlertsOnMyFavouriteProjectTest.java @@ -19,14 +19,20 @@ */ package org.sonar.plugins.emailnotifications.alerts; +import com.google.common.collect.HashMultimap; import com.google.common.collect.Lists; +import com.google.common.collect.Multimap; +import org.junit.Before; import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.sonar.api.notifications.Notification; +import org.sonar.api.notifications.NotificationChannel; import org.sonar.api.notifications.NotificationDispatcher; +import org.sonar.api.notifications.NotificationManager; import org.sonar.core.properties.PropertiesDao; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -34,28 +40,49 @@ import static org.mockito.Mockito.when; public class AlertsOnMyFavouriteProjectTest { + @Mock + private NotificationManager notificationManager; + + @Mock + private PropertiesDao propertiesDao; + + @Mock + private NotificationDispatcher.Context context; + + @Mock + private NotificationChannel emailChannel; + + @Mock + private NotificationChannel twitterChannel; + + private AlertsOnMyFavouriteProject dispatcher; + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + dispatcher = new AlertsOnMyFavouriteProject(notificationManager, propertiesDao); + } + @Test - public void shouldNotDispatchIfNotNewViolationsNotification() throws Exception { - NotificationDispatcher.Context context = mock(NotificationDispatcher.Context.class); - AlertsOnMyFavouriteProject dispatcher = new AlertsOnMyFavouriteProject(null); + public void shouldNotDispatchIfNotAlertsNotification() throws Exception { Notification notification = new Notification("other-notif"); dispatcher.performDispatch(notification, context); - verify(context, never()).addUser(any(String.class)); + verify(context, never()).addUser(any(String.class), any(NotificationChannel.class)); } @Test - public void shouldDispatchToUsersWhoHaveFlaggedProjectAsFavourite() { - NotificationDispatcher.Context context = mock(NotificationDispatcher.Context.class); - PropertiesDao propertiesDao = mock(PropertiesDao.class); - when(propertiesDao.findUserIdsForFavouriteResource(34L)).thenReturn(Lists.newArrayList("user1", "user2")); - AlertsOnMyFavouriteProject dispatcher = new AlertsOnMyFavouriteProject(propertiesDao); + public void shouldDispatchToUsersWhoHaveSubscribedAndFlaggedProjectAsFavourite() { + Multimap<String, NotificationChannel> recipients = HashMultimap.create(); + recipients.put("user1", emailChannel); + recipients.put("user2", twitterChannel); + when(notificationManager.findSubscribedRecipientsForDispatcher(dispatcher, null)).thenReturn(recipients); + when(propertiesDao.findUserIdsForFavouriteResource(34L)).thenReturn(Lists.newArrayList("user2", "user3")); Notification notification = new Notification("alerts").setFieldValue("projectId", "34"); dispatcher.performDispatch(notification, context); - verify(context).addUser("user1"); - verify(context).addUser("user2"); + verify(context).addUser("user2", twitterChannel); verifyNoMoreInteractions(context); } diff --git a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsOnMyFavouriteProjectTest.java b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsOnMyFavouriteProjectTest.java index 4e8e66e002e..91de747a3e3 100644 --- a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsOnMyFavouriteProjectTest.java +++ b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsOnMyFavouriteProjectTest.java @@ -19,14 +19,20 @@ */ package org.sonar.plugins.emailnotifications.newviolations; +import com.google.common.collect.HashMultimap; import com.google.common.collect.Lists; +import com.google.common.collect.Multimap; +import org.junit.Before; import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.sonar.api.notifications.Notification; +import org.sonar.api.notifications.NotificationChannel; import org.sonar.api.notifications.NotificationDispatcher; +import org.sonar.api.notifications.NotificationManager; import org.sonar.core.properties.PropertiesDao; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -34,28 +40,49 @@ import static org.mockito.Mockito.when; public class NewViolationsOnMyFavouriteProjectTest { + @Mock + private NotificationManager notificationManager; + + @Mock + private PropertiesDao propertiesDao; + + @Mock + private NotificationDispatcher.Context context; + + @Mock + private NotificationChannel emailChannel; + + @Mock + private NotificationChannel twitterChannel; + + private NewViolationsOnMyFavouriteProject dispatcher; + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + dispatcher = new NewViolationsOnMyFavouriteProject(notificationManager, propertiesDao); + } + @Test public void shouldNotDispatchIfNotNewViolationsNotification() throws Exception { - NotificationDispatcher.Context context = mock(NotificationDispatcher.Context.class); - NewViolationsOnMyFavouriteProject dispatcher = new NewViolationsOnMyFavouriteProject(null); Notification notification = new Notification("other-notif"); dispatcher.performDispatch(notification, context); - verify(context, never()).addUser(any(String.class)); + verify(context, never()).addUser(any(String.class), any(NotificationChannel.class)); } @Test - public void shouldDispatchToUsersWhoHaveFlaggedProjectAsFavourite() { - NotificationDispatcher.Context context = mock(NotificationDispatcher.Context.class); - PropertiesDao propertiesDao = mock(PropertiesDao.class); - when(propertiesDao.findUserIdsForFavouriteResource(34L)).thenReturn(Lists.newArrayList("user1", "user2")); - NewViolationsOnMyFavouriteProject dispatcher = new NewViolationsOnMyFavouriteProject(propertiesDao); + public void shouldDispatchToUsersWhoHaveSubscribedAndFlaggedProjectAsFavourite() { + Multimap<String, NotificationChannel> recipients = HashMultimap.create(); + recipients.put("user1", emailChannel); + recipients.put("user2", twitterChannel); + when(notificationManager.findSubscribedRecipientsForDispatcher(dispatcher, null)).thenReturn(recipients); + when(propertiesDao.findUserIdsForFavouriteResource(34L)).thenReturn(Lists.newArrayList("user2", "user3")); Notification notification = new Notification("new-violations").setFieldValue("projectId", "34"); dispatcher.performDispatch(notification, context); - verify(context).addUser("user1"); - verify(context).addUser("user2"); + verify(context).addUser("user2", twitterChannel); verifyNoMoreInteractions(context); } diff --git a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/reviews/ChangesInReviewAssignedToMeOrCreatedByMeTest.java b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/reviews/ChangesInReviewAssignedToMeOrCreatedByMeTest.java index b0f89604bc4..a5b68352aa2 100644 --- a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/reviews/ChangesInReviewAssignedToMeOrCreatedByMeTest.java +++ b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/reviews/ChangesInReviewAssignedToMeOrCreatedByMeTest.java @@ -19,46 +19,90 @@ */ package org.sonar.plugins.emailnotifications.reviews; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; import org.junit.Before; import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.sonar.api.notifications.Notification; +import org.sonar.api.notifications.NotificationChannel; import org.sonar.api.notifications.NotificationDispatcher; +import org.sonar.api.notifications.NotificationManager; -import static org.mockito.Mockito.mock; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; public class ChangesInReviewAssignedToMeOrCreatedByMeTest { + @Mock + private NotificationManager notificationManager; + + @Mock private NotificationDispatcher.Context context; + + @Mock + private NotificationChannel emailChannel; + + @Mock + private NotificationChannel twitterChannel; + private ChangesInReviewAssignedToMeOrCreatedByMe dispatcher; @Before public void setUp() { - context = mock(NotificationDispatcher.Context.class); - dispatcher = new ChangesInReviewAssignedToMeOrCreatedByMe(); + MockitoAnnotations.initMocks(this); + + dispatcher = new ChangesInReviewAssignedToMeOrCreatedByMe(notificationManager); + } + + @Test + public void shouldNotDispatchIfNotNewViolationsNotification() throws Exception { + Notification notification = new Notification("other-notif"); + dispatcher.performDispatch(notification, context); + + verify(context, never()).addUser(any(String.class), any(NotificationChannel.class)); } @Test public void dispatchToCreatorAndAssignee() { + Multimap<String, NotificationChannel> recipients = HashMultimap.create(); + recipients.put("simon", emailChannel); + recipients.put("freddy", twitterChannel); + recipients.put("godin", twitterChannel); + when(notificationManager.findSubscribedRecipientsForDispatcher(dispatcher, 42)).thenReturn(recipients); + Notification notification = new Notification("review-changed") + .setFieldValue("projectId", "42") .setFieldValue("author", "olivier") .setFieldValue("creator", "simon") .setFieldValue("old.assignee", "godin") .setFieldValue("assignee", "freddy"); dispatcher.performDispatch(notification, context); - verify(context).addUser("simon"); - verify(context).addUser("godin"); - verify(context).addUser("freddy"); + verify(context).addUser("simon", emailChannel); + verify(context).addUser("freddy", twitterChannel); + verify(context).addUser("godin", twitterChannel); verifyNoMoreInteractions(context); } @Test public void doNotDispatchToAuthorOfChanges() { - dispatcher.performDispatch(new Notification("review-changed").setFieldValue("author", "simon").setFieldValue("creator", "simon"), context); - dispatcher.performDispatch(new Notification("review-changed").setFieldValue("author", "simon").setFieldValue("assignee", "simon"), context); - dispatcher.performDispatch(new Notification("review-changed").setFieldValue("author", "simon").setFieldValue("old.assignee", "simon"), context); + Multimap<String, NotificationChannel> recipients = HashMultimap.create(); + recipients.put("simon", emailChannel); + recipients.put("freddy", twitterChannel); + recipients.put("godin", twitterChannel); + when(notificationManager.findSubscribedRecipientsForDispatcher(dispatcher, 42)).thenReturn(recipients); + + dispatcher.performDispatch(new Notification("review-changed").setFieldValue("projectId", "42") + .setFieldValue("author", "simon").setFieldValue("creator", "simon"), context); + dispatcher.performDispatch(new Notification("review-changed").setFieldValue("projectId", "42") + .setFieldValue("author", "simon").setFieldValue("assignee", "simon"), context); + dispatcher.performDispatch(new Notification("review-changed").setFieldValue("projectId", "42") + .setFieldValue("author", "simon").setFieldValue("old.assignee", "simon"), context); verifyNoMoreInteractions(context); } |