diff options
author | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2013-01-28 14:15:45 +0100 |
---|---|---|
committer | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2013-01-29 18:01:15 +0100 |
commit | 4fa03eea720ef2067bb2f920edd6c3f8f0080b85 (patch) | |
tree | 9a7080a1777ec7f4751263ce7bc0e78938d8f9b8 | |
parent | c964f2ea5fc57d72d19d7f2bfbedf9fe62c7c33c (diff) | |
download | sonarqube-4fa03eea720ef2067bb2f920edd6c3f8f0080b85.tar.gz sonarqube-4fa03eea720ef2067bb2f920edd6c3f8f0080b85.zip |
SONAR-3959 Add notification type on NotificationDispatcher
8 files changed, 84 insertions, 51 deletions
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 38a46a6ca49..5b073afdd79 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,7 +19,6 @@ */ package org.sonar.plugins.emailnotifications.alerts; -import org.apache.commons.lang.StringUtils; import org.sonar.api.notifications.Notification; import org.sonar.api.notifications.NotificationDispatcher; import org.sonar.core.properties.PropertiesDao; @@ -36,17 +35,16 @@ public class AlertsOnMyFavouriteProject extends NotificationDispatcher { private PropertiesDao propertiesDao; public AlertsOnMyFavouriteProject(PropertiesDao propertiesDao) { + super("alerts"); this.propertiesDao = propertiesDao; } @Override public void dispatch(Notification notification, Context context) { - if (StringUtils.equals(notification.getType(), "alerts")) { - Long projectId = Long.parseLong(notification.getFieldValue("projectId")); - List<String> userLogins = propertiesDao.findUserIdsForFavouriteResource(projectId); - for (String userLogin : userLogins) { - context.addUser(userLogin); - } + Long projectId = Long.parseLong(notification.getFieldValue("projectId")); + List<String> userLogins = propertiesDao.findUserIdsForFavouriteResource(projectId); + for (String userLogin : userLogins) { + context.addUser(userLogin); } } 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 d0db106f056..cb101e4a023 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,13 +19,12 @@ */ package org.sonar.plugins.emailnotifications.newviolations; -import java.util.List; - -import org.apache.commons.lang.StringUtils; import org.sonar.api.notifications.Notification; import org.sonar.api.notifications.NotificationDispatcher; import org.sonar.core.properties.PropertiesDao; +import java.util.List; + /** * This dispatcher means: "notify me when new violations are introduced on projects that I flagged as favourite". * @@ -36,17 +35,16 @@ public class NewViolationsOnMyFavouriteProject extends NotificationDispatcher { private PropertiesDao propertiesDao; public NewViolationsOnMyFavouriteProject(PropertiesDao propertiesDao) { + super("new-violations"); this.propertiesDao = propertiesDao; } @Override public void dispatch(Notification notification, Context context) { - if (StringUtils.equals(notification.getType(), "new-violations")) { - Long projectId = Long.parseLong(notification.getFieldValue("projectId")); - List<String> userLogins = propertiesDao.findUserIdsForFavouriteResource(projectId); - for (String userLogin : userLogins) { - context.addUser(userLogin); - } + Long projectId = Long.parseLong(notification.getFieldValue("projectId")); + List<String> userLogins = propertiesDao.findUserIdsForFavouriteResource(projectId); + for (String userLogin : userLogins) { + context.addUser(userLogin); } } 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 047b6269d1c..582c21d7367 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 @@ -30,22 +30,24 @@ import org.sonar.api.notifications.NotificationDispatcher; */ public class ChangesInReviewAssignedToMeOrCreatedByMe extends NotificationDispatcher { + public ChangesInReviewAssignedToMeOrCreatedByMe() { + super("review-changed"); + } + @Override public void dispatch(Notification notification, Context context) { - if (StringUtils.startsWith(notification.getType(), "review")) { - String author = notification.getFieldValue("author"); // author of change - String creator = notification.getFieldValue("creator"); // creator of review - String oldAssignee = notification.getFieldValue("old.assignee"); // previous assignee - String assignee = notification.getFieldValue("assignee"); // current assignee - if (creator != null && !StringUtils.equals(author, creator)) { - context.addUser(creator); - } - if (oldAssignee != null && !StringUtils.equals(author, oldAssignee)) { - context.addUser(oldAssignee); - } - if (assignee != null && !StringUtils.equals(author, assignee)) { - context.addUser(assignee); - } + 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); + } + if (oldAssignee != null && !StringUtils.equals(author, oldAssignee)) { + context.addUser(oldAssignee); + } + if (assignee != null && !StringUtils.equals(author, assignee)) { + context.addUser(assignee); } } 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 fe29748d332..7bf49f0c2ad 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 @@ -39,7 +39,7 @@ public class AlertsOnMyFavouriteProjectTest { NotificationDispatcher.Context context = mock(NotificationDispatcher.Context.class); AlertsOnMyFavouriteProject dispatcher = new AlertsOnMyFavouriteProject(null); Notification notification = new Notification("other-notif"); - dispatcher.dispatch(notification, context); + dispatcher.performDispatch(notification, context); verify(context, never()).addUser(any(String.class)); } @@ -52,7 +52,7 @@ public class AlertsOnMyFavouriteProjectTest { AlertsOnMyFavouriteProject dispatcher = new AlertsOnMyFavouriteProject(propertiesDao); Notification notification = new Notification("alerts").setFieldValue("projectId", "34"); - dispatcher.dispatch(notification, context); + dispatcher.performDispatch(notification, context); verify(context).addUser("user1"); verify(context).addUser("user2"); 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 5a4b1adce3a..4e8e66e002e 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,6 +19,12 @@ */ package org.sonar.plugins.emailnotifications.newviolations; +import com.google.common.collect.Lists; +import org.junit.Test; +import org.sonar.api.notifications.Notification; +import org.sonar.api.notifications.NotificationDispatcher; +import org.sonar.core.properties.PropertiesDao; + import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -26,13 +32,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; -import org.junit.Test; -import org.sonar.api.notifications.Notification; -import org.sonar.api.notifications.NotificationDispatcher; -import org.sonar.core.properties.PropertiesDao; - -import com.google.common.collect.Lists; - public class NewViolationsOnMyFavouriteProjectTest { @Test @@ -40,7 +39,7 @@ public class NewViolationsOnMyFavouriteProjectTest { NotificationDispatcher.Context context = mock(NotificationDispatcher.Context.class); NewViolationsOnMyFavouriteProject dispatcher = new NewViolationsOnMyFavouriteProject(null); Notification notification = new Notification("other-notif"); - dispatcher.dispatch(notification, context); + dispatcher.performDispatch(notification, context); verify(context, never()).addUser(any(String.class)); } @@ -53,7 +52,7 @@ public class NewViolationsOnMyFavouriteProjectTest { NewViolationsOnMyFavouriteProject dispatcher = new NewViolationsOnMyFavouriteProject(propertiesDao); Notification notification = new Notification("new-violations").setFieldValue("projectId", "34"); - dispatcher.dispatch(notification, context); + dispatcher.performDispatch(notification, context); verify(context).addUser("user1"); verify(context).addUser("user2"); 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 a2e2f215771..b0f89604bc4 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,15 +19,15 @@ */ package org.sonar.plugins.emailnotifications.reviews; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; - import org.junit.Before; import org.junit.Test; import org.sonar.api.notifications.Notification; import org.sonar.api.notifications.NotificationDispatcher; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + public class ChangesInReviewAssignedToMeOrCreatedByMeTest { private NotificationDispatcher.Context context; @@ -46,7 +46,7 @@ public class ChangesInReviewAssignedToMeOrCreatedByMeTest { .setFieldValue("creator", "simon") .setFieldValue("old.assignee", "godin") .setFieldValue("assignee", "freddy"); - dispatcher.dispatch(notification, context); + dispatcher.performDispatch(notification, context); verify(context).addUser("simon"); verify(context).addUser("godin"); @@ -56,9 +56,9 @@ public class ChangesInReviewAssignedToMeOrCreatedByMeTest { @Test public void doNotDispatchToAuthorOfChanges() { - dispatcher.dispatch(new Notification("review-changed").setFieldValue("author", "simon").setFieldValue("creator", "simon"), context); - dispatcher.dispatch(new Notification("review-changed").setFieldValue("author", "simon").setFieldValue("assignee", "simon"), context); - dispatcher.dispatch(new Notification("review-changed").setFieldValue("author", "simon").setFieldValue("old.assignee", "simon"), context); + 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); verifyNoMoreInteractions(context); } @@ -66,7 +66,7 @@ public class ChangesInReviewAssignedToMeOrCreatedByMeTest { @Test public void shouldNotDispatch() { Notification notification = new Notification("other"); - dispatcher.dispatch(notification, context); + dispatcher.performDispatch(notification, context); verifyNoMoreInteractions(context); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/notifications/NotificationDispatcher.java b/sonar-plugin-api/src/main/java/org/sonar/api/notifications/NotificationDispatcher.java index 0516215729a..9956d3df092 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/notifications/NotificationDispatcher.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/notifications/NotificationDispatcher.java @@ -19,6 +19,7 @@ */ package org.sonar.api.notifications; +import org.apache.commons.lang.StringUtils; import org.sonar.api.ServerExtension; /** @@ -38,6 +39,8 @@ import org.sonar.api.ServerExtension; */ public abstract class NotificationDispatcher implements ServerExtension { + private String notificationType; + /** * Additional information related to the notification, which will be used * to know who should receive the notification. @@ -52,6 +55,25 @@ public abstract class NotificationDispatcher implements ServerExtension { } /** + * Creates a new dispatcher for notifications of the given type. + * + * @param notificationType the type of notifications handled by this dispatcher + */ + public NotificationDispatcher(String notificationType) { + this.notificationType = notificationType; + } + + /** + * Creates a new generic dispatcher, used for any kind of notification. + * <br/> + * Should be avoided and replaced by the other constructor - as it is easier to understand that a + * dispatcher listens for a specific type of notification. + */ + public NotificationDispatcher() { + this(""); + } + + /** * Returns the unique key of this dispatcher. * * @return the key @@ -62,6 +84,20 @@ public abstract class NotificationDispatcher implements ServerExtension { /** * <p> + * Performs the dispatch. + * </p> + * + * @param notification the notification that will be sent + * @param the context linked to this notification + */ + public final void performDispatch(Notification notification, Context context) { + if (StringUtils.equals(notification.getType(), notificationType) || StringUtils.equals("", notificationType)) { + dispatch(notification, context); + } + } + + /** + * <p> * Implements the logic that defines which users will receive the notification. * </p> * The purpose of this method is to populate the context object with users, based on the type of notification and the content of the notification. diff --git a/sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java b/sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java index 96dfa809ccc..aeeda4c6499 100644 --- a/sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java +++ b/sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java @@ -134,7 +134,7 @@ public class NotificationService implements ServerComponent { } }; try { - dispatcher.dispatch(notification, context); + dispatcher.performDispatch(notification, context); } catch (Exception e) { // catch all exceptions in order to dispatch using other dispatchers Logs.INFO.warn("Unable to dispatch notification " + notification + " using " + dispatcher, e); } |