Deprecate messagebus-netconf
[netconf.git] / netconf / messagebus-netconf / src / test / java / org / opendaylight / netconf / messagebus / eventsources / netconf / ConnectionNotificationTopicRegistrationTest.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.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.mockito.Mockito.verify;
16
17 import java.util.Collection;
18 import java.util.Set;
19 import javax.xml.transform.dom.DOMSource;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Mock;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.opendaylight.mdsal.dom.api.DOMNotification;
27 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
28 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
29 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatus;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
32
33 @Deprecated(forRemoval = true)
34 @RunWith(MockitoJUnitRunner.StrictStubs.class)
35 public class ConnectionNotificationTopicRegistrationTest {
36
37     private ConnectionNotificationTopicRegistration registration;
38
39     @Mock
40     private DOMNotificationListener listener;
41
42     @Before
43     public void setUp() {
44         registration = new ConnectionNotificationTopicRegistration("candidate", listener);
45     }
46
47     @Test
48     public void testClose() throws Exception {
49         registration.setActive(true);
50         registration.close();
51         assertFalse(registration.isActive());
52         checkStatus(listener, EventSourceStatus.Deactive);
53     }
54
55     @Test
56     public void testActivateNotificationSource() throws Exception {
57         registration.activateNotificationSource();
58         checkStatus(listener, EventSourceStatus.Active);
59     }
60
61     @Test
62     public void testDeActivateNotificationSource() throws Exception {
63         registration.deActivateNotificationSource();
64         checkStatus(listener, EventSourceStatus.Inactive);
65     }
66
67     @Test
68     public void testReActivateNotificationSource() throws Exception {
69         registration.reActivateNotificationSource();
70         checkStatus(listener, EventSourceStatus.Active);
71     }
72
73     @Test
74     public void testRegisterAndUnregisterNotificationTopic() throws Exception {
75         final TopicId topic1 = registerTopic("topic1");
76         final TopicId topic2 = registerTopic("topic2");
77         final TopicId topic3 = registerTopic("topic3");
78         final Set<TopicId> notificationTopicIds = registration.getTopicsForNotification(
79             ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH.asSchemaPath());
80         assertNotNull(notificationTopicIds);
81         assertThat(notificationTopicIds, hasItems(topic1, topic2, topic3));
82
83         registration.unRegisterNotificationTopic(topic3);
84         final Set<TopicId> afterUnregister = registration.getTopicsForNotification(
85             ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH.asSchemaPath());
86         assertNotNull(afterUnregister);
87         assertThat(afterUnregister, hasItems(topic1, topic2));
88         assertFalse(afterUnregister.contains(topic3));
89     }
90
91     private TopicId registerTopic(final String value) {
92         final TopicId topic = TopicId.getDefaultInstance(value);
93         registration.registerNotificationTopic(
94             ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH.asSchemaPath(), topic);
95         return topic;
96     }
97
98
99     /**
100      * Checks status node of notification received by listener.
101      *
102      * @param listener listener
103      * @param status   expected value
104      */
105     private static void checkStatus(final DOMNotificationListener listener, final EventSourceStatus status) {
106         ArgumentCaptor<DOMNotification> notificationCaptor = ArgumentCaptor.forClass(DOMNotification.class);
107         verify(listener).onNotification(notificationCaptor.capture());
108         final DOMNotification value = notificationCaptor.getValue();
109         assertEquals(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH, value.getType());
110         final Collection<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> body =
111             value.getBody().getValue();
112         assertEquals(1, body.size());
113         final DOMSource source = (DOMSource) body.iterator().next().getValue();
114         final String statusNodeValue = source.getNode().getFirstChild().getFirstChild().getNodeValue();
115         assertEquals(status.toString(), statusNodeValue);
116     }
117 }