3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program 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.
11 * This program 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.
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.
20 package org.sonar.server.issue.notification;
22 import com.google.common.collect.HashMultimap;
23 import com.google.common.collect.Multimap;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.notifications.Notification;
27 import org.sonar.api.notifications.NotificationChannel;
28 import org.sonar.api.web.UserRole;
29 import org.sonar.server.notification.NotificationDispatcher;
30 import org.sonar.server.notification.NotificationManager;
32 import static org.mockito.Mockito.*;
34 public class NewIssuesNotificationDispatcherTest {
36 private NotificationManager notifications = mock(NotificationManager.class);
37 private NotificationDispatcher.Context context = mock(NotificationDispatcher.Context.class);
38 private NotificationChannel emailChannel = mock(NotificationChannel.class);
39 private NotificationChannel twitterChannel = mock(NotificationChannel.class);
40 private NewIssuesNotificationDispatcher dispatcher = mock(NewIssuesNotificationDispatcher.class);
44 dispatcher = new NewIssuesNotificationDispatcher(notifications);
48 public void shouldNotDispatchIfNotNewViolationsNotification() {
49 Notification notification = new Notification("other-notif");
50 dispatcher.performDispatch(notification, context);
52 verify(context, never()).addUser(any(String.class), any(NotificationChannel.class));
56 public void shouldDispatchToUsersWhoHaveSubscribedAndFlaggedProjectAsFavourite() {
57 Multimap<String, NotificationChannel> recipients = HashMultimap.create();
58 recipients.put("user1", emailChannel);
59 recipients.put("user2", twitterChannel);
60 when(notifications.findSubscribedRecipientsForDispatcher(dispatcher, "uuid1", new NotificationManager.SubscriberPermissionsOnProject(UserRole.USER))).thenReturn(recipients);
62 Notification notification = new Notification(NewIssuesNotification.TYPE)
63 .setFieldValue("projectKey", "struts")
64 .setFieldValue("projectUuid", "uuid1");
65 dispatcher.performDispatch(notification, context);
67 verify(context).addUser("user1", emailChannel);
68 verify(context).addUser("user2", twitterChannel);
69 verifyNoMoreInteractions(context);