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
8 package org.opendaylight.netconf.messagebus.eventsources.netconf;
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;
17 import java.util.Collection;
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;
33 @RunWith(MockitoJUnitRunner.StrictStubs.class)
34 public class ConnectionNotificationTopicRegistrationTest {
36 private ConnectionNotificationTopicRegistration registration;
39 private DOMNotificationListener listener;
43 registration = new ConnectionNotificationTopicRegistration("candidate", listener);
47 public void testClose() throws Exception {
48 registration.setActive(true);
50 assertFalse(registration.isActive());
51 checkStatus(listener, EventSourceStatus.Deactive);
55 public void testActivateNotificationSource() throws Exception {
56 registration.activateNotificationSource();
57 checkStatus(listener, EventSourceStatus.Active);
61 public void testDeActivateNotificationSource() throws Exception {
62 registration.deActivateNotificationSource();
63 checkStatus(listener, EventSourceStatus.Inactive);
67 public void testReActivateNotificationSource() throws Exception {
68 registration.reActivateNotificationSource();
69 checkStatus(listener, EventSourceStatus.Active);
73 public void testRegisterAndUnregisterNotificationTopic() throws Exception {
74 final TopicId topic1 = registerTopic("topic1");
75 final TopicId topic2 = registerTopic("topic2");
76 final TopicId topic3 = registerTopic("topic3");
77 final Set<TopicId> notificationTopicIds = registration.getTopicsForNotification(
78 ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH.asSchemaPath());
79 assertNotNull(notificationTopicIds);
80 assertThat(notificationTopicIds, hasItems(topic1, topic2, topic3));
82 registration.unRegisterNotificationTopic(topic3);
83 final Set<TopicId> afterUnregister = registration.getTopicsForNotification(
84 ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH.asSchemaPath());
85 assertNotNull(afterUnregister);
86 assertThat(afterUnregister, hasItems(topic1, topic2));
87 assertFalse(afterUnregister.contains(topic3));
90 private TopicId registerTopic(final String value) {
91 final TopicId topic = TopicId.getDefaultInstance(value);
92 registration.registerNotificationTopic(
93 ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH.asSchemaPath(), topic);
99 * Checks status node of notification received by listener.
101 * @param listener listener
102 * @param status expected value
104 private static void checkStatus(final DOMNotificationListener listener, final EventSourceStatus status) {
105 ArgumentCaptor<DOMNotification> notificationCaptor = ArgumentCaptor.forClass(DOMNotification.class);
106 verify(listener).onNotification(notificationCaptor.capture());
107 final DOMNotification value = notificationCaptor.getValue();
108 assertEquals(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH, value.getType());
109 final Collection<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> body =
110 value.getBody().getValue();
111 assertEquals(1, body.size());
112 final DOMSource source = (DOMSource) body.iterator().next().getValue();
113 final String statusNodeValue = source.getNode().getFirstChild().getFirstChild().getNodeValue();
114 assertEquals(status.toString(), statusNodeValue);