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