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