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