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 com.google.common.base.Optional;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import java.util.ArrayList;
22 import java.util.List;
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.DataTreeIdentifier;
28 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.messagebus.spi.EventSource;
32 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
33 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
34 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
35 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.CreateTopicInput;
36 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.DestroyTopicInput;
37 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.DestroyTopicInputBuilder;
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.eventaggregator.rev141202.TopicId;
42 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
48 import org.opendaylight.yangtools.concepts.ListenerRegistration;
49 import org.opendaylight.yangtools.yang.binding.DataObject;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
53 public class EventSourceTopologyTest {
55 EventSourceTopology eventSourceTopology;
56 DataBroker dataBrokerMock;
57 RpcProviderRegistry rpcProviderRegistryMock;
58 CreateTopicInput createTopicInputMock;
59 ListenerRegistration<?> listenerRegistrationMock;
61 RpcRegistration<EventAggregatorService> aggregatorRpcReg;
65 dataBrokerMock = mock(DataBroker.class);
66 rpcProviderRegistryMock = mock(RpcProviderRegistry.class);
70 public void constructorTest() {
71 constructorTestHelper();
72 eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock);
73 assertNotNull("Instance has not been created correctly.", eventSourceTopology);
76 private void constructorTestHelper() {
77 aggregatorRpcReg = mock(RpcRegistration.class);
78 EventSourceService eventSourceService = mock(EventSourceService.class);
79 doReturn(aggregatorRpcReg).when(rpcProviderRegistryMock).addRpcImplementation(eq(EventAggregatorService.class),
80 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),
85 any(DataObject.class),eq(true));
86 CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
87 doReturn(checkedFutureMock).when(writeTransactionMock).submit();
91 public void createTopicTest() throws Exception {
93 assertNotNull("Topic has not been created correctly.", eventSourceTopology.createTopic(createTopicInputMock));
97 public void destroyTopicTest() throws Exception {
99 TopicId topicId = new TopicId("topic-id-007");
100 Map<TopicId, EventSourceTopic> localMap = eventSourceTopology.getEventSourceTopicMap();
101 EventSourceTopic eventSourceTopic = EventSourceTopic.create(new NotificationPattern("foo"),
102 "pattern", eventSourceTopology);
103 localMap.put(topicId, eventSourceTopic);
104 DestroyTopicInput input = new DestroyTopicInputBuilder().setTopicId(topicId).build();
105 eventSourceTopology.destroyTopic(input);
106 verify(listenerRegistrationMock, times(1)).close();
109 private void topicTestHelper() throws Exception {
110 constructorTestHelper();
111 createTopicInputMock = mock(CreateTopicInput.class);
112 eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock);
114 NotificationPattern notificationPattern = new NotificationPattern("value1");
115 doReturn(notificationPattern).when(createTopicInputMock).getNotificationPattern();
116 Pattern pattern = new Pattern("valuePattern1");
117 doReturn(pattern).when(createTopicInputMock).getNodeIdPattern();
119 listenerRegistrationMock = mock(ListenerRegistration.class);
120 doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataTreeChangeListener(
121 any(DataTreeIdentifier.class), any(EventSourceTopic.class));
123 ReadOnlyTransaction readOnlyTransactionMock = mock(ReadOnlyTransaction.class);
124 doReturn(readOnlyTransactionMock).when(dataBrokerMock).newReadOnlyTransaction();
126 CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
127 doReturn(checkedFutureMock).when(readOnlyTransactionMock).read(eq(LogicalDatastoreType.OPERATIONAL),
128 any(InstanceIdentifier.class));
129 Optional optionalMock = mock(Optional.class);
130 doReturn(optionalMock).when(checkedFutureMock).checkedGet();
131 doReturn(true).when(optionalMock).isPresent();
133 Topology topologyMock = mock(Topology.class);
134 doReturn(topologyMock).when(optionalMock).get();
135 Node nodeMock = mock(Node.class);
136 List<Node> nodeList = new ArrayList<>();
137 nodeList.add(nodeMock);
138 doReturn(nodeList).when(topologyMock).getNode();
140 NodeId nodeId = new NodeId("nodeIdValue1");
141 doReturn(nodeId).when(nodeMock).getNodeId();
145 public void closeTest() throws Exception {
146 constructorTestHelper();
148 Map<TopicId, EventSourceTopic> localMap = eventSourceTopology.getEventSourceTopicMap();
149 TopicId topicIdMock = mock(TopicId.class);
150 EventSourceTopic eventSourceTopic = EventSourceTopic.create(new NotificationPattern("foo"),
151 "pattern", eventSourceTopology);
152 localMap.put(topicIdMock, eventSourceTopic);
153 eventSourceTopology.close();
154 verify(aggregatorRpcReg, times(1)).close();
155 verify(listenerRegistrationMock, 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).key();
166 doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
167 BindingAwareBroker.RoutedRpcRegistration routedRpcRegistrationMock = mock(
168 BindingAwareBroker.RoutedRpcRegistration.class);
169 doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock)
170 .addRoutedRpcImplementation(EventSourceService.class, eventSourceMock);
171 doNothing().when(routedRpcRegistrationMock).registerPath(eq(NodeContext.class),
172 any(KeyedInstanceIdentifier.class));
173 eventSourceTopology.register(eventSourceMock);
174 verify(routedRpcRegistrationMock, times(1)).registerPath(eq(NodeContext.class),
175 any(KeyedInstanceIdentifier.class));
179 public void unregisterTest() throws Exception {
181 EventSource eventSourceMock = mock(EventSource.class);
182 NodeId nodeId = new NodeId("nodeIdValue1");
183 nodeKey = new NodeKey(nodeId);
184 Map<NodeKey, BindingAwareBroker.RoutedRpcRegistration<EventSourceService>> localMap = eventSourceTopology
185 .getRoutedRpcRegistrations();
186 NodeKey nodeKeyMock = mock(NodeKey.class);
187 doReturn(nodeKeyMock).when(eventSourceMock).getSourceNodeKey();
188 BindingAwareBroker.RoutedRpcRegistration<EventSourceService> routedRpcRegistrationMock =
189 mock(BindingAwareBroker.RoutedRpcRegistration.class);
190 localMap.put(nodeKeyMock, routedRpcRegistrationMock);
191 eventSourceTopology.unRegister(eventSourceMock);
192 verify(routedRpcRegistrationMock, times(1)).close();
196 public void registerEventSourceTest() throws Exception {
198 Node nodeMock = mock(Node.class);
199 EventSource eventSourceMock = mock(EventSource.class);
200 NodeId nodeId = new NodeId("nodeIdValue1");
201 nodeKey = new NodeKey(nodeId);
202 doReturn(nodeKey).when(nodeMock).key();
203 doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
204 BindingAwareBroker.RoutedRpcRegistration routedRpcRegistrationMock = mock(
205 BindingAwareBroker.RoutedRpcRegistration.class);
206 doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock)
207 .addRoutedRpcImplementation(EventSourceService.class, eventSourceMock);
208 doNothing().when(routedRpcRegistrationMock).registerPath(eq(NodeContext.class),
209 any(KeyedInstanceIdentifier.class));
210 assertNotNull("Return value has not been created correctly.",
211 eventSourceTopology.registerEventSource(eventSourceMock));