2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.messagebus.app.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import org.junit.Before;
13 import org.junit.BeforeClass;
14 import org.junit.Test;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
20 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
21 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.CreateTopicInput;
22 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.EventAggregatorService;
23 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.DestroyTopicInput;
24 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
25 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.Pattern;
26 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
37 import java.util.ArrayList;
38 import java.util.List;
40 import static org.junit.Assert.assertNotNull;
41 import static org.mockito.Matchers.any;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.times;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.doReturn;
46 import static org.mockito.Mockito.doNothing;
47 import static org.mockito.Mockito.eq;
49 public class EventSourceTopologyTest {
51 EventSourceTopology eventSourceTopology;
52 DataBroker dataBrokerMock;
53 RpcProviderRegistry rpcProviderRegistryMock;
54 CreateTopicInput createTopicInputMock;
55 ListenerRegistration listenerRegistrationMock;
59 public static void initTestClass() throws IllegalAccessException, InstantiationException {
63 public void setUp() throws Exception {
64 dataBrokerMock = mock(DataBroker.class);
65 rpcProviderRegistryMock = mock(RpcProviderRegistry.class);
70 public void constructorTest() {
71 constructorTestHelper();
72 eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock);
73 assertNotNull("Instance has not been created correctly.", eventSourceTopology);
76 private void constructorTestHelper(){
77 WriteTransaction writeTransactionMock = mock(WriteTransaction.class);
78 doReturn(writeTransactionMock).when(dataBrokerMock).newWriteOnlyTransaction();
79 doNothing().when(writeTransactionMock).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class));
80 CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
81 doReturn(checkedFutureMock).when(writeTransactionMock).submit();
85 public void createTopicTest() throws Exception{
86 createTopicTestHelper();
87 assertNotNull("Topic has not been created correctly.", eventSourceTopology.createTopic(createTopicInputMock));
90 private void createTopicTestHelper() throws Exception{
91 constructorTestHelper();
92 createTopicInputMock = mock(CreateTopicInput.class);
93 eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock);
95 NotificationPattern notificationPattern = new NotificationPattern("value1");
96 doReturn(notificationPattern).when(createTopicInputMock).getNotificationPattern();
97 Pattern pattern = new Pattern("valuePattern1");
98 doReturn(pattern).when(createTopicInputMock).getNodeIdPattern();
100 listenerRegistrationMock = mock(ListenerRegistration.class);
101 doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataChangeListener(eq(LogicalDatastoreType.OPERATIONAL),
102 any(InstanceIdentifier.class),
103 any(EventSourceTopic.class),
104 eq(DataBroker.DataChangeScope.SUBTREE));
106 ReadOnlyTransaction readOnlyTransactionMock = mock(ReadOnlyTransaction.class);
107 doReturn(readOnlyTransactionMock).when(dataBrokerMock).newReadOnlyTransaction();
109 CheckedFuture checkedFutureMock = mock(CheckedFuture.class);
110 doReturn(checkedFutureMock).when(readOnlyTransactionMock).read(eq(LogicalDatastoreType.OPERATIONAL),
111 any(InstanceIdentifier.class));
112 Optional optionalMock = mock(Optional.class);
113 doReturn(optionalMock).when(checkedFutureMock).checkedGet();
114 doReturn(true).when(optionalMock).isPresent();
116 Topology topologyMock = mock(Topology.class);
117 doReturn(topologyMock).when(optionalMock).get();
118 Node nodeMock = mock(Node.class);
119 List<Node> nodeList = new ArrayList<>();
120 nodeList.add(nodeMock);
121 doReturn(nodeList).when(topologyMock).getNode();
123 NodeId nodeId = new NodeId("nodeIdValue1");
124 doReturn(nodeId).when(nodeMock).getNodeId();
128 public void destroyTopicTest() throws Exception{
129 createTopicTestHelper();
130 DestroyTopicInput destroyTopicInput = null;
131 assertNotNull("Instance has not been created correctly.", eventSourceTopology.destroyTopic(destroyTopicInput));
135 public void closeTest() throws Exception{
136 BindingAwareBroker.RpcRegistration rpcRegistrationMock = mock(BindingAwareBroker.RpcRegistration.class);
137 doReturn(rpcRegistrationMock).when(rpcProviderRegistryMock).addRpcImplementation(eq(EventAggregatorService.class), any(EventSourceTopology.class));
138 doNothing().when(rpcRegistrationMock).close();
139 createTopicTestHelper();
140 eventSourceTopology.createTopic(createTopicInputMock);
141 eventSourceTopology.close();
142 verify(rpcRegistrationMock, times(1)).close();
146 public void registerTest() throws Exception {
147 createTopicTestHelper();
148 Node nodeMock = mock(Node.class);
149 NetconfEventSource netconfEventSourceMock = mock(NetconfEventSource.class);
151 NodeId nodeId = new NodeId("nodeIdValue1");
152 nodeKey = new NodeKey(nodeId);
153 doReturn(nodeKey).when(nodeMock).getKey();
155 BindingAwareBroker.RoutedRpcRegistration routedRpcRegistrationMock = mock(BindingAwareBroker.RoutedRpcRegistration.class);
156 doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock).addRoutedRpcImplementation(EventSourceService.class, netconfEventSourceMock);
157 eventSourceTopology.register(nodeMock, netconfEventSourceMock);
158 verify(routedRpcRegistrationMock, times(1)).registerPath(eq(NodeContext.class), any(KeyedInstanceIdentifier.class));