2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.netconf.notifications.impl;
11 import static org.junit.Assert.fail;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.verifyNoMoreInteractions;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.netconf.notifications.BaseNotificationPublisherRegistration;
23 import org.opendaylight.controller.netconf.notifications.NetconfNotification;
24 import org.opendaylight.controller.netconf.notifications.NetconfNotificationCollector;
25 import org.opendaylight.controller.netconf.notifications.NetconfNotificationListener;
26 import org.opendaylight.controller.netconf.notifications.NetconfNotificationRegistry;
27 import org.opendaylight.controller.netconf.notifications.NotificationListenerRegistration;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChangeBuilder;
33 public class NetconfNotificationManagerTest {
36 private NetconfNotificationRegistry notificationRegistry;
39 public void setUp() throws Exception {
40 MockitoAnnotations.initMocks(this);
44 public void testNotificationListeners() throws Exception {
45 final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
46 final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration =
47 netconfNotificationManager.registerBaseNotificationPublisher();
49 final NetconfCapabilityChangeBuilder capabilityChangedBuilder = new NetconfCapabilityChangeBuilder();
51 final NetconfNotificationListener listener = mock(NetconfNotificationListener.class);
52 doNothing().when(listener).onNotification(any(StreamNameType.class), any(NetconfNotification.class));
53 final NotificationListenerRegistration notificationListenerRegistration = netconfNotificationManager.registerNotificationListener(NetconfNotificationManager.BASE_NETCONF_STREAM.getName(), listener);
54 final NetconfCapabilityChange notification = capabilityChangedBuilder.build();
55 baseNotificationPublisherRegistration.onCapabilityChanged(notification);
57 verify(listener).onNotification(any(StreamNameType.class), any(NetconfNotification.class));
59 notificationListenerRegistration.close();
61 baseNotificationPublisherRegistration.onCapabilityChanged(notification);
62 verifyNoMoreInteractions(listener);
66 public void testClose() throws Exception {
67 final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
69 final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration = netconfNotificationManager.registerBaseNotificationPublisher();
71 final NetconfNotificationListener listener = mock(NetconfNotificationListener.class);
72 doNothing().when(listener).onNotification(any(StreamNameType.class), any(NetconfNotification.class));
74 netconfNotificationManager.registerNotificationListener(NetconfNotificationManager.BASE_NETCONF_STREAM.getName(), listener);
76 final NetconfNotificationCollector.NetconfNotificationStreamListener streamListener =
77 mock(NetconfNotificationCollector.NetconfNotificationStreamListener.class);
78 doNothing().when(streamListener).onStreamUnregistered(any(StreamNameType.class));
79 doNothing().when(streamListener).onStreamRegistered(any(Stream.class));
80 netconfNotificationManager.registerStreamListener(streamListener);
82 verify(streamListener).onStreamRegistered(NetconfNotificationManager.BASE_NETCONF_STREAM);
84 netconfNotificationManager.close();
86 verify(streamListener).onStreamUnregistered(NetconfNotificationManager.BASE_NETCONF_STREAM.getName());
89 baseNotificationPublisherRegistration.onCapabilityChanged(new NetconfCapabilityChangeBuilder().build());
90 } catch (final IllegalStateException e) {
91 // Exception should be thrown after manager is closed
95 fail("Publishing into a closed manager should fail");
99 public void testStreamListeners() throws Exception {
100 final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
102 final NetconfNotificationCollector.NetconfNotificationStreamListener streamListener = mock(NetconfNotificationCollector.NetconfNotificationStreamListener.class);
103 doNothing().when(streamListener).onStreamRegistered(any(Stream.class));
104 doNothing().when(streamListener).onStreamUnregistered(any(StreamNameType.class));
106 netconfNotificationManager.registerStreamListener(streamListener);
108 final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration =
109 netconfNotificationManager.registerBaseNotificationPublisher();
111 verify(streamListener).onStreamRegistered(NetconfNotificationManager.BASE_NETCONF_STREAM);
114 baseNotificationPublisherRegistration.close();
116 verify(streamListener).onStreamUnregistered(NetconfNotificationManager.BASE_STREAM_NAME);