2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.plugins.core.notifications.alerts;
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.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.sonar.api.notifications.Notification;
29 import org.sonar.api.notifications.NotificationChannel;
30 import org.sonar.api.notifications.NotificationDispatcher;
31 import org.sonar.api.notifications.NotificationManager;
33 import static org.mockito.Matchers.any;
34 import static org.mockito.Mockito.never;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.verifyNoMoreInteractions;
37 import static org.mockito.Mockito.when;
39 public class NewAlertsTest {
42 private NotificationManager notificationManager;
45 private NotificationDispatcher.Context context;
48 private NotificationChannel emailChannel;
51 private NotificationChannel twitterChannel;
53 private NewAlerts dispatcher;
57 MockitoAnnotations.initMocks(this);
58 dispatcher = new NewAlerts(notificationManager);
62 public void should_not_dispatch_if_not_alerts_notification() throws Exception {
63 Notification notification = new Notification("other-notif");
64 dispatcher.performDispatch(notification, context);
66 verify(context, never()).addUser(any(String.class), any(NotificationChannel.class));
70 public void should_dispatch_to_users_who_have_subscribed() {
71 Multimap<String, NotificationChannel> recipients = HashMultimap.create();
72 recipients.put("user1", emailChannel);
73 recipients.put("user2", twitterChannel);
74 when(notificationManager.findSubscribedRecipientsForDispatcher(dispatcher, 34)).thenReturn(recipients);
76 Notification notification = new Notification("alerts").setFieldValue("projectId", "34");
77 dispatcher.performDispatch(notification, context);
79 verify(context).addUser("user1", emailChannel);
80 verify(context).addUser("user2", twitterChannel);
81 verifyNoMoreInteractions(context);