Remove deprecated MD-SAL APIs
[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.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Optional;
24 import java.util.Set;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.messagebus.spi.EventSource;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
30 import org.opendaylight.mdsal.binding.api.ReadTransaction;
31 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
32 import org.opendaylight.mdsal.binding.api.RpcProviderService;
33 import org.opendaylight.mdsal.binding.api.WriteTransaction;
34 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
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.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.concepts.ObjectRegistration;
49 import org.opendaylight.yangtools.concepts.Registration;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52
53 public class EventSourceTopologyTest {
54
55     EventSourceTopology eventSourceTopology;
56     DataBroker dataBrokerMock;
57     RpcProviderService rpcProviderRegistryMock;
58     RpcConsumerRegistry rpcServiceMock;
59     CreateTopicInput createTopicInputMock;
60     ListenerRegistration<?> listenerRegistrationMock;
61     NodeKey nodeKey;
62     ObjectRegistration<EventAggregatorService> aggregatorRpcReg;
63
64     @Before
65     public void setUp() {
66         dataBrokerMock = mock(DataBroker.class);
67         rpcProviderRegistryMock = mock(RpcProviderService.class);
68         rpcServiceMock = mock(RpcConsumerRegistry.class);
69     }
70
71     @Test
72     public void constructorTest() {
73         constructorTestHelper();
74         eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock, rpcServiceMock);
75         assertNotNull("Instance has not been created correctly.", eventSourceTopology);
76     }
77
78     private void constructorTestHelper() {
79         aggregatorRpcReg = mock(ObjectRegistration.class);
80         EventSourceService eventSourceService = mock(EventSourceService.class);
81         doReturn(aggregatorRpcReg).when(rpcProviderRegistryMock).registerRpcImplementation(
82             eq(EventAggregatorService.class), any(EventSourceTopology.class));
83         doReturn(eventSourceService).when(rpcServiceMock).getRpcService(EventSourceService.class);
84         WriteTransaction writeTransactionMock = mock(WriteTransaction.class);
85         doReturn(writeTransactionMock).when(dataBrokerMock).newWriteOnlyTransaction();
86         doNothing().when(writeTransactionMock).mergeParentStructurePut(any(LogicalDatastoreType.class),
87             any(InstanceIdentifier.class), any(DataObject.class));
88         FluentFuture checkedFutureMock = mock(FluentFuture.class);
89         doReturn(checkedFutureMock).when(writeTransactionMock).commit();
90     }
91
92     @Test
93     public void createTopicTest() throws Exception {
94         topicTestHelper();
95         assertNotNull("Topic has not been created correctly.", eventSourceTopology.createTopic(createTopicInputMock));
96     }
97
98     @Test
99     public void destroyTopicTest() throws Exception {
100         topicTestHelper();
101         TopicId topicId = new TopicId("topic-id-007");
102         Map<TopicId, EventSourceTopic> localMap = eventSourceTopology.getEventSourceTopicMap();
103         EventSourceTopic eventSourceTopic = EventSourceTopic.create(new NotificationPattern("foo"),
104                 "pattern", eventSourceTopology);
105         localMap.put(topicId, eventSourceTopic);
106         DestroyTopicInput input = new DestroyTopicInputBuilder().setTopicId(topicId).build();
107         eventSourceTopology.destroyTopic(input);
108         verify(listenerRegistrationMock, times(1)).close();
109     }
110
111     private void topicTestHelper() throws Exception {
112         constructorTestHelper();
113         createTopicInputMock = mock(CreateTopicInput.class);
114         eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock, rpcServiceMock);
115
116         NotificationPattern notificationPattern = new NotificationPattern("value1");
117         doReturn(notificationPattern).when(createTopicInputMock).getNotificationPattern();
118         Pattern pattern = new Pattern("valuePattern1");
119         doReturn(pattern).when(createTopicInputMock).getNodeIdPattern();
120
121         listenerRegistrationMock = mock(ListenerRegistration.class);
122         doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataTreeChangeListener(
123                 any(DataTreeIdentifier.class), any(EventSourceTopic.class));
124
125         ReadTransaction readOnlyTransactionMock = mock(ReadTransaction.class);
126         doReturn(readOnlyTransactionMock).when(dataBrokerMock).newReadOnlyTransaction();
127
128         FluentFuture checkedFutureMock = mock(FluentFuture.class);
129         doReturn(checkedFutureMock).when(readOnlyTransactionMock).read(eq(LogicalDatastoreType.OPERATIONAL),
130                 any(InstanceIdentifier.class));
131         Topology topologyMock = mock(Topology.class);
132         doReturn(Optional.of(topologyMock)).when(checkedFutureMock).get();
133
134         Node nodeMock = mock(Node.class);
135         List<Node> nodeList = new ArrayList<>();
136         nodeList.add(nodeMock);
137         doReturn(nodeList).when(topologyMock).getNode();
138
139         NodeId nodeId = new NodeId("nodeIdValue1");
140         doReturn(nodeId).when(nodeMock).getNodeId();
141     }
142
143     @Test
144     public void closeTest() throws Exception {
145         constructorTestHelper();
146         topicTestHelper();
147         Map<TopicId, EventSourceTopic> localMap = eventSourceTopology.getEventSourceTopicMap();
148         TopicId topicIdMock = mock(TopicId.class);
149         EventSourceTopic eventSourceTopic = EventSourceTopic.create(new NotificationPattern("foo"),
150                 "pattern", eventSourceTopology);
151         localMap.put(topicIdMock, eventSourceTopic);
152         eventSourceTopology.close();
153         verify(aggregatorRpcReg, times(1)).close();
154         verify(listenerRegistrationMock, times(1)).close();
155     }
156
157     @Test
158     public void registerTest() throws Exception {
159         topicTestHelper();
160         Node nodeMock = mock(Node.class);
161         EventSource eventSourceMock = mock(EventSource.class);
162         NodeId nodeId = new NodeId("nodeIdValue1");
163         nodeKey = new NodeKey(nodeId);
164         doReturn(nodeKey).when(nodeMock).key();
165         doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
166         ObjectRegistration routedRpcRegistrationMock = mock(ObjectRegistration.class);
167         doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock).registerRpcImplementation(
168             eq(EventSourceService.class), eq(eventSourceMock), any(Set.class));
169         eventSourceTopology.register(eventSourceMock);
170         verify(rpcProviderRegistryMock, times(1)).registerRpcImplementation(eq(EventSourceService.class),
171             eq(eventSourceMock), any(Set.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, Registration> localMap = eventSourceTopology.getRoutedRpcRegistrations();
181         NodeKey nodeKeyMock = mock(NodeKey.class);
182         doReturn(nodeKeyMock).when(eventSourceMock).getSourceNodeKey();
183         ObjectRegistration routedRpcRegistrationMock = mock(ObjectRegistration.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).key();
197         doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
198         ObjectRegistration routedRpcRegistrationMock = mock(ObjectRegistration.class);
199         doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock)
200                 .registerRpcImplementation(eq(EventSourceService.class), eq(eventSourceMock), any(Set.class));
201         assertNotNull("Return value has not been created correctly.",
202                 eventSourceTopology.registerEventSource(eventSourceMock));
203     }
204 }