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 MyNewIssuesNotificationDispatcherTest {
36 private MyNewIssuesNotificationDispatcher underTest;
38 private NotificationManager notificationManager = mock(NotificationManager.class);
39 private NotificationDispatcher.Context context = mock(NotificationDispatcher.Context.class);
40 private NotificationChannel emailChannel = mock(NotificationChannel.class);
41 private NotificationChannel twitterChannel = mock(NotificationChannel.class);
46 underTest = new MyNewIssuesNotificationDispatcher(notificationManager);
50 public void do_not_dispatch_if_no_new_notification() {
51 Notification notification = new Notification("other-notif");
52 underTest.performDispatch(notification, context);
54 verify(context, never()).addUser(any(String.class), any(NotificationChannel.class));
58 public void dispatch_to_users_who_have_subscribed_to_notification_and_project() {
59 Multimap<String, NotificationChannel> recipients = HashMultimap.create();
60 recipients.put("user1", emailChannel);
61 recipients.put("user2", twitterChannel);
62 when(notificationManager.findSubscribedRecipientsForDispatcher(underTest, "uuid1", new NotificationManager.SubscriberPermissionsOnProject(UserRole.USER))).thenReturn(recipients);
64 Notification notification = new Notification(MyNewIssuesNotification.MY_NEW_ISSUES_NOTIF_TYPE)
65 .setFieldValue("projectKey", "struts")
66 .setFieldValue("projectUuid", "uuid1")
67 .setFieldValue("assignee", "user1");
68 underTest.performDispatch(notification, context);
70 verify(context).addUser("user1", emailChannel);
71 verifyNoMoreInteractions(context);