2 * Copyright (c) 2016 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.messagebus.eventsources.netconf;
11 import static org.hamcrest.CoreMatchers.hasItems;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
16 import com.google.common.base.Optional;
17 import com.google.common.util.concurrent.Futures;
18 import java.util.Date;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
26 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
27 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
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.netmod.notification.rev080714.netconf.streams.StreamBuilder;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
34 import org.opendaylight.yangtools.concepts.ListenerRegistration;
36 public class StreamNotificationTopicRegistrationTest {
38 private static final String STREAM_NAME = "stream-1";
39 private static final String PREFIX = ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH
40 .getLastComponent().getNamespace().toString();
43 private NetconfEventSource source;
45 private NetconfEventSourceMount mount;
47 private DOMNotificationService reference;
49 private ListenerRegistration<DOMNotificationListener> listenerRegistration;
51 private StreamNotificationTopicRegistration registration;
52 private Stream stream;
55 public void setUp() throws Exception {
56 MockitoAnnotations.initMocks(this);
58 Node node = new NodeBuilder().setNodeId(NodeId.getDefaultInstance("node-id")).build();
59 when(mount.getNode()).thenReturn(node);
60 when(mount.registerNotificationListener(source, ConnectionNotificationTopicRegistration
61 .EVENT_SOURCE_STATUS_PATH))
62 .thenReturn(listenerRegistration);
63 when(mount.invokeCreateSubscription(any(), any())).thenReturn(Futures.immediateCheckedFuture(null));
64 when(mount.invokeCreateSubscription(any())).thenReturn(Futures.immediateCheckedFuture(null));
66 when(source.getMount()).thenReturn(mount);
67 stream = new StreamBuilder().setName(StreamNameType.getDefaultInstance(STREAM_NAME)).setReplaySupport(true)
70 registration = new StreamNotificationTopicRegistration(stream, PREFIX, source);
74 public void testActivateNotificationSource() throws Exception {
75 registration.activateNotificationSource();
76 Assert.assertTrue(registration.isActive());
77 verify(mount).invokeCreateSubscription(stream);
82 public void testReActivateNotificationSource() throws Exception {
83 registration.setActive(true);
84 registration.reActivateNotificationSource();
86 Assert.assertTrue(registration.isActive());
87 verify(mount).invokeCreateSubscription(stream, Optional.absent());
91 public void testReActivateNotificationSourceWithReplay() throws Exception {
92 final Date lastEventTime = new Date();
93 registration.setActive(true);
94 registration.setLastEventTime(lastEventTime);
95 registration.reActivateNotificationSource();
97 Assert.assertTrue(registration.isActive());
98 verify(mount).invokeCreateSubscription(stream, Optional.of(lastEventTime));
102 public void testClose() throws Exception {
103 registration.setActive(true);
104 registration.close();
105 Assert.assertFalse(registration.isActive());
109 public void testRegisterAndUnregisterNotificationTopic() throws Exception {
110 final TopicId topic1 = registerTopic("topic1");
111 final TopicId topic2 = registerTopic("topic2");
112 final TopicId topic3 = registerTopic("topic3");
113 final Set<TopicId> notificationTopicIds =
114 registration.getTopicsForNotification(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH);
115 Assert.assertNotNull(notificationTopicIds);
116 Assert.assertThat(notificationTopicIds, hasItems(topic1, topic2, topic3));
118 registration.unRegisterNotificationTopic(topic3);
119 final Set<TopicId> afterUnregister =
120 registration.getTopicsForNotification(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH);
121 Assert.assertNotNull(afterUnregister);
122 Assert.assertThat(afterUnregister, hasItems(topic1, topic2));
123 Assert.assertFalse(afterUnregister.contains(topic3));
126 private TopicId registerTopic(String value) {
127 final TopicId topic = TopicId.getDefaultInstance(value);
128 registration.registerNotificationTopic(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH, topic);