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

©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.