2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.messagebus.eventsources.netconf;
11 import static org.hamcrest.CoreMatchers.hasItems;
12 import static org.mockito.Mockito.verify;
14 import java.util.ArrayList;
15 import java.util.Collection;
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;
30 public class ConnectionNotificationTopicRegistrationTest {
32 private ConnectionNotificationTopicRegistration registration;
35 private DOMNotificationListener listener;
38 public void setUp() throws Exception {
39 MockitoAnnotations.initMocks(this);
40 registration = new ConnectionNotificationTopicRegistration("candidate", listener);
44 public void testClose() throws Exception {
45 registration.setActive(true);
47 Assert.assertFalse(registration.isActive());
48 checkStatus(listener, EventSourceStatus.Deactive);
52 public void testActivateNotificationSource() throws Exception {
53 registration.activateNotificationSource();
54 checkStatus(listener, EventSourceStatus.Active);
58 public void testDeActivateNotificationSource() throws Exception {
59 registration.deActivateNotificationSource();
60 checkStatus(listener, EventSourceStatus.Inactive);
64 public void testReActivateNotificationSource() throws Exception {
65 registration.reActivateNotificationSource();
66 checkStatus(listener, EventSourceStatus.Active);
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 ArrayList<TopicId> notificationTopicIds = registration.getNotificationTopicIds(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH);
75 Assert.assertNotNull(notificationTopicIds);
76 Assert.assertThat(notificationTopicIds, hasItems(topic1, topic2, topic3));
78 registration.unRegisterNotificationTopic(topic3);
79 final ArrayList<TopicId> afterUnregister = registration.getNotificationTopicIds(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH);
80 Assert.assertNotNull(afterUnregister);
81 Assert.assertThat(afterUnregister, hasItems(topic1, topic2));
82 Assert.assertFalse(afterUnregister.contains(topic3));
85 private TopicId registerTopic(String value) {
86 final TopicId topic = TopicId.getDefaultInstance(value);
87 registration.registerNotificationTopic(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH, topic);
93 * Checks status node of notification received by listener.
94 * @param listener listener
95 * @param status expected value
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);