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

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