Merge "Bug 2948: Recovered log entries not applied after prior snapshot"
[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 java.lang.reflect.Field;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
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.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.eventsource.rev141202.EventSourceService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
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.yang.binding.DataObject;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
51
52 import com.google.common.base.Optional;
53 import com.google.common.util.concurrent.CheckedFuture;
54
55 public class EventSourceTopologyTest {
56
57     EventSourceTopology eventSourceTopology;
58     DataBroker dataBrokerMock;
59     RpcProviderRegistry rpcProviderRegistryMock;
60     CreateTopicInput createTopicInputMock;
61     ListenerRegistration listenerRegistrationMock;
62     NodeKey nodeKey;
63     RpcRegistration<EventAggregatorService> aggregatorRpcReg;
64
65     @BeforeClass
66     public static void initTestClass() throws IllegalAccessException, InstantiationException {
67     }
68
69     @Before
70     public void setUp() throws Exception {
71         dataBrokerMock = mock(DataBroker.class);
72         rpcProviderRegistryMock = mock(RpcProviderRegistry.class);
73     }
74
75     @Test
76     public void constructorTest() {
77         constructorTestHelper();
78         eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock);
79         assertNotNull("Instance has not been created correctly.", eventSourceTopology);
80     }
81
82     private void constructorTestHelper(){
83         aggregatorRpcReg = mock(RpcRegistration.class);
84         EventSourceService eventSourceService = mock(EventSourceService.class);
85         doReturn(aggregatorRpcReg).when(rpcProviderRegistryMock).addRpcImplementation(eq(EventAggregatorService.class), any(EventSourceTopology.class));
86         doReturn(eventSourceService).when(rpcProviderRegistryMock).getRpcService(EventSourceService.class);
87         WriteTransaction writeTransactionMock = mock(WriteTransaction.class);
88         doReturn(writeTransactionMock).when(dataBrokerMock).newWriteOnlyTransaction();
89         doNothing().when(writeTransactionMock).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class),eq(true));
90         CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
91         doReturn(checkedFutureMock).when(writeTransactionMock).submit();
92     }
93
94     @Test
95     public void createTopicTest() throws Exception{
96         topicTestHelper();
97         assertNotNull("Topic has not been created correctly.", eventSourceTopology.createTopic(createTopicInputMock));
98     }
99
100     private void topicTestHelper() throws Exception{
101         constructorTestHelper();
102         createTopicInputMock = mock(CreateTopicInput.class);
103         eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock);
104
105         NotificationPattern notificationPattern = new NotificationPattern("value1");
106         doReturn(notificationPattern).when(createTopicInputMock).getNotificationPattern();
107         Pattern pattern = new Pattern("valuePattern1");
108         doReturn(pattern).when(createTopicInputMock).getNodeIdPattern();
109
110         listenerRegistrationMock = mock(ListenerRegistration.class);
111         doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataChangeListener(eq(LogicalDatastoreType.OPERATIONAL),
112                 any(InstanceIdentifier.class),
113                 any(EventSourceTopic.class),
114                 eq(DataBroker.DataChangeScope.SUBTREE));
115
116         ReadOnlyTransaction readOnlyTransactionMock = mock(ReadOnlyTransaction.class);
117         doReturn(readOnlyTransactionMock).when(dataBrokerMock).newReadOnlyTransaction();
118
119         CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
120         doReturn(checkedFutureMock).when(readOnlyTransactionMock).read(eq(LogicalDatastoreType.OPERATIONAL),
121                 any(InstanceIdentifier.class));
122         Optional optionalMock = mock(Optional.class);
123         doReturn(optionalMock).when(checkedFutureMock).checkedGet();
124         doReturn(true).when(optionalMock).isPresent();
125
126         Topology topologyMock = mock(Topology.class);
127         doReturn(topologyMock).when(optionalMock).get();
128         Node nodeMock = mock(Node.class);
129         List<Node> nodeList = new ArrayList<>();
130         nodeList.add(nodeMock);
131         doReturn(nodeList).when(topologyMock).getNode();
132
133         NodeId nodeId = new NodeId("nodeIdValue1");
134         doReturn(nodeId).when(nodeMock).getNodeId();
135     }
136
137     @Test
138     public void destroyTopicTest() throws Exception{
139         topicTestHelper();
140         //TODO: modify test when destroyTopic will be implemented
141         DestroyTopicInput destroyTopicInput = null;
142         assertNotNull("Instance has not been created correctly.", eventSourceTopology.destroyTopic(destroyTopicInput));
143     }
144
145     @Test
146     public void closeTest() throws Exception{
147         constructorTestHelper();
148         topicTestHelper();
149         Map<DataChangeListener, ListenerRegistration<DataChangeListener>> localMap = getTopicListenerRegistrations();
150         DataChangeListener dataChangeListenerMock = mock(DataChangeListener.class);
151         ListenerRegistration<DataChangeListener> listenerListenerRegistrationMock = (ListenerRegistration<DataChangeListener>) mock(ListenerRegistration.class);
152         localMap.put(dataChangeListenerMock, listenerListenerRegistrationMock);
153         eventSourceTopology.close();
154         verify(aggregatorRpcReg, times(1)).close();
155         verify(listenerListenerRegistrationMock, 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).getKey();
166         doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
167         BindingAwareBroker.RoutedRpcRegistration routedRpcRegistrationMock = mock(BindingAwareBroker.RoutedRpcRegistration.class);
168         doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock).addRoutedRpcImplementation(EventSourceService.class, eventSourceMock);
169         doNothing().when(routedRpcRegistrationMock).registerPath(eq(NodeContext.class), any(KeyedInstanceIdentifier.class));
170         eventSourceTopology.register(eventSourceMock);
171         verify(routedRpcRegistrationMock, times(1)).registerPath(eq(NodeContext.class), any(KeyedInstanceIdentifier.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, BindingAwareBroker.RoutedRpcRegistration<EventSourceService>> localMap = getRoutedRpcRegistrations();
181         NodeKey nodeKeyMock = mock(NodeKey.class);
182         doReturn(nodeKeyMock).when(eventSourceMock).getSourceNodeKey();
183         BindingAwareBroker.RoutedRpcRegistration<EventSourceService> routedRpcRegistrationMock = (BindingAwareBroker.RoutedRpcRegistration<EventSourceService>) mock(BindingAwareBroker.RoutedRpcRegistration.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).getKey();
197         doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
198         BindingAwareBroker.RoutedRpcRegistration routedRpcRegistrationMock = mock(BindingAwareBroker.RoutedRpcRegistration.class);
199         doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock).addRoutedRpcImplementation(EventSourceService.class, eventSourceMock);
200         doNothing().when(routedRpcRegistrationMock).registerPath(eq(NodeContext.class), any(KeyedInstanceIdentifier.class));
201         assertNotNull("Return value has not been created correctly.", eventSourceTopology.registerEventSource(eventSourceMock));
202     }
203
204     private Map getTopicListenerRegistrations() throws Exception{
205         Field nesField = EventSourceTopology.class.getDeclaredField("topicListenerRegistrations");
206         nesField.setAccessible(true);
207         return (Map) nesField.get(eventSourceTopology);
208     }
209
210     private Map getRoutedRpcRegistrations() throws Exception{
211         Field nesField = EventSourceTopology.class.getDeclaredField("routedRpcRegistrations");
212         nesField.setAccessible(true);
213         return (Map) nesField.get(eventSourceTopology);
214     }
215
216 }