Merge changes I114cbac1,I45c2e7cd
[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.util.ArrayList;
20 import java.util.List;
21
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.controller.messagebus.spi.EventSource;
30 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
31 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
32 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
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.EventAggregatorService;
36 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
37 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.Pattern;
38 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
44 import org.opendaylight.yangtools.concepts.ListenerRegistration;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
48
49 import com.google.common.base.Optional;
50 import com.google.common.util.concurrent.CheckedFuture;
51
52 public class EventSourceTopologyTest {
53
54     EventSourceTopology eventSourceTopology;
55     DataBroker dataBrokerMock;
56     RpcProviderRegistry rpcProviderRegistryMock;
57     CreateTopicInput createTopicInputMock;
58     ListenerRegistration listenerRegistrationMock;
59     NodeKey nodeKey;
60
61     @BeforeClass
62     public static void initTestClass() throws IllegalAccessException, InstantiationException {
63     }
64
65     @Before
66     public void setUp() throws Exception {
67         dataBrokerMock = mock(DataBroker.class);
68         rpcProviderRegistryMock = mock(RpcProviderRegistry.class);
69     }
70
71     @Test
72     public void constructorTest() {
73         constructorTestHelper();
74         eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock);
75         assertNotNull("Instance has not been created correctly.", eventSourceTopology);
76     }
77
78     private void constructorTestHelper(){
79         RpcRegistration<EventAggregatorService> aggregatorRpcReg = mock(RpcRegistration.class);
80         EventSourceService eventSourceService = mock(EventSourceService.class);
81         doReturn(aggregatorRpcReg).when(rpcProviderRegistryMock).addRpcImplementation(eq(EventAggregatorService.class), 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), any(DataObject.class),eq(true));
86         CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
87         doReturn(checkedFutureMock).when(writeTransactionMock).submit();
88     }
89
90 //TODO: create test for createTopic
91 //    public void createTopicTest() throws Exception{
92 //        createTopicTestHelper();
93 //        assertNotNull("Topic has not been created correctly.", eventSourceTopology.createTopic(createTopicInputMock));
94 //    }
95
96     private void topicTestHelper() throws Exception{
97         constructorTestHelper();
98         createTopicInputMock = mock(CreateTopicInput.class);
99         eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock);
100
101         NotificationPattern notificationPattern = new NotificationPattern("value1");
102         doReturn(notificationPattern).when(createTopicInputMock).getNotificationPattern();
103         Pattern pattern = new Pattern("valuePattern1");
104         doReturn(pattern).when(createTopicInputMock).getNodeIdPattern();
105
106         listenerRegistrationMock = mock(ListenerRegistration.class);
107         doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataChangeListener(eq(LogicalDatastoreType.OPERATIONAL),
108                 any(InstanceIdentifier.class),
109                 any(EventSourceTopic.class),
110                 eq(DataBroker.DataChangeScope.SUBTREE));
111
112         ReadOnlyTransaction readOnlyTransactionMock = mock(ReadOnlyTransaction.class);
113         doReturn(readOnlyTransactionMock).when(dataBrokerMock).newReadOnlyTransaction();
114
115         CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
116         doReturn(checkedFutureMock).when(readOnlyTransactionMock).read(eq(LogicalDatastoreType.OPERATIONAL),
117                 any(InstanceIdentifier.class));
118         Optional optionalMock = mock(Optional.class);
119         doReturn(optionalMock).when(checkedFutureMock).checkedGet();
120         doReturn(true).when(optionalMock).isPresent();
121
122         Topology topologyMock = mock(Topology.class);
123         doReturn(topologyMock).when(optionalMock).get();
124         Node nodeMock = mock(Node.class);
125         List<Node> nodeList = new ArrayList<>();
126         nodeList.add(nodeMock);
127         doReturn(nodeList).when(topologyMock).getNode();
128
129         NodeId nodeId = new NodeId("nodeIdValue1");
130         doReturn(nodeId).when(nodeMock).getNodeId();
131     }
132
133     @Test
134     public void destroyTopicTest() throws Exception{
135         topicTestHelper();
136         //TODO: modify test when destroyTopic will be implemented
137         DestroyTopicInput destroyTopicInput = null;
138         assertNotNull("Instance has not been created correctly.", eventSourceTopology.destroyTopic(destroyTopicInput));
139     }
140
141     @Test
142     public void registerTest() throws Exception {
143         topicTestHelper();
144         Node nodeMock = mock(Node.class);
145         EventSource eventSourceMock = mock(EventSource.class);
146         NodeId nodeId = new NodeId("nodeIdValue1");
147         nodeKey = new NodeKey(nodeId);
148         doReturn(nodeKey).when(nodeMock).getKey();
149         doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey();
150         BindingAwareBroker.RoutedRpcRegistration routedRpcRegistrationMock = mock(BindingAwareBroker.RoutedRpcRegistration.class);
151         doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock).addRoutedRpcImplementation(EventSourceService.class, eventSourceMock);
152         doNothing().when(routedRpcRegistrationMock).registerPath(eq(NodeContext.class), any(KeyedInstanceIdentifier.class));
153         eventSourceTopology.register(eventSourceMock);
154         verify(routedRpcRegistrationMock, times(1)).registerPath(eq(NodeContext.class), any(KeyedInstanceIdentifier.class));
155     }
156
157 }