2 * Copyright (c) 2015 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.controller.messagebus.app.impl;
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;
19 import java.lang.reflect.Field;
20 import java.util.ArrayList;
21 import java.util.List;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
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.EventAggregatorService;
39 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
40 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.Pattern;
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;
52 import com.google.common.base.Optional;
53 import com.google.common.util.concurrent.CheckedFuture;
55 public class EventSourceTopologyTest {
57 EventSourceTopology eventSourceTopology;
58 DataBroker dataBrokerMock;
59 RpcProviderRegistry rpcProviderRegistryMock;
60 CreateTopicInput createTopicInputMock;
61 ListenerRegistration listenerRegistrationMock;
63 RpcRegistration<EventAggregatorService> aggregatorRpcReg;
66 public static void initTestClass() throws IllegalAccessException, InstantiationException {
70 public void setUp() throws Exception {
71 dataBrokerMock = mock(DataBroker.class);
72 rpcProviderRegistryMock = mock(RpcProviderRegistry.class);
76 public void constructorTest() {
77 constructorTestHelper();
78 eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock);
79 assertNotNull("Instance has not been created correctly.", eventSourceTopology);
82 private void constructorTestHelper(){
83 aggregatorRpcReg = mock(RpcRegistration.class);
84 EventSourceService eventSourceService = mock(EventSourceService.class);
85 doReturn(aggregatorRpcReg).when(rpcProviderRegistryMock).addRpcImplementation(eq(EventAggregatorService.class), any(EventSourceTopology.class));
86 doReturn(eventSourceService).when(rpcProviderRegistryMock).getRpcService(EventSourceService.class);
87 WriteTransaction writeTransactionMock = mock(WriteTransaction.class);
88 doReturn(writeTransactionMock).when(dataBrokerMock).newWriteOnlyTransaction();
89 doNothing().when(writeTransactionMock).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class),eq(true));
90 CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
91 doReturn(checkedFutureMock).when(writeTransactionMock).submit();
95 public void createTopicTest() throws Exception{
97 assertNotNull("Topic has not been created correctly.", eventSourceTopology.createTopic(createTopicInputMock));
100 private void topicTestHelper() throws Exception{
101 constructorTestHelper();
102 createTopicInputMock = mock(CreateTopicInput.class);
103 eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock);
105 NotificationPattern notificationPattern = new NotificationPattern("value1");
106 doReturn(notificationPattern).when(createTopicInputMock).getNotificationPattern();
107 Pattern pattern = new Pattern("valuePattern1");
108 doReturn(pattern).when(createTopicInputMock).getNodeIdPattern();
110 listenerRegistrationMock = mock(ListenerRegistration.class);
111 doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataChangeListener(eq(LogicalDatastoreType.OPERATIONAL),
112 any(InstanceIdentifier.class),
113 any(EventSourceTopic.class),
114 eq(DataBroker.DataChangeScope.SUBTREE));
116 ReadOnlyTransaction readOnlyTransactionMock = mock(ReadOnlyTransaction.class);
117 doReturn(readOnlyTransactionMock).when(dataBrokerMock).newReadOnlyTransaction();
119 CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
120 doReturn(checkedFutureMock).when(readOnlyTransactionMock).read(eq(LogicalDatastoreType.OPERATIONAL),
121 any(InstanceIdentifier.class));
122 Optional optionalMock = mock(Optional.class);
123 doReturn(optionalMock).when(checkedFutureMock).checkedGet();
124 doReturn(true).when(optionalMock).isPresent();
126 Topology topologyMock = mock(Topology.class);
127 doReturn(topologyMock).when(optionalMock).get();
128 Node nodeMock = mock(Node.class);
129 List<Node> nodeList = new ArrayList<>();
130 nodeList.add(nodeMock);
131 doReturn(nodeList).when(topologyMock).getNode();
133 NodeId nodeId = new NodeId("nodeIdValue1");
134 doReturn(nodeId).when(nodeMock).getNodeId();
138 public void destroyTopicTest() throws Exception{
140 //TODO: modify test when destroyTopic will be implemented
141 DestroyTopicInput destroyTopicInput = null;
142 assertNotNull("Instance has not been created correctly.", eventSourceTopology.destroyTopic(destroyTopicInput));
146 public void closeTest() throws Exception{
147 constructorTestHelper();
149 Map<DataChangeListener, ListenerRegistration<DataChangeListener>> localMap = getTopicListenerRegistrations();
150 DataChangeListener dataChangeListenerMock = mock(DataChangeListener.class);
151 ListenerRegistration<DataChangeListener> listenerListenerRegistrationMock = (ListenerRegistration<DataChangeListener>) mock(ListenerRegistration.class);
152 localMap.put(dataChangeListenerMock, listenerListenerRegistrationMock);
153 eventSourceTopology.close();
154 verify(aggregatorRpcReg, times(1)).close();
155 verify(listenerListenerRegistrationMock, times(1)).close();
159 public void registerTest() throws Exception {
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));
175 public void unregisterTest() throws Exception {
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();
190 public void registerEventSourceTest() throws Exception {
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));
204 private Map getTopicListenerRegistrations() throws Exception{
205 Field nesField = EventSourceTopology.class.getDeclaredField("topicListenerRegistrations");
206 nesField.setAccessible(true);
207 return (Map) nesField.get(eventSourceTopology);
210 private Map getRoutedRpcRegistrations() throws Exception{
211 Field nesField = EventSourceTopology.class.getDeclaredField("routedRpcRegistrations");
212 nesField.setAccessible(true);
213 return (Map) nesField.get(eventSourceTopology);