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.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 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;
36 public class NetconfNotificationManagerTest {
39 private NetconfNotificationRegistry notificationRegistry;
42 public void setUp() throws Exception {
43 MockitoAnnotations.initMocks(this);
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);
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"
64 simpleDateFormat.parse(time);
65 } catch (ParseException e) {
66 simpleDateFormat2.parse(time);
72 public void testNotificationListeners() throws Exception {
73 final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
74 final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration =
75 netconfNotificationManager.registerBaseNotificationPublisher();
77 final NetconfCapabilityChangeBuilder capabilityChangedBuilder = new NetconfCapabilityChangeBuilder();
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);
85 verify(listener).onNotification(any(StreamNameType.class), any(NetconfNotification.class));
87 notificationListenerRegistration.close();
89 baseNotificationPublisherRegistration.onCapabilityChanged(notification);
90 verifyNoMoreInteractions(listener);
94 public void testClose() throws Exception {
95 final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
97 final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration = netconfNotificationManager.registerBaseNotificationPublisher();
99 final NetconfNotificationListener listener = mock(NetconfNotificationListener.class);
100 doNothing().when(listener).onNotification(any(StreamNameType.class), any(NetconfNotification.class));
102 netconfNotificationManager.registerNotificationListener(NetconfNotificationManager.BASE_NETCONF_STREAM.getName(), listener);
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);
110 verify(streamListener).onStreamRegistered(NetconfNotificationManager.BASE_NETCONF_STREAM);
112 netconfNotificationManager.close();
114 verify(streamListener).onStreamUnregistered(NetconfNotificationManager.BASE_NETCONF_STREAM.getName());
117 baseNotificationPublisherRegistration.onCapabilityChanged(new NetconfCapabilityChangeBuilder().build());
118 } catch (final IllegalStateException e) {
119 // Exception should be thrown after manager is closed
123 fail("Publishing into a closed manager should fail");
127 public void testStreamListeners() throws Exception {
128 final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
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));
134 netconfNotificationManager.registerStreamListener(streamListener);
136 final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration =
137 netconfNotificationManager.registerBaseNotificationPublisher();
139 verify(streamListener).onStreamRegistered(NetconfNotificationManager.BASE_NETCONF_STREAM);
142 baseNotificationPublisherRegistration.close();
144 verify(streamListener).onStreamUnregistered(NetconfNotificationManager.BASE_STREAM_NAME);