DataChangeListener cleanup
[controller.git] / opendaylight / md-sal / messagebus-impl / src / test / java / org / opendaylight / controller / messagebus / app / impl / EventSourceTopologyTest.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.assertNotNull;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18
19 import com.google.common.base.Optional;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import java.lang.reflect.Field;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
29 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
30 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.messagebus.spi.EventSource;
33 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
34 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
35 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
36 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.CreateTopicInput;
37 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.DestroyTopicInput;
38 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.DestroyTopicInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.EventAggregatorService;
40 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
41 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.Pattern;
42 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
43 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
49 import org.opendaylight.yangtools.concepts.ListenerRegistration;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
53
54 public class EventSourceTopologyTest {
55
56     EventSourceTopology eventSourceTopology;
57     DataBroker dataBrokerMock;
58     RpcProviderRegistry rpcProviderRegistryMock;
59     CreateTopicInput createTopicInputMock;
60     ListenerRegistration<?> listenerRegistrationMock;
61     NodeKey nodeKey;
62     RpcRegistration<EventAggregatorService> aggregatorRpcReg;
63
64     @Before
65     public void setUp() throws Exception {
66         dataBrokerMock = mock(DataBroker.class);
67         rpcProviderRegistryMock = mock(RpcProviderRegistry.class);
68     }
69
70     @Test
71     public void constructorTest() {
72         constructorTestHelper();
73         eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock);
74         assertNotNull("Instance has not been created correctly.", eventSourceTopology);
75     }
76
77     private void constructorTestHelper(){
78         aggregatorRpcReg = mock(RpcRegistration.class);
79         EventSourceService eventSourceService = mock(EventSourceService.class);
80         doReturn(aggregatorRpcReg).when(rpcProviderRegistryMock).addRpcImplementation(eq(EventAggregatorService.class), any(EventSourceTopology.class));
81         doReturn(eventSourceService).when(rpcProviderRegistryMock).getRpcService(EventSourceService.class);
82         WriteTransaction writeTransactionMock = mock(WriteTransaction.class);
83         doReturn(writeTransactionMock).when(dataBrokerMock).newWriteOnlyTransaction();
84         doNothing().when(writeTransactionMock).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class),eq(true));
85         CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
86         doReturn(checkedFutureMock).when(writeTransactionMock).submit();
87     }
88
89     @Test
90     public void createTopicTest() throws Exception{
91         topicTestHelper();
92         assertNotNull("Topic has not been created correctly.", eventSourceTopology.createTopic(createTopicInputMock));
93     }
94
95     @Test
96     public void destroyTopicTest() throws Exception{
97         topicTestHelper();
98         TopicId topicId = new TopicId("topic-id-007");
99         Map<TopicId,EventSourceTopic> localMap = getEventSourceTopicMap();
100         EventSourceTopic eventSourceTopicMock = mock(EventSourceTopic.class);
101         localMap.put(topicId, eventSourceTopicMock);
102         DestroyTopicInput input = new DestroyTopicInputBuilder().setTopicId(topicId).build();
103         eventSourceTopology.destroyTopic(input);
104         verify(eventSourceTopicMock, times(1)).close();
105     }
106
107     private void topicTestHelper() throws Exception{
108         constructorTestHelper();
109         createTopicInputMock = mock(CreateTopicInput.class);
110         eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock);
111
112         NotificationPattern notificationPattern = new NotificationPattern("value1");
113         doReturn(notificationPattern).when(createTopicInputMock).getNotificationPattern();
114         Pattern pattern = new Pattern("valuePattern1");
115         doReturn(pattern).when(createTopicInputMock).getNodeIdPattern();
116
117         listenerRegistrationMock = mock(ListenerRegistration.class);
118         doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataTreeChangeListener(
119                 any(DataTreeIdentifier.class), any(EventSourceTopic.class));
120
121         ReadOnlyTransaction readOnlyTransactionMock = mock(ReadOnlyTransaction.class);
122         doReturn(readOnlyTransactionMock).when(dataBrokerMock).newReadOnlyTransaction();
123
124         CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
125         doReturn(checkedFutureMock).when(readOnlyTransactionMock).read(eq(LogicalDatastoreType.OPERATIONAL),
126                 any(InstanceIdentifier.class));
127         Optional optionalMock = mock(Optional.class);
128         doReturn(optionalMock).when(checkedFutureMock).checkedGet();
129         doReturn(true).when(optionalMock).isPresent();
130
131         Topology topologyMock = mock(Topology.class);
132         doReturn(topologyMock).when(optionalMock).get();
133         Node nodeMock = mock(Node.class);
134         List<Node> nodeList = new ArrayList<>();
135         nodeList.add(nodeMock);
136         doReturn(nodeList).when(topologyMock).getNode();
137
138         NodeId nodeId = new NodeId("nodeIdValue1");
139         doReturn(nodeId).when(nodeMock).getNodeId();
140     }
141
142     @Test
143     public void closeTest() throws Exception{
144         constructorTestHelper();
145         topicTestHelper();
146         Map<TopicId,EventSourceTopic> localMap = getEventSourceTopicMap();
147         TopicId topicIdMock = mock(TopicId.class);
148         EventSourceTopic eventSourceTopicMock = mock(EventSourceTopic.class);
149         localMap.put(topicIdMock, eventSourceTopicMock);
150         eventSourceTopology.close();
151         verify(aggregatorRpcReg, times(1)).close();
152         verify(eventSourceTopicMock, times(1)).close();
153     }
154
155     @Test
156     public void registerTest() throws Exception {
157         topicTestHelper();
158         Node nodeMock = mock(Node.class);
159         EventSource eventSourceMock = mock(EventSource.class);
160         NodeId nodeId = new NodeId("nodeIdValue1");
161         nodeKey = new NodeKey(nodeId);
162         doReturn(nodeKey).when(nodeMock).getKey();
163         doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
164         BindingAwareBroker.RoutedRpcRegistration routedRpcRegistrationMock = mock(BindingAwareBroker.RoutedRpcRegistration.class);
165         doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock).addRoutedRpcImplementation(EventSourceService.class, eventSourceMock);
166         doNothing().when(routedRpcRegistrationMock).registerPath(eq(NodeContext.class), any(KeyedInstanceIdentifier.class));
167         eventSourceTopology.register(eventSourceMock);
168         verify(routedRpcRegistrationMock, times(1)).registerPath(eq(NodeContext.class), any(KeyedInstanceIdentifier.class));
169     }
170
171     @Test
172     public void unregisterTest() throws Exception {
173         topicTestHelper();
174         EventSource eventSourceMock = mock(EventSource.class);
175         NodeId nodeId = new NodeId("nodeIdValue1");
176         nodeKey = new NodeKey(nodeId);
177         Map<NodeKey, BindingAwareBroker.RoutedRpcRegistration<EventSourceService>> localMap = getRoutedRpcRegistrations();
178         NodeKey nodeKeyMock = mock(NodeKey.class);
179         doReturn(nodeKeyMock).when(eventSourceMock).getSourceNodeKey();
180         BindingAwareBroker.RoutedRpcRegistration<EventSourceService> routedRpcRegistrationMock = mock(BindingAwareBroker.RoutedRpcRegistration.class);
181         localMap.put(nodeKeyMock, routedRpcRegistrationMock);
182         eventSourceTopology.unRegister(eventSourceMock);
183         verify(routedRpcRegistrationMock, times(1)).close();
184     }
185
186     @Test
187     public void registerEventSourceTest() throws Exception {
188         topicTestHelper();
189         Node nodeMock = mock(Node.class);
190         EventSource eventSourceMock = mock(EventSource.class);
191         NodeId nodeId = new NodeId("nodeIdValue1");
192         nodeKey = new NodeKey(nodeId);
193         doReturn(nodeKey).when(nodeMock).getKey();
194         doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
195         BindingAwareBroker.RoutedRpcRegistration routedRpcRegistrationMock = mock(BindingAwareBroker.RoutedRpcRegistration.class);
196         doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock).addRoutedRpcImplementation(EventSourceService.class, eventSourceMock);
197         doNothing().when(routedRpcRegistrationMock).registerPath(eq(NodeContext.class), any(KeyedInstanceIdentifier.class));
198         assertNotNull("Return value has not been created correctly.", eventSourceTopology.registerEventSource(eventSourceMock));
199     }
200
201     private Map getEventSourceTopicMap() throws Exception{
202         Field nesField = EventSourceTopology.class.getDeclaredField("eventSourceTopicMap");
203         nesField.setAccessible(true);
204         return (Map) nesField.get(eventSourceTopology);
205     }
206
207     private Map getRoutedRpcRegistrations() throws Exception{
208         Field nesField = EventSourceTopology.class.getDeclaredField("routedRpcRegistrations");
209         nesField.setAccessible(true);
210         return (Map) nesField.get(eventSourceTopology);
211     }
212
213 }

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