Merge "Bug 2669: Use slf4j Logger instead of akka LoggingAdapter"
[controller.git] / opendaylight / netconf / netconf-notifications-impl / src / test / java / org / opendaylight / controller / netconf / notifications / impl / NetconfNotificationManagerTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.notifications.impl;
10
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;
17
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;
32
33 public class NetconfNotificationManagerTest {
34
35     @Mock
36     private NetconfNotificationRegistry notificationRegistry;
37
38     @Before
39     public void setUp() throws Exception {
40         MockitoAnnotations.initMocks(this);
41     }
42
43     @Test
44     public void testNotificationListeners() throws Exception {
45         final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
46         final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration =
47                 netconfNotificationManager.registerBaseNotificationPublisher();
48
49         final NetconfCapabilityChangeBuilder capabilityChangedBuilder = new NetconfCapabilityChangeBuilder();
50
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);
56
57         verify(listener).onNotification(any(StreamNameType.class), any(NetconfNotification.class));
58
59         notificationListenerRegistration.close();
60
61         baseNotificationPublisherRegistration.onCapabilityChanged(notification);
62         verifyNoMoreInteractions(listener);
63     }
64
65     @Test
66     public void testClose() throws Exception {
67         final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
68
69         final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration = netconfNotificationManager.registerBaseNotificationPublisher();
70
71         final NetconfNotificationListener listener = mock(NetconfNotificationListener.class);
72         doNothing().when(listener).onNotification(any(StreamNameType.class), any(NetconfNotification.class));
73
74         netconfNotificationManager.registerNotificationListener(NetconfNotificationManager.BASE_NETCONF_STREAM.getName(), listener);
75
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);
81
82         verify(streamListener).onStreamRegistered(NetconfNotificationManager.BASE_NETCONF_STREAM);
83
84         netconfNotificationManager.close();
85
86         verify(streamListener).onStreamUnregistered(NetconfNotificationManager.BASE_NETCONF_STREAM.getName());
87
88         try {
89             baseNotificationPublisherRegistration.onCapabilityChanged(new NetconfCapabilityChangeBuilder().build());
90         } catch (final IllegalStateException e) {
91             // Exception should be thrown after manager is closed
92             return;
93         }
94
95         fail("Publishing into a closed manager should fail");
96     }
97
98     @Test
99     public void testStreamListeners() throws Exception {
100         final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
101
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));
105
106         netconfNotificationManager.registerStreamListener(streamListener);
107
108         final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration =
109                 netconfNotificationManager.registerBaseNotificationPublisher();
110
111         verify(streamListener).onStreamRegistered(NetconfNotificationManager.BASE_NETCONF_STREAM);
112
113
114         baseNotificationPublisherRegistration.close();
115
116         verify(streamListener).onStreamUnregistered(NetconfNotificationManager.BASE_STREAM_NAME);
117     }
118 }