3 * Copyright (C) 2009-2023 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.notification;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.sonar.api.notifications.Notification;
27 import org.sonar.api.notifications.NotificationChannel;
29 import static org.hamcrest.CoreMatchers.is;
30 import static org.junit.Assert.assertThat;
31 import static org.mockito.Mockito.never;
32 import static org.mockito.Mockito.times;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
36 public class NotificationDispatcherTest {
39 private NotificationChannel channel;
42 private Notification notification;
45 private NotificationDispatcher.Context context;
49 MockitoAnnotations.initMocks(this);
50 when(notification.getType()).thenReturn("event1");
54 public void defaultMethods() {
55 NotificationDispatcher dispatcher = new FakeGenericNotificationDispatcher();
56 assertThat(dispatcher.getKey(), is("FakeGenericNotificationDispatcher"));
57 assertThat(dispatcher.toString(), is("FakeGenericNotificationDispatcher"));
61 public void shouldAlwaysRunDispatchForGenericDispatcher() {
62 NotificationDispatcher dispatcher = new FakeGenericNotificationDispatcher();
63 dispatcher.performDispatch(notification, context);
65 verify(context, times(1)).addUser("user1", channel);
69 public void shouldNotAlwaysRunDispatchForSpecificDispatcher() {
70 NotificationDispatcher dispatcher = new FakeSpecificNotificationDispatcher();
72 // a "event1" notif is sent
73 dispatcher.performDispatch(notification, context);
74 verify(context, never()).addUser("user1", channel);
76 // now, a "specific-event" notif is sent
77 when(notification.getType()).thenReturn("specific-event");
78 dispatcher.performDispatch(notification, context);
79 verify(context, times(1)).addUser("user1", channel);
82 class FakeGenericNotificationDispatcher extends NotificationDispatcher {
84 public void dispatch(Notification notification, Context context) {
85 context.addUser("user1", channel);
89 class FakeSpecificNotificationDispatcher extends NotificationDispatcher {
91 public FakeSpecificNotificationDispatcher() {
92 super("specific-event");
96 public void dispatch(Notification notification, Context context) {
97 context.addUser("user1", channel);