Reduce the use of AttrBuilders
[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.mdsal.dom.api.DOMNotification;
24 import org.opendaylight.mdsal.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 =
75                 registration.getTopicsForNotification(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH);
76         Assert.assertNotNull(notificationTopicIds);
77         Assert.assertThat(notificationTopicIds, hasItems(topic1, topic2, topic3));
78
79         registration.unRegisterNotificationTopic(topic3);
80         final Set<TopicId> afterUnregister =
81                 registration.getTopicsForNotification(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH);
82         Assert.assertNotNull(afterUnregister);
83         Assert.assertThat(afterUnregister, hasItems(topic1, topic2));
84         Assert.assertFalse(afterUnregister.contains(topic3));
85     }
86
87     private TopicId registerTopic(final String value) {
88         final TopicId topic = TopicId.getDefaultInstance(value);
89         registration.registerNotificationTopic(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH, topic);
90         return topic;
91     }
92
93
94     /**
95      * Checks status node of notification received by listener.
96      *
97      * @param listener listener
98      * @param status   expected value
99      */
100     private static void checkStatus(final DOMNotificationListener listener, final EventSourceStatus status) {
101         ArgumentCaptor<DOMNotification> notificationCaptor = ArgumentCaptor.forClass(DOMNotification.class);
102         verify(listener).onNotification(notificationCaptor.capture());
103         final DOMNotification value = notificationCaptor.getValue();
104         Assert.assertEquals(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH, value.getType());
105         final Collection<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> body = value.getBody()
106                 .getValue();
107         Assert.assertEquals(1, body.size());
108         final DOMSource source = (DOMSource) body.iterator().next().getValue();
109         final String statusNodeValue = source.getNode().getFirstChild().getFirstChild().getNodeValue();
110         Assert.assertEquals(status.toString(), statusNodeValue);
111     }
112 }