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.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 Set<TopicId> notificationTopicIds =
75 registration.getTopicsForNotification(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH);
76 Assert.assertNotNull(notificationTopicIds);
77 Assert.assertThat(notificationTopicIds, hasItems(topic1, topic2, topic3));
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));
87 private TopicId registerTopic(String value) {
88 final TopicId topic = TopicId.getDefaultInstance(value);
89 registration.registerNotificationTopic(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH, topic);
95 * Checks status node of notification received by listener.
97 * @param listener listener
98 * @param status expected value
100 private static void checkStatus(DOMNotificationListener listener, 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()
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);