Remove opendaylight directory
[netconf.git] / netconf / netconf-notifications-impl / src / test / java / org / opendaylight / 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.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 com.google.common.collect.Lists;
19 import java.text.ParseException;
20 import java.text.SimpleDateFormat;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.netconf.notifications.BaseNotificationPublisherRegistration;
26 import org.opendaylight.netconf.notifications.NetconfNotification;
27 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
28 import org.opendaylight.netconf.notifications.NetconfNotificationListener;
29 import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
30 import org.opendaylight.netconf.notifications.NotificationListenerRegistration;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChangeBuilder;
35
36 public class NetconfNotificationManagerTest {
37
38     @Mock
39     private NetconfNotificationRegistry notificationRegistry;
40
41     @Before
42     public void setUp() throws Exception {
43         MockitoAnnotations.initMocks(this);
44     }
45
46
47     @Test public void testEventTime() throws Exception {
48         final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
49             NetconfNotification.RFC3339_DATE_FORMAT_BLUEPRINT);
50         final SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat(
51             NetconfNotification.RFC3339_DATE_FORMAT_WITH_MILLIS_BLUEPRINT);
52
53         for (String time : Lists.newArrayList(
54             "2001-07-04T12:08:56.235-07:00",
55             "2015-10-23T09:42:27.67175+00:00",
56             "1970-01-01T17:17:22.229568+00:00",
57             "1937-01-01T12:00:27.87+00:20",
58             "1990-12-31T15:59:60-08:00",
59             "1990-12-31T23:59:60Z",
60             "1996-12-19T16:39:57-08:00"
61 //          ,"1985-04-12T23:20:50.52Z"
62         )) {
63             try {
64                 simpleDateFormat.parse(time);
65             } catch (ParseException e) {
66                 simpleDateFormat2.parse(time);
67             }
68         }
69     }
70
71     @Test
72     public void testNotificationListeners() throws Exception {
73         final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
74         final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration =
75                 netconfNotificationManager.registerBaseNotificationPublisher();
76
77         final NetconfCapabilityChangeBuilder capabilityChangedBuilder = new NetconfCapabilityChangeBuilder();
78
79         final NetconfNotificationListener listener = mock(NetconfNotificationListener.class);
80         doNothing().when(listener).onNotification(any(StreamNameType.class), any(NetconfNotification.class));
81         final NotificationListenerRegistration notificationListenerRegistration = netconfNotificationManager.registerNotificationListener(NetconfNotificationManager.BASE_NETCONF_STREAM.getName(), listener);
82         final NetconfCapabilityChange notification = capabilityChangedBuilder.build();
83         baseNotificationPublisherRegistration.onCapabilityChanged(notification);
84
85         verify(listener).onNotification(any(StreamNameType.class), any(NetconfNotification.class));
86
87         notificationListenerRegistration.close();
88
89         baseNotificationPublisherRegistration.onCapabilityChanged(notification);
90         verifyNoMoreInteractions(listener);
91     }
92
93     @Test
94     public void testClose() throws Exception {
95         final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
96
97         final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration = netconfNotificationManager.registerBaseNotificationPublisher();
98
99         final NetconfNotificationListener listener = mock(NetconfNotificationListener.class);
100         doNothing().when(listener).onNotification(any(StreamNameType.class), any(NetconfNotification.class));
101
102         netconfNotificationManager.registerNotificationListener(NetconfNotificationManager.BASE_NETCONF_STREAM.getName(), listener);
103
104         final NetconfNotificationCollector.NetconfNotificationStreamListener streamListener =
105                 mock(NetconfNotificationCollector.NetconfNotificationStreamListener.class);
106         doNothing().when(streamListener).onStreamUnregistered(any(StreamNameType.class));
107         doNothing().when(streamListener).onStreamRegistered(any(Stream.class));
108         netconfNotificationManager.registerStreamListener(streamListener);
109
110         verify(streamListener).onStreamRegistered(NetconfNotificationManager.BASE_NETCONF_STREAM);
111
112         netconfNotificationManager.close();
113
114         verify(streamListener).onStreamUnregistered(NetconfNotificationManager.BASE_NETCONF_STREAM.getName());
115
116         try {
117             baseNotificationPublisherRegistration.onCapabilityChanged(new NetconfCapabilityChangeBuilder().build());
118         } catch (final IllegalStateException e) {
119             // Exception should be thrown after manager is closed
120             return;
121         }
122
123         fail("Publishing into a closed manager should fail");
124     }
125
126     @Test
127     public void testStreamListeners() throws Exception {
128         final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
129
130         final NetconfNotificationCollector.NetconfNotificationStreamListener streamListener = mock(NetconfNotificationCollector.NetconfNotificationStreamListener.class);
131         doNothing().when(streamListener).onStreamRegistered(any(Stream.class));
132         doNothing().when(streamListener).onStreamUnregistered(any(StreamNameType.class));
133
134         netconfNotificationManager.registerStreamListener(streamListener);
135
136         final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration =
137                 netconfNotificationManager.registerBaseNotificationPublisher();
138
139         verify(streamListener).onStreamRegistered(NetconfNotificationManager.BASE_NETCONF_STREAM);
140
141
142         baseNotificationPublisherRegistration.close();
143
144         verify(streamListener).onStreamUnregistered(NetconfNotificationManager.BASE_STREAM_NAME);
145     }
146 }