Merge "BUG-2953: do not use a complete InMemoryDataStore"
[controller.git] / opendaylight / md-sal / messagebus-impl / src / test / java / org / opendaylight / controller / messagebus / app / impl / NetconfEventSourceTest.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.controller.messagebus.app.impl;
9
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.controller.md.sal.binding.api.BindingService;
15 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
18 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
19 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
21 import org.opendaylight.controller.md.sal.dom.api.DOMService;
22 import org.opendaylight.controller.messagebus.eventsources.netconf.NetconfEventSource;
23 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
24 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
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.JoinTopicInput;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.inventory.rev140108.NetconfNode;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
41
42 import java.lang.reflect.Field;
43 import java.net.URI;
44 import java.util.HashMap;
45 import java.util.HashSet;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Set;
49
50 import static org.junit.Assert.assertEquals;
51 import static org.junit.Assert.assertNotNull;
52 import static org.mockito.Matchers.any;
53 import static org.mockito.Mockito.doReturn;
54 import static org.mockito.Mockito.mock;
55 import static org.mockito.Mockito.times;
56 import static org.mockito.Mockito.verify;
57
58 public class NetconfEventSourceTest {
59
60     NetconfEventSource netconfEventSource;
61     DOMMountPoint domMountPointMock;
62     JoinTopicInput joinTopicInputMock;
63
64     @Before
65     public void setUp() throws Exception {
66         Map<String, String> streamMap = new HashMap<>();
67         streamMap.put("uriStr1", "string2");
68         domMountPointMock = mock(DOMMountPoint.class);
69         DOMNotificationPublishService domNotificationPublishServiceMock = mock(DOMNotificationPublishService.class);
70         MountPoint mountPointMock = mock(MountPoint.class);
71
72         RpcConsumerRegistry rpcConsumerRegistryMock = mock(RpcConsumerRegistry.class);
73         Optional<BindingService> onlyOptionalMock = (Optional<BindingService>) mock(Optional.class);
74         NotificationsService notificationsServiceMock = mock(NotificationsService.class);
75
76         doReturn(onlyOptionalMock).when(mountPointMock).getService(RpcConsumerRegistry.class);
77         doReturn(rpcConsumerRegistryMock).when(onlyOptionalMock).get();
78         doReturn(notificationsServiceMock).when(rpcConsumerRegistryMock).getRpcService(NotificationsService.class);
79         org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node node
80                 = mock(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class);
81         org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId nodeId
82                 = new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId("NodeId1");
83         doReturn(nodeId).when(node).getNodeId();
84         netconfEventSource = new NetconfEventSource(node, streamMap, domMountPointMock, domNotificationPublishServiceMock, mountPointMock);
85     }
86
87     @Test
88     public void onDataChangedTest(){
89         InstanceIdentifier brmIdent = InstanceIdentifier.create(Nodes.class)
90                 .child(Node.class, new NodeKey(new NodeId("brm"))).augmentation(NetconfNode.class);
91         AsyncDataChangeEvent asyncDataChangeEventMock = mock(AsyncDataChangeEvent.class);
92         NetconfNode dataObjectMock = mock(NetconfNode.class);
93         Map<InstanceIdentifier, DataObject> dataChangeMap = new HashMap<>();
94         dataChangeMap.put(brmIdent, dataObjectMock);
95         doReturn(dataChangeMap).when(asyncDataChangeEventMock).getOriginalData();
96         doReturn(dataChangeMap).when(asyncDataChangeEventMock).getUpdatedData();
97         doReturn(true).when(dataObjectMock).isConnected();
98         netconfEventSource.onDataChanged(asyncDataChangeEventMock);
99         verify(dataObjectMock, times(2)).isConnected();
100     }
101
102     @Test
103     public void onDataChangedResubscribeTest() throws Exception{
104
105         InstanceIdentifier brmIdent = InstanceIdentifier.create(Nodes.class)
106                 .child(Node.class, new NodeKey(new NodeId("brm"))).augmentation(NetconfNode.class);
107
108         AsyncDataChangeEvent asyncDataChangeEventMock = mock(AsyncDataChangeEvent.class);
109         NetconfNode dataObjectMock = mock(NetconfNode.class);
110         Map<InstanceIdentifier, DataObject> dataChangeMap = new HashMap<>();
111         dataChangeMap.put(brmIdent, dataObjectMock);
112         doReturn(dataChangeMap).when(asyncDataChangeEventMock).getUpdatedData();
113         doReturn(new HashMap<InstanceIdentifier, DataObject>()).when(asyncDataChangeEventMock).getOriginalData();
114         doReturn(true).when(dataObjectMock).isConnected();
115
116         Set<String> localSet = getActiveStreams();
117         localSet.add("activeStream1");
118
119         Optional<DOMService> optionalMock = (Optional<DOMService>) mock(Optional.class);
120         doReturn(optionalMock).when(domMountPointMock).getService(DOMRpcService.class);
121         DOMRpcService domRpcServiceMock = mock(DOMRpcService.class);
122         doReturn(domRpcServiceMock).when(optionalMock).get();
123         CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
124         doReturn(checkedFutureMock).when(domRpcServiceMock).invokeRpc(any(SchemaPath.class), any(ContainerNode.class));
125
126         netconfEventSource.onDataChanged(asyncDataChangeEventMock);
127         verify(dataObjectMock, times(1)).isConnected();
128         assertEquals("Size of set has not been set correctly.", 1, getActiveStreams().size());
129     }
130
131     @Test
132     public void joinTopicTest() throws Exception{
133         joinTopicTestHelper();
134         assertNotNull("JoinTopic return value has not been created correctly.", netconfEventSource.joinTopic(joinTopicInputMock));
135     }
136
137     private void joinTopicTestHelper() throws Exception{
138         joinTopicInputMock = mock(JoinTopicInput.class);
139         TopicId topicId = new TopicId("topicID007");
140         doReturn(topicId).when(joinTopicInputMock).getTopicId();
141         NotificationPattern notificationPatternMock = mock(NotificationPattern.class);
142         doReturn(notificationPatternMock).when(joinTopicInputMock).getNotificationPattern();
143         doReturn("uriStr1").when(notificationPatternMock).getValue();
144
145         SchemaContext schemaContextMock = mock(SchemaContext.class);
146         doReturn(schemaContextMock).when(domMountPointMock).getSchemaContext();
147         Set<NotificationDefinition> notificationDefinitionSet = new HashSet<>();
148         NotificationDefinition notificationDefinitionMock = mock(NotificationDefinition.class);
149         notificationDefinitionSet.add(notificationDefinitionMock);
150
151         URI uri = new URI("uriStr1");
152         QName qName = new QName(uri, "localName1");
153         org.opendaylight.yangtools.yang.model.api.SchemaPath schemaPath = SchemaPath.create(true, qName);
154         doReturn(notificationDefinitionSet).when(schemaContextMock).getNotifications();
155         doReturn(schemaPath).when(notificationDefinitionMock).getPath();
156
157         Optional<DOMNotificationService> domNotificationServiceOptionalMock = (Optional<DOMNotificationService>) mock(Optional.class);
158         doReturn(domNotificationServiceOptionalMock).when(domMountPointMock).getService(DOMNotificationService.class);
159         doReturn(true).when(domNotificationServiceOptionalMock).isPresent();
160
161         DOMNotificationService domNotificationServiceMock = mock(DOMNotificationService.class);
162         doReturn(domNotificationServiceMock).when(domNotificationServiceOptionalMock).get();
163         ListenerRegistration listenerRegistrationMock = mock(ListenerRegistration.class);
164         doReturn(listenerRegistrationMock).when(domNotificationServiceMock).registerNotificationListener(any(NetconfEventSource.class), any(List.class));
165
166         Optional<DOMService> optionalMock = (Optional<DOMService>) mock(Optional.class);
167         doReturn(optionalMock).when(domMountPointMock).getService(DOMRpcService.class);
168         DOMRpcService domRpcServiceMock = mock(DOMRpcService.class);
169         doReturn(domRpcServiceMock).when(optionalMock).get();
170         CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
171         doReturn(checkedFutureMock).when(domRpcServiceMock).invokeRpc(any(SchemaPath.class), any(ContainerNode.class));
172     }
173
174 //TODO: create Test for NetConfEventSource#onNotification
175
176     private Set getActiveStreams() throws Exception{
177         Field nesField = NetconfEventSource.class.getDeclaredField("activeStreams");
178         nesField.setAccessible(true);
179         return (Set) nesField.get(netconfEventSource);
180     }
181
182 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.