Bump odlparent/yangtools/mdsal
[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 public class EventSourceTopologyTest {
53
54     EventSourceTopology eventSourceTopology;
55     DataBroker dataBrokerMock;
56     RpcProviderService rpcProviderRegistryMock;
57     RpcConsumerRegistry rpcServiceMock;
58     CreateTopicInput createTopicInputMock;
59     ListenerRegistration<?> listenerRegistrationMock;
60     ObjectRegistration<EventAggregatorService> aggregatorRpcReg;
61
62     @Before
63     public void setUp() {
64         dataBrokerMock = mock(DataBroker.class);
65         rpcProviderRegistryMock = mock(RpcProviderService.class);
66         rpcServiceMock = mock(RpcConsumerRegistry.class);
67     }
68
69     @Test
70     public void constructorTest() {
71         constructorTestHelper();
72         eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock, rpcServiceMock);
73         assertNotNull("Instance has not been created correctly.", eventSourceTopology);
74     }
75
76     private void constructorTestHelper() {
77         aggregatorRpcReg = mock(ObjectRegistration.class);
78         EventSourceService eventSourceService = mock(EventSourceService.class);
79         doReturn(aggregatorRpcReg).when(rpcProviderRegistryMock).registerRpcImplementation(
80             eq(EventAggregatorService.class), any(EventSourceTopology.class));
81         doReturn(eventSourceService).when(rpcServiceMock).getRpcService(EventSourceService.class);
82         WriteTransaction writeTransactionMock = mock(WriteTransaction.class);
83         doReturn(writeTransactionMock).when(dataBrokerMock).newWriteOnlyTransaction();
84         doNothing().when(writeTransactionMock).mergeParentStructurePut(any(LogicalDatastoreType.class),
85             any(InstanceIdentifier.class), any(DataObject.class));
86         FluentFuture checkedFutureMock = mock(FluentFuture.class);
87         doReturn(checkedFutureMock).when(writeTransactionMock).commit();
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 = 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();
107     }
108
109     private void topicTestHelper() throws Exception {
110         constructorTestHelper();
111         createTopicInputMock = mock(CreateTopicInput.class);
112         eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock, rpcServiceMock);
113
114         NotificationPattern notificationPattern = new NotificationPattern("value1");
115         doReturn(notificationPattern).when(createTopicInputMock).getNotificationPattern();
116         Pattern pattern = new Pattern("valuePattern1");
117         doReturn(pattern).when(createTopicInputMock).getNodeIdPattern();
118
119         listenerRegistrationMock = mock(ListenerRegistration.class);
120         doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataTreeChangeListener(
121                 any(DataTreeIdentifier.class), any(EventSourceTopic.class));
122
123         ReadTransaction readOnlyTransactionMock = mock(ReadTransaction.class);
124         doReturn(readOnlyTransactionMock).when(dataBrokerMock).newReadOnlyTransaction();
125
126         FluentFuture checkedFutureMock = mock(FluentFuture.class);
127         doReturn(checkedFutureMock).when(readOnlyTransactionMock).read(eq(LogicalDatastoreType.OPERATIONAL),
128                 any(InstanceIdentifier.class));
129         Topology topologyMock = mock(Topology.class);
130         doReturn(Optional.of(topologyMock)).when(checkedFutureMock).get();
131
132         final NodeKey nodeKey = new NodeKey(new NodeId("nodeIdValue1"));
133         final Node node = new NodeBuilder().withKey(nodeKey).build();
134         doReturn(Map.of(nodeKey, node)).when(topologyMock).getNode();
135     }
136
137     @Test
138     public void closeTest() throws Exception {
139         constructorTestHelper();
140         topicTestHelper();
141         Map<TopicId, EventSourceTopic> localMap = eventSourceTopology.getEventSourceTopicMap();
142         TopicId topicIdMock = mock(TopicId.class);
143         EventSourceTopic eventSourceTopic = EventSourceTopic.create(new NotificationPattern("foo"),
144                 "pattern", eventSourceTopology);
145         localMap.put(topicIdMock, eventSourceTopic);
146         eventSourceTopology.close();
147         verify(aggregatorRpcReg, times(1)).close();
148         verify(listenerRegistrationMock, times(1)).close();
149     }
150
151     @Test
152     public void registerTest() throws Exception {
153         topicTestHelper();
154         Node nodeMock = mock(Node.class);
155         EventSource eventSourceMock = mock(EventSource.class);
156         NodeId nodeId = new NodeId("nodeIdValue1");
157         NodeKey nodeKey = new NodeKey(nodeId);
158         doReturn(nodeKey).when(nodeMock).key();
159         doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
160         ObjectRegistration routedRpcRegistrationMock = mock(ObjectRegistration.class);
161         doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock).registerRpcImplementation(
162             eq(EventSourceService.class), eq(eventSourceMock), any(Set.class));
163         eventSourceTopology.register(eventSourceMock);
164         verify(rpcProviderRegistryMock, times(1)).registerRpcImplementation(eq(EventSourceService.class),
165             eq(eventSourceMock), any(Set.class));
166     }
167
168     @Test
169     public void unregisterTest() throws Exception {
170         topicTestHelper();
171         EventSource eventSourceMock = mock(EventSource.class);
172         NodeId nodeId = new NodeId("nodeIdValue1");
173         NodeKey nodeKey = new NodeKey(nodeId);
174         Map<NodeKey, Registration> localMap = eventSourceTopology.getRoutedRpcRegistrations();
175         NodeKey nodeKeyMock = mock(NodeKey.class);
176         doReturn(nodeKeyMock).when(eventSourceMock).getSourceNodeKey();
177         ObjectRegistration routedRpcRegistrationMock = mock(ObjectRegistration.class);
178         localMap.put(nodeKeyMock, routedRpcRegistrationMock);
179         eventSourceTopology.unRegister(eventSourceMock);
180         verify(routedRpcRegistrationMock, times(1)).close();
181     }
182
183     @Test
184     public void registerEventSourceTest() throws Exception {
185         topicTestHelper();
186         Node nodeMock = mock(Node.class);
187         EventSource eventSourceMock = mock(EventSource.class);
188         NodeId nodeId = new NodeId("nodeIdValue1");
189         NodeKey nodeKey = new NodeKey(nodeId);
190         doReturn(nodeKey).when(nodeMock).key();
191         doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
192         ObjectRegistration routedRpcRegistrationMock = mock(ObjectRegistration.class);
193         doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock)
194                 .registerRpcImplementation(eq(EventSourceService.class), eq(eventSourceMock), any(Set.class));
195         assertNotNull("Return value has not been created correctly.",
196                 eventSourceTopology.registerEventSource(eventSourceMock));
197     }
198 }