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.assertEquals;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.verifyNoMoreInteractions;
19 import com.google.common.collect.Lists;
20 import java.text.SimpleDateFormat;
21 import java.time.Instant;
22 import java.time.format.DateTimeParseException;
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.Iterator;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.opendaylight.netconf.notifications.BaseNotificationPublisherRegistration;
32 import org.opendaylight.netconf.notifications.NetconfNotification;
33 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
34 import org.opendaylight.netconf.notifications.NetconfNotificationListener;
35 import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
36 import org.opendaylight.netconf.notifications.NotificationListenerRegistration;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChangeBuilder;
42 public class NetconfNotificationManagerTest {
44 public static final String RFC3339_DATE_FORMAT_WITH_MILLIS_BLUEPRINT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
46 private NetconfNotificationRegistry notificationRegistry;
49 public void setUp() throws Exception {
50 MockitoAnnotations.initMocks(this);
54 public void testEventTime() throws Exception {
55 //Testing values with SimpleDateFormat
56 final ArrayList<String> checkWith = Lists.newArrayList(
57 "2001-07-04T12:08:56.235-07:00",
58 "2015-10-23T09:42:27.671+00:00",
59 "1970-01-01T17:17:22.229+00:00",
60 "1937-01-01T12:00:27.870+00:20",
61 "2015-06-30T23:59:59.000+00:00",
62 "1996-12-19T16:39:57.000-08:00",
63 "2015-10-23T09:42:27.000+00:00",
64 "2015-10-23T09:42:27.200+00:00",
65 "1985-04-12T23:20:50.520+00:00",
66 // Values with leap second
67 "2001-07-04T23:59:59.235-07:00",
68 "1990-12-31T23:59:59.000-08:00",
69 "2015-10-23T23:59:59.671+00:00",
70 "1970-01-01T23:59:59.229+00:00",
71 "1937-01-01T23:59:59.870+00:20",
72 "1990-12-31T23:59:59.000+00:00",
73 "2015-10-23T23:59:59.200+00:00",
74 "1985-04-12T23:59:59.520+00:00");
75 final Iterator<String> iterator = checkWith.iterator();
77 // Testing correct values
78 for (final String time : Lists.newArrayList(
79 "2001-07-04T12:08:56.235-07:00",
80 "2015-10-23T09:42:27.67175+00:00",
81 "1970-01-01T17:17:22.229568+00:00",
82 "1937-01-01T12:00:27.87+00:20",
83 "2015-06-30T23:59:59Z",
84 "1996-12-19T16:39:57-08:00",
85 "2015-10-23T09:42:27Z",
86 "2015-10-23T09:42:27.200001Z",
87 "1985-04-12T23:20:50.52Z",
88 // Values with leap second
89 "2001-07-04T23:59:60.235-07:00",
90 "1990-12-31T23:59:60-08:00",
91 "2015-10-23T23:59:60.67175+00:00",
92 "1970-01-01T23:59:60.229568+00:00",
93 "1937-01-01T23:59:60.87+00:20",
94 "1990-12-31T23:59:60Z",
95 "2015-10-23T23:59:60.20001Z",
96 "1985-04-12T23:59:60.52Z"
99 final Date apply = NetconfNotification.RFC3339_DATE_PARSER.apply(time);
101 new SimpleDateFormat(RFC3339_DATE_FORMAT_WITH_MILLIS_BLUEPRINT).parse(iterator.next());
102 assertEquals(parse.getTime(), apply.getTime());
103 // Testing that we're consistent from formatting to parsing.
104 final String dateString = NetconfNotification.RFC3339_DATE_FORMATTER.apply(apply);
105 final Date date1 = NetconfNotification.RFC3339_DATE_PARSER.apply(dateString);
106 final String dateString1 = NetconfNotification.RFC3339_DATE_FORMATTER.apply(date1);
107 Assert.assertEquals(apply, date1);
108 Assert.assertEquals(dateString, dateString1);
109 } catch (final DateTimeParseException e) {
110 fail("Failed to parse time value = " + time + " " + e);
115 // Testing that we're consistent from formatting to parsing.
116 final Date date0 = Date.from(Instant.ofEpochMilli(0));
117 final String dateString0 = NetconfNotification.RFC3339_DATE_FORMATTER.apply(date0);
118 final Date date1 = NetconfNotification.RFC3339_DATE_PARSER.apply(dateString0);
119 final String dateString1 = NetconfNotification.RFC3339_DATE_FORMATTER.apply(date1);
120 Assert.assertEquals(date0, date1);
121 Assert.assertEquals(dateString0, dateString1);
123 // Testing wrong values
124 for (final String time : Lists.newArrayList(
126 "205-10-23T09:42:27.67175+00:00",
127 "1970-01-01T17:60:22.229568+00:00",
128 "1937-01-01T32:00:27.87+00:20",
129 "2060-13-31T15:59:90-08:00",
130 "1990-12-31T23:58:60Z"
133 NetconfNotification.RFC3339_DATE_PARSER.apply(time);
134 } catch (final DateTimeParseException e) {
137 fail("Should have thrown an exception; value= " + time);
142 public void testNotificationListeners() throws Exception {
143 final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
144 final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration =
145 netconfNotificationManager.registerBaseNotificationPublisher();
147 final NetconfCapabilityChangeBuilder capabilityChangedBuilder = new NetconfCapabilityChangeBuilder();
149 final NetconfNotificationListener listener = mock(NetconfNotificationListener.class);
150 doNothing().when(listener).onNotification(any(StreamNameType.class), any(NetconfNotification.class));
151 final NotificationListenerRegistration notificationListenerRegistration = netconfNotificationManager
152 .registerNotificationListener(NetconfNotificationManager.BASE_NETCONF_STREAM.getName(), listener);
153 final NetconfCapabilityChange notification = capabilityChangedBuilder.build();
154 baseNotificationPublisherRegistration.onCapabilityChanged(notification);
156 verify(listener).onNotification(any(StreamNameType.class), any(NetconfNotification.class));
158 notificationListenerRegistration.close();
160 baseNotificationPublisherRegistration.onCapabilityChanged(notification);
161 verifyNoMoreInteractions(listener);
165 public void testClose() throws Exception {
166 final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
168 final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration =
169 netconfNotificationManager.registerBaseNotificationPublisher();
171 final NetconfNotificationListener listener = mock(NetconfNotificationListener.class);
172 doNothing().when(listener).onNotification(any(StreamNameType.class), any(NetconfNotification.class));
174 netconfNotificationManager
175 .registerNotificationListener(NetconfNotificationManager.BASE_NETCONF_STREAM.getName(), listener);
177 final NetconfNotificationCollector.NetconfNotificationStreamListener streamListener =
178 mock(NetconfNotificationCollector.NetconfNotificationStreamListener.class);
179 doNothing().when(streamListener).onStreamUnregistered(any(StreamNameType.class));
180 doNothing().when(streamListener).onStreamRegistered(any(Stream.class));
181 netconfNotificationManager.registerStreamListener(streamListener);
183 verify(streamListener).onStreamRegistered(NetconfNotificationManager.BASE_NETCONF_STREAM);
185 netconfNotificationManager.close();
187 verify(streamListener).onStreamUnregistered(NetconfNotificationManager.BASE_NETCONF_STREAM.getName());
190 baseNotificationPublisherRegistration.onCapabilityChanged(new NetconfCapabilityChangeBuilder().build());
191 } catch (final IllegalStateException e) {
192 // Exception should be thrown after manager is closed
196 fail("Publishing into a closed manager should fail");
200 public void testStreamListeners() throws Exception {
201 final NetconfNotificationManager netconfNotificationManager = new NetconfNotificationManager();
203 final NetconfNotificationCollector.NetconfNotificationStreamListener streamListener =
204 mock(NetconfNotificationCollector.NetconfNotificationStreamListener.class);
205 doNothing().when(streamListener).onStreamRegistered(any(Stream.class));
206 doNothing().when(streamListener).onStreamUnregistered(any(StreamNameType.class));
208 netconfNotificationManager.registerStreamListener(streamListener);
210 final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration =
211 netconfNotificationManager.registerBaseNotificationPublisher();
213 verify(streamListener).onStreamRegistered(NetconfNotificationManager.BASE_NETCONF_STREAM);
216 baseNotificationPublisherRegistration.close();
218 verify(streamListener).onStreamUnregistered(NetconfNotificationManager.BASE_STREAM_NAME);