Deprecate messagebus for removal
[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.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.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.util.concurrent.FluentFuture;
20 import java.util.Map;
21 import java.util.Optional;
22 import java.util.Set;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.messagebus.spi.EventSource;
26 import org.opendaylight.mdsal.binding.api.DataBroker;
27 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
28 import org.opendaylight.mdsal.binding.api.ReadTransaction;
29 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
30 import org.opendaylight.mdsal.binding.api.RpcProviderService;
31 import org.opendaylight.mdsal.binding.api.WriteTransaction;
32 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
33 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.CreateTopicInput;
34 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.DestroyTopicInput;
35 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.DestroyTopicInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.EventAggregatorService;
37 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
38 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.Pattern;
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.EventSourceService;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
46 import org.opendaylight.yangtools.concepts.ListenerRegistration;
47 import org.opendaylight.yangtools.concepts.ObjectRegistration;
48 import org.opendaylight.yangtools.concepts.Registration;
49 import org.opendaylight.yangtools.yang.binding.DataObject;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51
52 @Deprecated(forRemoval = true)
53 public class EventSourceTopologyTest {
54
55     EventSourceTopology eventSourceTopology;
56     DataBroker dataBrokerMock;
57     RpcProviderService rpcProviderRegistryMock;
58     RpcConsumerRegistry rpcServiceMock;
59     CreateTopicInput createTopicInputMock;
60     ListenerRegistration<?> listenerRegistrationMock;
61     ObjectRegistration<EventAggregatorService> aggregatorRpcReg;
62
63     @Before
64     public void setUp() {
65         dataBrokerMock = mock(DataBroker.class);
66         rpcProviderRegistryMock = mock(RpcProviderService.class);
67         rpcServiceMock = mock(RpcConsumerRegistry.class);
68     }
69
70     @Test
71     public void constructorTest() {
72         constructorTestHelper();
73         eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock, rpcServiceMock);
74         assertNotNull("Instance has not been created correctly.", eventSourceTopology);
75     }
76
77     private void constructorTestHelper() {
78         aggregatorRpcReg = mock(ObjectRegistration.class);
79         EventSourceService eventSourceService = mock(EventSourceService.class);
80         doReturn(aggregatorRpcReg).when(rpcProviderRegistryMock).registerRpcImplementation(
81             eq(EventAggregatorService.class), any(EventSourceTopology.class));
82         doReturn(eventSourceService).when(rpcServiceMock).getRpcService(EventSourceService.class);
83         WriteTransaction writeTransactionMock = mock(WriteTransaction.class);
84         doReturn(writeTransactionMock).when(dataBrokerMock).newWriteOnlyTransaction();
85         doNothing().when(writeTransactionMock).mergeParentStructurePut(any(LogicalDatastoreType.class),
86             any(InstanceIdentifier.class), any(DataObject.class));
87         FluentFuture checkedFutureMock = mock(FluentFuture.class);
88         doReturn(checkedFutureMock).when(writeTransactionMock).commit();
89     }
90
91     @Test
92     public void createTopicTest() throws Exception {
93         topicTestHelper();
94         assertNotNull("Topic has not been created correctly.", eventSourceTopology.createTopic(createTopicInputMock));
95     }
96
97     @Test
98     public void destroyTopicTest() throws Exception {
99         topicTestHelper();
100         TopicId topicId = new TopicId("topic-id-007");
101         Map<TopicId, EventSourceTopic> localMap = eventSourceTopology.getEventSourceTopicMap();
102         EventSourceTopic eventSourceTopic = EventSourceTopic.create(new NotificationPattern("foo"),
103                 "pattern", eventSourceTopology);
104         localMap.put(topicId, eventSourceTopic);
105         DestroyTopicInput input = new DestroyTopicInputBuilder().setTopicId(topicId).build();
106         eventSourceTopology.destroyTopic(input);
107         verify(listenerRegistrationMock, times(1)).close();
108     }
109
110     private void topicTestHelper() throws Exception {
111         constructorTestHelper();
112         createTopicInputMock = mock(CreateTopicInput.class);
113         eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock, rpcServiceMock);
114
115         NotificationPattern notificationPattern = new NotificationPattern("value1");
116         doReturn(notificationPattern).when(createTopicInputMock).getNotificationPattern();
117         Pattern pattern = new Pattern("valuePattern1");
118         doReturn(pattern).when(createTopicInputMock).getNodeIdPattern();
119
120         listenerRegistrationMock = mock(ListenerRegistration.class);
121         doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataTreeChangeListener(
122                 any(DataTreeIdentifier.class), any(EventSourceTopic.class));
123
124         ReadTransaction readOnlyTransactionMock = mock(ReadTransaction.class);
125         doReturn(readOnlyTransactionMock).when(dataBrokerMock).newReadOnlyTransaction();
126
127         FluentFuture checkedFutureMock = mock(FluentFuture.class);
128         doReturn(checkedFutureMock).when(readOnlyTransactionMock).read(eq(LogicalDatastoreType.OPERATIONAL),
129                 any(InstanceIdentifier.class));
130         Topology topologyMock = mock(Topology.class);
131         doReturn(Optional.of(topologyMock)).when(checkedFutureMock).get();
132
133         final NodeKey nodeKey = new NodeKey(new NodeId("nodeIdValue1"));
134         final Node node = new NodeBuilder().withKey(nodeKey).build();
135         doReturn(Map.of(nodeKey, node)).when(topologyMock).getNode();
136     }
137
138     @Test
139     public void closeTest() throws Exception {
140         constructorTestHelper();
141         topicTestHelper();
142         Map<TopicId, EventSourceTopic> localMap = eventSourceTopology.getEventSourceTopicMap();
143         TopicId topicIdMock = mock(TopicId.class);
144         EventSourceTopic eventSourceTopic = EventSourceTopic.create(new NotificationPattern("foo"),
145                 "pattern", eventSourceTopology);
146         localMap.put(topicIdMock, eventSourceTopic);
147         eventSourceTopology.close();
148         verify(aggregatorRpcReg, times(1)).close();
149         verify(listenerRegistrationMock, times(1)).close();
150     }
151
152     @Test
153     public void registerTest() throws Exception {
154         topicTestHelper();
155         Node nodeMock = mock(Node.class);
156         EventSource eventSourceMock = mock(EventSource.class);
157         NodeId nodeId = new NodeId("nodeIdValue1");
158         NodeKey nodeKey = new NodeKey(nodeId);
159         doReturn(nodeKey).when(nodeMock).key();
160         doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
161         ObjectRegistration routedRpcRegistrationMock = mock(ObjectRegistration.class);
162         doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock).registerRpcImplementation(
163             eq(EventSourceService.class), eq(eventSourceMock), any(Set.class));
164         eventSourceTopology.register(eventSourceMock);
165         verify(rpcProviderRegistryMock, times(1)).registerRpcImplementation(eq(EventSourceService.class),
166             eq(eventSourceMock), any(Set.class));
167     }
168
169     @Test
170     public void unregisterTest() throws Exception {
171         topicTestHelper();
172         EventSource eventSourceMock = mock(EventSource.class);
173         NodeId nodeId = new NodeId("nodeIdValue1");
174         NodeKey nodeKey = new NodeKey(nodeId);
175         Map<NodeKey, Registration> localMap = eventSourceTopology.getRoutedRpcRegistrations();
176         NodeKey nodeKeyMock = mock(NodeKey.class);
177         doReturn(nodeKeyMock).when(eventSourceMock).getSourceNodeKey();
178         ObjectRegistration routedRpcRegistrationMock = mock(ObjectRegistration.class);
179         localMap.put(nodeKeyMock, routedRpcRegistrationMock);
180         eventSourceTopology.unRegister(eventSourceMock);
181         verify(routedRpcRegistrationMock, times(1)).close();
182     }
183
184     @Test
185     public void registerEventSourceTest() throws Exception {
186         topicTestHelper();
187         Node nodeMock = mock(Node.class);
188         EventSource eventSourceMock = mock(EventSource.class);
189         NodeId nodeId = new NodeId("nodeIdValue1");
190         NodeKey nodeKey = new NodeKey(nodeId);
191         doReturn(nodeKey).when(nodeMock).key();
192         doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
193         ObjectRegistration routedRpcRegistrationMock = mock(ObjectRegistration.class);
194         doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock)
195                 .registerRpcImplementation(eq(EventSourceService.class), eq(eventSourceMock), any(Set.class));
196         assertNotNull("Return value has not been created correctly.",
197                 eventSourceTopology.registerEventSource(eventSourceMock));
198     }
199 }