import org.sonar.plugins.core.measurefilters.ProjectFilter;
import org.sonar.plugins.core.notifications.alerts.NewAlerts;
import org.sonar.plugins.core.notifications.reviews.ChangesInReviewAssignedToMeOrCreatedByMe;
+import org.sonar.plugins.core.notifications.reviews.NewFalsePositiveReview;
import org.sonar.plugins.core.notifications.violations.NewViolationsOnFirstDifferentialPeriod;
import org.sonar.plugins.core.security.ApplyProjectRolesDecorator;
import org.sonar.plugins.core.sensors.BranchCoverageDecorator;
ChangesInReviewAssignedToMeOrCreatedByMe.class,
NotificationDispatcherMetadata.create("ChangesInReviewAssignedToMeOrCreatedByMe")
.setProperty(NotificationDispatcherMetadata.GLOBAL_NOTIFICATION, String.valueOf(true))
- .setProperty(NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION, String.valueOf(true)));
+ .setProperty(NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION, String.valueOf(true)),
+ // Notify new false positive resolution
+ NewFalsePositiveReview.class,
+ NotificationDispatcherMetadata.create("NewFalsePositiveReview")
+ .setProperty(NotificationDispatcherMetadata.GLOBAL_NOTIFICATION, String.valueOf(true))
+ .setProperty(NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION, String.valueOf(true))
+ );
}
}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+
+package org.sonar.plugins.core.notifications.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 org.sonar.core.review.ReviewDto;
+
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * This dispatcher means: "notify me when someone resolve a review as false positive".
+ *
+ * @since 3.6
+ */
+public class NewFalsePositiveReview extends NotificationDispatcher {
+
+ private NotificationManager notificationManager;
+
+ public NewFalsePositiveReview(NotificationManager notificationManager) {
+ super("review-changed");
+ this.notificationManager = notificationManager;
+ }
+
+ @Override
+ public void dispatch(Notification notification, Context context) {
+ String newResolution = notification.getFieldValue("new.resolution");
+ if (StringUtils.equals(newResolution, ReviewDto.RESOLUTION_FALSE_POSITIVE)) {
+ String author = notification.getFieldValue("author");
+ int projectId = Integer.parseInt(notification.getFieldValue("projectId"));
+ Multimap<String, NotificationChannel> subscribedRecipients = notificationManager.findSubscribedRecipientsForDispatcher(this, projectId);
+ notify(author, context, subscribedRecipients);
+ }
+ }
+
+ private void notify(String author, Context context, Multimap<String, NotificationChannel> subscribedRecipients) {
+ for (Map.Entry<String, Collection<NotificationChannel>> channelsByRecipients : subscribedRecipients.asMap().entrySet()) {
+ String userLogin = channelsByRecipients.getKey();
+ if (!StringUtils.equals(author, userLogin)) {
+ for (NotificationChannel channel : channelsByRecipients.getValue()) {
+ context.addUser(userLogin, channel);
+ }
+ }
+ }
+ }
+
+}
notification.dispatcher.ChangesInReviewAssignedToMeOrCreatedByMe=Changes in reviews assigned to me or created by me
notification.dispatcher.NewViolationsOnFirstDifferentialPeriod=New violations introduced during the first differential period
notification.dispatcher.NewAlerts=New alerts
+notification.dispatcher.NewFalsePositiveReview=New false positive issue
#------------------------------------------------------------------------------
}
@Test
- public void shouldNotDispatchIfNotAlertsNotification() throws Exception {
+ public void should_not_dispatch_if_not_alerts_notification() throws Exception {
Notification notification = new Notification("other-notif");
dispatcher.performDispatch(notification, context);
}
@Test
- public void shouldDispatchToUsersWhoHaveSubscribedAndFlaggedProjectAsFavourite() {
+ public void should_dispatch_to_users_who_have_subscribed() {
Multimap<String, NotificationChannel> recipients = HashMultimap.create();
recipients.put("user1", emailChannel);
recipients.put("user2", twitterChannel);
*/
package org.sonar.plugins.core.notifications.reviews;
-import org.sonar.plugins.core.notifications.reviews.ChangesInReviewAssignedToMeOrCreatedByMe;
-
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import org.junit.Before;
private ChangesInReviewAssignedToMeOrCreatedByMe dispatcher;
@Before
- public void setUp() {
+ public void before() {
MockitoAnnotations.initMocks(this);
dispatcher = new ChangesInReviewAssignedToMeOrCreatedByMe(notificationManager);
}
@Test
- public void shouldNotDispatchIfNotNewViolationsNotification() throws Exception {
+ public void should_not_dispatch_if_not_new_violations_notification() throws Exception {
Notification notification = new Notification("other-notif");
dispatcher.performDispatch(notification, context);
}
@Test
- public void dispatchToCreatorAndAssignee() {
+ public void should_dispatch_to_creator_and_assignee() {
Multimap<String, NotificationChannel> recipients = HashMultimap.create();
recipients.put("simon", emailChannel);
recipients.put("freddy", twitterChannel);
}
@Test
- public void doNotDispatchToAuthorOfChanges() {
+ public void should_not_dispatch_to_author_of_changes() {
Multimap<String, NotificationChannel> recipients = HashMultimap.create();
recipients.put("simon", emailChannel);
recipients.put("freddy", twitterChannel);
}
@Test
- public void shouldNotDispatch() {
+ public void should_not_dispatch_when_other_notification_type() {
Notification notification = new Notification("other");
dispatcher.performDispatch(notification, context);
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+
+package org.sonar.plugins.core.notifications.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.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 NewFalsePositiveReviewTest {
+
+ @Mock
+ private NotificationManager notificationManager;
+
+ @Mock
+ private NotificationDispatcher.Context context;
+
+ @Mock
+ private NotificationChannel emailChannel;
+
+ @Mock
+ private NotificationChannel twitterChannel;
+
+ private NotificationDispatcher dispatcher;
+
+ @Before
+ public void before() {
+ MockitoAnnotations.initMocks(this);
+
+ dispatcher = new NewFalsePositiveReview(notificationManager);
+ }
+
+ @Test
+ public void should_not_dispatch_if_not_reviews_notification() 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 should_dispatch_false_positive_resolution_to_every_subscribers() {
+ Multimap<String, NotificationChannel> recipients = HashMultimap.create();
+ recipients.put("user1", emailChannel);
+ recipients.put("user2", twitterChannel);
+ when(notificationManager.findSubscribedRecipientsForDispatcher(dispatcher, 42)).thenReturn(recipients);
+
+ dispatcher.performDispatch(new Notification("review-changed")
+ .setFieldValue("projectId", "42")
+ .setFieldValue("author", "user3")
+ .setFieldValue("new.resolution", "FALSE-POSITIVE"),
+ context);
+
+ verify(context).addUser("user1", emailChannel);
+ verify(context).addUser("user2", twitterChannel);
+ verifyNoMoreInteractions(context);
+ }
+
+ @Test
+ public void should_not_dispatch_to_author_of_changes() {
+ Multimap<String, NotificationChannel> recipients = HashMultimap.create();
+ recipients.put("user1", emailChannel);
+ recipients.put("user2", twitterChannel);
+ when(notificationManager.findSubscribedRecipientsForDispatcher(dispatcher, 42)).thenReturn(recipients);
+
+ dispatcher.performDispatch(new Notification("review-changed")
+ .setFieldValue("projectId", "42")
+ .setFieldValue("author", "user1")
+ .setFieldValue("new.resolution", "FALSE-POSITIVE"),
+ context);
+
+ verify(context).addUser("user2", twitterChannel);
+ verify(context, never()).addUser("user1", emailChannel);
+ verifyNoMoreInteractions(context);
+ }
+
+ @Test
+ public void should_not_dispatch_other_than_false_positive_resolution() {
+ Multimap<String, NotificationChannel> recipients = HashMultimap.create();
+ recipients.put("user", emailChannel);
+ when(notificationManager.findSubscribedRecipientsForDispatcher(dispatcher, 42)).thenReturn(recipients);
+
+ dispatcher.performDispatch(new Notification("review-changed")
+ .setFieldValue("projectId", "42")
+ .setFieldValue("author", "user2")
+ .setFieldValue("new.assignee", "user"), context);
+
+ verify(context, never()).addUser(any(String.class), any(NotificationChannel.class));
+ }
+}