Bump versions to 2.0.0-SNAPSHOT
[netconf.git] / netconf / messagebus-netconf / src / test / java / org / opendaylight / netconf / messagebus / eventsources / netconf / StreamNotificationTopicRegistrationTest.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.netconf.messagebus.eventsources.netconf;
9
10 import static org.hamcrest.CoreMatchers.hasItems;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateNullFluentFuture;
20
21 import java.time.Instant;
22 import java.util.Optional;
23 import java.util.Set;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
30 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
31 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
38 import org.opendaylight.yangtools.concepts.ListenerRegistration;
39
40 @Deprecated(forRemoval = true)
41 @RunWith(MockitoJUnitRunner.StrictStubs.class)
42 public class StreamNotificationTopicRegistrationTest {
43
44     private static final String STREAM_NAME = "stream-1";
45     private static final String PREFIX = ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH
46             .lastNodeIdentifier().getNamespace().toString();
47
48     @Mock
49     private NetconfEventSource source;
50     @Mock
51     private NetconfEventSourceMount mount;
52     @Mock
53     private DOMNotificationService reference;
54     @Mock
55     private ListenerRegistration<DOMNotificationListener> listenerRegistration;
56
57     private StreamNotificationTopicRegistration registration;
58     private Stream stream;
59
60     @Before
61     public void setUp() throws Exception {
62         Node node = new NodeBuilder().setNodeId(NodeId.getDefaultInstance("node-id")).build();
63         when(mount.getNode()).thenReturn(node);
64         when(mount.registerNotificationListener(source,
65                 ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH.asSchemaPath()))
66                 .thenReturn(listenerRegistration);
67         doReturn(immediateNullFluentFuture()).when(mount).invokeCreateSubscription(any(), any());
68         doReturn(immediateNullFluentFuture()).when(mount).invokeCreateSubscription(any());
69
70         when(source.getMount()).thenReturn(mount);
71         stream = new StreamBuilder().setName(StreamNameType.getDefaultInstance(STREAM_NAME)).setReplaySupport(true)
72                 .build();
73
74         registration = new StreamNotificationTopicRegistration(stream, PREFIX, source);
75     }
76
77     @Test
78     public void testActivateNotificationSource() throws Exception {
79         registration.activateNotificationSource();
80         assertTrue(registration.isActive());
81         verify(mount).invokeCreateSubscription(stream);
82
83     }
84
85     @Test
86     public void testReActivateNotificationSource() throws Exception {
87         registration.setActive(true);
88         registration.reActivateNotificationSource();
89
90         assertTrue(registration.isActive());
91         verify(mount).invokeCreateSubscription(stream, Optional.empty());
92     }
93
94     @Test
95     public void testReActivateNotificationSourceWithReplay() throws Exception {
96         final Instant lastEventTime = Instant.now();
97         registration.setActive(true);
98         registration.setLastEventTime(lastEventTime);
99         registration.reActivateNotificationSource();
100
101         assertTrue(registration.isActive());
102         verify(mount).invokeCreateSubscription(stream, Optional.of(lastEventTime));
103     }
104
105     @Test
106     public void testClose() throws Exception {
107         registration.setActive(true);
108         registration.close();
109         assertFalse(registration.isActive());
110     }
111
112     @Test
113     public void testRegisterAndUnregisterNotificationTopic() throws Exception {
114         final TopicId topic1 = registerTopic("topic1");
115         final TopicId topic2 = registerTopic("topic2");
116         final TopicId topic3 = registerTopic("topic3");
117         final Set<TopicId> notificationTopicIds = registration.getTopicsForNotification(
118             ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH.asSchemaPath());
119         assertNotNull(notificationTopicIds);
120         assertThat(notificationTopicIds, hasItems(topic1, topic2, topic3));
121
122         registration.unRegisterNotificationTopic(topic3);
123         final Set<TopicId> afterUnregister = registration.getTopicsForNotification(
124             ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH.asSchemaPath());
125         assertNotNull(afterUnregister);
126         assertThat(afterUnregister, hasItems(topic1, topic2));
127         assertFalse(afterUnregister.contains(topic3));
128     }
129
130     private TopicId registerTopic(final String value) {
131         final TopicId topic = TopicId.getDefaultInstance(value);
132         registration.registerNotificationTopic(
133             ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH.asSchemaPath(), topic);
134         return topic;
135     }
136
137
138 }