Fix checkstyle violations in messagebus
[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.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;
18
19 import com.google.common.base.Optional;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import java.lang.reflect.Field;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
29 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
30 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.messagebus.spi.EventSource;
33 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
34 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
35 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
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.opendaylight.inventory.rev130819.NodeContext;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
49 import org.opendaylight.yangtools.concepts.ListenerRegistration;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
53
54 public class EventSourceTopologyTest {
55
56     EventSourceTopology eventSourceTopology;
57     DataBroker dataBrokerMock;
58     RpcProviderRegistry rpcProviderRegistryMock;
59     CreateTopicInput createTopicInputMock;
60     ListenerRegistration<?> listenerRegistrationMock;
61     NodeKey nodeKey;
62     RpcRegistration<EventAggregatorService> aggregatorRpcReg;
63
64     @Before
65     public void setUp() throws Exception {
66         dataBrokerMock = mock(DataBroker.class);
67         rpcProviderRegistryMock = mock(RpcProviderRegistry.class);
68     }
69
70     @Test
71     public void constructorTest() {
72         constructorTestHelper();
73         eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock);
74         assertNotNull("Instance has not been created correctly.", eventSourceTopology);
75     }
76
77     private void constructorTestHelper() {
78         aggregatorRpcReg = mock(RpcRegistration.class);
79         EventSourceService eventSourceService = mock(EventSourceService.class);
80         doReturn(aggregatorRpcReg).when(rpcProviderRegistryMock).addRpcImplementation(eq(EventAggregatorService.class),
81                 any(EventSourceTopology.class));
82         doReturn(eventSourceService).when(rpcProviderRegistryMock).getRpcService(EventSourceService.class);
83         WriteTransaction writeTransactionMock = mock(WriteTransaction.class);
84         doReturn(writeTransactionMock).when(dataBrokerMock).newWriteOnlyTransaction();
85         doNothing().when(writeTransactionMock).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
86                 any(DataObject.class),eq(true));
87         CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
88         doReturn(checkedFutureMock).when(writeTransactionMock).submit();
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 = 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);
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         ReadOnlyTransaction readOnlyTransactionMock = mock(ReadOnlyTransaction.class);
125         doReturn(readOnlyTransactionMock).when(dataBrokerMock).newReadOnlyTransaction();
126
127         CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
128         doReturn(checkedFutureMock).when(readOnlyTransactionMock).read(eq(LogicalDatastoreType.OPERATIONAL),
129                 any(InstanceIdentifier.class));
130         Optional optionalMock = mock(Optional.class);
131         doReturn(optionalMock).when(checkedFutureMock).checkedGet();
132         doReturn(true).when(optionalMock).isPresent();
133
134         Topology topologyMock = mock(Topology.class);
135         doReturn(topologyMock).when(optionalMock).get();
136         Node nodeMock = mock(Node.class);
137         List<Node> nodeList = new ArrayList<>();
138         nodeList.add(nodeMock);
139         doReturn(nodeList).when(topologyMock).getNode();
140
141         NodeId nodeId = new NodeId("nodeIdValue1");
142         doReturn(nodeId).when(nodeMock).getNodeId();
143     }
144
145     @Test
146     public void closeTest() throws Exception {
147         constructorTestHelper();
148         topicTestHelper();
149         Map<TopicId,EventSourceTopic> localMap = getEventSourceTopicMap();
150         TopicId topicIdMock = mock(TopicId.class);
151         EventSourceTopic eventSourceTopic = EventSourceTopic.create(new NotificationPattern("foo"),
152                 "pattern", eventSourceTopology);
153         localMap.put(topicIdMock, eventSourceTopic);
154         eventSourceTopology.close();
155         verify(aggregatorRpcReg, times(1)).close();
156         verify(listenerRegistrationMock, times(1)).close();
157     }
158
159     @Test
160     public void registerTest() throws Exception {
161         topicTestHelper();
162         Node nodeMock = mock(Node.class);
163         EventSource eventSourceMock = mock(EventSource.class);
164         NodeId nodeId = new NodeId("nodeIdValue1");
165         nodeKey = new NodeKey(nodeId);
166         doReturn(nodeKey).when(nodeMock).getKey();
167         doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
168         BindingAwareBroker.RoutedRpcRegistration routedRpcRegistrationMock = mock(
169                 BindingAwareBroker.RoutedRpcRegistration.class);
170         doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock)
171                 .addRoutedRpcImplementation(EventSourceService.class, eventSourceMock);
172         doNothing().when(routedRpcRegistrationMock).registerPath(eq(NodeContext.class),
173                 any(KeyedInstanceIdentifier.class));
174         eventSourceTopology.register(eventSourceMock);
175         verify(routedRpcRegistrationMock, times(1)).registerPath(eq(NodeContext.class),
176                 any(KeyedInstanceIdentifier.class));
177     }
178
179     @Test
180     public void unregisterTest() throws Exception {
181         topicTestHelper();
182         EventSource eventSourceMock = mock(EventSource.class);
183         NodeId nodeId = new NodeId("nodeIdValue1");
184         nodeKey = new NodeKey(nodeId);
185         Map<NodeKey, BindingAwareBroker.RoutedRpcRegistration<EventSourceService>> localMap =
186                 getRoutedRpcRegistrations();
187         NodeKey nodeKeyMock = mock(NodeKey.class);
188         doReturn(nodeKeyMock).when(eventSourceMock).getSourceNodeKey();
189         BindingAwareBroker.RoutedRpcRegistration<EventSourceService> routedRpcRegistrationMock =
190                 mock(BindingAwareBroker.RoutedRpcRegistration.class);
191         localMap.put(nodeKeyMock, routedRpcRegistrationMock);
192         eventSourceTopology.unRegister(eventSourceMock);
193         verify(routedRpcRegistrationMock, times(1)).close();
194     }
195
196     @Test
197     public void registerEventSourceTest() throws Exception {
198         topicTestHelper();
199         Node nodeMock = mock(Node.class);
200         EventSource eventSourceMock = mock(EventSource.class);
201         NodeId nodeId = new NodeId("nodeIdValue1");
202         nodeKey = new NodeKey(nodeId);
203         doReturn(nodeKey).when(nodeMock).getKey();
204         doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
205         BindingAwareBroker.RoutedRpcRegistration routedRpcRegistrationMock = mock(
206                 BindingAwareBroker.RoutedRpcRegistration.class);
207         doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock)
208                 .addRoutedRpcImplementation(EventSourceService.class, eventSourceMock);
209         doNothing().when(routedRpcRegistrationMock).registerPath(eq(NodeContext.class),
210                 any(KeyedInstanceIdentifier.class));
211         assertNotNull("Return value has not been created correctly.",
212                 eventSourceTopology.registerEventSource(eventSourceMock));
213     }
214
215     private Map getEventSourceTopicMap() throws Exception {
216         Field nesField = EventSourceTopology.class.getDeclaredField("eventSourceTopicMap");
217         nesField.setAccessible(true);
218         return (Map) nesField.get(eventSourceTopology);
219     }
220
221     private Map getRoutedRpcRegistrations() throws Exception {
222         Field nesField = EventSourceTopology.class.getDeclaredField("routedRpcRegistrations");
223         nesField.setAccessible(true);
224         return (Map) nesField.get(eventSourceTopology);
225     }
226
227 }