X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fmessagebus-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmessagebus%2Fapp%2Fimpl%2FEventSourceTopologyTest.java;h=ce3e52de2b2dac9d933812cc8dca0343dc755390;hb=2611e6a728e586ea34dd891f30a473bf54d6cbd8;hp=c2f6ef54bfdae145a2be44cdec65bf5c75f9edc3;hpb=f84f3b8fb24b1ee2074fbbb3f5c64e3ba3191e3e;p=controller.git diff --git a/opendaylight/md-sal/messagebus-impl/src/test/java/org/opendaylight/controller/messagebus/app/impl/EventSourceTopologyTest.java b/opendaylight/md-sal/messagebus-impl/src/test/java/org/opendaylight/controller/messagebus/app/impl/EventSourceTopologyTest.java index c2f6ef54bf..ce3e52de2b 100644 --- a/opendaylight/md-sal/messagebus-impl/src/test/java/org/opendaylight/controller/messagebus/app/impl/EventSourceTopologyTest.java +++ b/opendaylight/md-sal/messagebus-impl/src/test/java/org/opendaylight/controller/messagebus/app/impl/EventSourceTopologyTest.java @@ -7,90 +7,111 @@ */ package org.opendaylight.controller.messagebus.app.impl; -import com.google.common.base.Optional; -import com.google.common.util.concurrent.CheckedFuture; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import com.google.common.util.concurrent.FluentFuture; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; -import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; -import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.controller.messagebus.spi.EventSource; +import org.opendaylight.mdsal.binding.api.DataBroker; +import org.opendaylight.mdsal.binding.api.DataTreeIdentifier; +import org.opendaylight.mdsal.binding.api.ReadTransaction; +import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry; +import org.opendaylight.mdsal.binding.api.RpcProviderService; +import org.opendaylight.mdsal.binding.api.WriteTransaction; +import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.CreateTopicInput; -import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.EventAggregatorService; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.DestroyTopicInput; +import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.DestroyTopicInputBuilder; +import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.EventAggregatorService; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.Pattern; +import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceService; -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey; import org.opendaylight.yangtools.concepts.ListenerRegistration; +import org.opendaylight.yangtools.concepts.ObjectRegistration; +import org.opendaylight.yangtools.concepts.Registration; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; - -import java.util.ArrayList; -import java.util.List; - -import static org.junit.Assert.assertNotNull; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.eq; public class EventSourceTopologyTest { EventSourceTopology eventSourceTopology; DataBroker dataBrokerMock; - RpcProviderRegistry rpcProviderRegistryMock; + RpcProviderService rpcProviderRegistryMock; + RpcConsumerRegistry rpcServiceMock; CreateTopicInput createTopicInputMock; - ListenerRegistration listenerRegistrationMock; + ListenerRegistration listenerRegistrationMock; NodeKey nodeKey; - - @BeforeClass - public static void initTestClass() throws IllegalAccessException, InstantiationException { - } + ObjectRegistration aggregatorRpcReg; @Before - public void setUp() throws Exception { + public void setUp() { dataBrokerMock = mock(DataBroker.class); - rpcProviderRegistryMock = mock(RpcProviderRegistry.class); - + rpcProviderRegistryMock = mock(RpcProviderService.class); + rpcServiceMock = mock(RpcConsumerRegistry.class); } @Test public void constructorTest() { constructorTestHelper(); - eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock); + eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock, rpcServiceMock); assertNotNull("Instance has not been created correctly.", eventSourceTopology); } - private void constructorTestHelper(){ + private void constructorTestHelper() { + aggregatorRpcReg = mock(ObjectRegistration.class); + EventSourceService eventSourceService = mock(EventSourceService.class); + doReturn(aggregatorRpcReg).when(rpcProviderRegistryMock).registerRpcImplementation( + eq(EventAggregatorService.class), any(EventSourceTopology.class)); + doReturn(eventSourceService).when(rpcServiceMock).getRpcService(EventSourceService.class); WriteTransaction writeTransactionMock = mock(WriteTransaction.class); doReturn(writeTransactionMock).when(dataBrokerMock).newWriteOnlyTransaction(); - doNothing().when(writeTransactionMock).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class)); - CheckedFuture checkedFutureMock = mock(CheckedFuture.class); - doReturn(checkedFutureMock).when(writeTransactionMock).submit(); + doNothing().when(writeTransactionMock).mergeParentStructurePut(any(LogicalDatastoreType.class), + any(InstanceIdentifier.class), any(DataObject.class)); + FluentFuture checkedFutureMock = mock(FluentFuture.class); + doReturn(checkedFutureMock).when(writeTransactionMock).commit(); } @Test - public void createTopicTest() throws Exception{ - createTopicTestHelper(); + public void createTopicTest() throws Exception { + topicTestHelper(); assertNotNull("Topic has not been created correctly.", eventSourceTopology.createTopic(createTopicInputMock)); } - private void createTopicTestHelper() throws Exception{ + @Test + public void destroyTopicTest() throws Exception { + topicTestHelper(); + TopicId topicId = new TopicId("topic-id-007"); + Map localMap = eventSourceTopology.getEventSourceTopicMap(); + EventSourceTopic eventSourceTopic = EventSourceTopic.create(new NotificationPattern("foo"), + "pattern", eventSourceTopology); + localMap.put(topicId, eventSourceTopic); + DestroyTopicInput input = new DestroyTopicInputBuilder().setTopicId(topicId).build(); + eventSourceTopology.destroyTopic(input); + verify(listenerRegistrationMock, times(1)).close(); + } + + private void topicTestHelper() throws Exception { constructorTestHelper(); createTopicInputMock = mock(CreateTopicInput.class); - eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock); + eventSourceTopology = new EventSourceTopology(dataBrokerMock, rpcProviderRegistryMock, rpcServiceMock); NotificationPattern notificationPattern = new NotificationPattern("value1"); doReturn(notificationPattern).when(createTopicInputMock).getNotificationPattern(); @@ -98,23 +119,18 @@ public class EventSourceTopologyTest { doReturn(pattern).when(createTopicInputMock).getNodeIdPattern(); listenerRegistrationMock = mock(ListenerRegistration.class); - doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataChangeListener(eq(LogicalDatastoreType.OPERATIONAL), - any(InstanceIdentifier.class), - any(EventSourceTopic.class), - eq(DataBroker.DataChangeScope.SUBTREE)); + doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataTreeChangeListener( + any(DataTreeIdentifier.class), any(EventSourceTopic.class)); - ReadOnlyTransaction readOnlyTransactionMock = mock(ReadOnlyTransaction.class); + ReadTransaction readOnlyTransactionMock = mock(ReadTransaction.class); doReturn(readOnlyTransactionMock).when(dataBrokerMock).newReadOnlyTransaction(); - CheckedFuture checkedFutureMock = mock(CheckedFuture.class); + FluentFuture checkedFutureMock = mock(FluentFuture.class); doReturn(checkedFutureMock).when(readOnlyTransactionMock).read(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class)); - Optional optionalMock = mock(Optional.class); - doReturn(optionalMock).when(checkedFutureMock).checkedGet(); - doReturn(true).when(optionalMock).isPresent(); - Topology topologyMock = mock(Topology.class); - doReturn(topologyMock).when(optionalMock).get(); + doReturn(Optional.of(topologyMock)).when(checkedFutureMock).get(); + Node nodeMock = mock(Node.class); List nodeList = new ArrayList<>(); nodeList.add(nodeMock); @@ -125,37 +141,64 @@ public class EventSourceTopologyTest { } @Test - public void destroyTopicTest() throws Exception{ - createTopicTestHelper(); - DestroyTopicInput destroyTopicInput = null; - assertNotNull("Instance has not been created correctly.", eventSourceTopology.destroyTopic(destroyTopicInput)); - } - - @Test - public void closeTest() throws Exception{ - BindingAwareBroker.RpcRegistration rpcRegistrationMock = mock(BindingAwareBroker.RpcRegistration.class); - doReturn(rpcRegistrationMock).when(rpcProviderRegistryMock).addRpcImplementation(eq(EventAggregatorService.class), any(EventSourceTopology.class)); - doNothing().when(rpcRegistrationMock).close(); - createTopicTestHelper(); - eventSourceTopology.createTopic(createTopicInputMock); + public void closeTest() throws Exception { + constructorTestHelper(); + topicTestHelper(); + Map localMap = eventSourceTopology.getEventSourceTopicMap(); + TopicId topicIdMock = mock(TopicId.class); + EventSourceTopic eventSourceTopic = EventSourceTopic.create(new NotificationPattern("foo"), + "pattern", eventSourceTopology); + localMap.put(topicIdMock, eventSourceTopic); eventSourceTopology.close(); - verify(rpcRegistrationMock, times(1)).close(); + verify(aggregatorRpcReg, times(1)).close(); + verify(listenerRegistrationMock, times(1)).close(); } @Test public void registerTest() throws Exception { - createTopicTestHelper(); + topicTestHelper(); Node nodeMock = mock(Node.class); - NetconfEventSource netconfEventSourceMock = mock(NetconfEventSource.class); - + EventSource eventSourceMock = mock(EventSource.class); NodeId nodeId = new NodeId("nodeIdValue1"); nodeKey = new NodeKey(nodeId); - doReturn(nodeKey).when(nodeMock).getKey(); + doReturn(nodeKey).when(nodeMock).key(); + doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey(); + ObjectRegistration routedRpcRegistrationMock = mock(ObjectRegistration.class); + doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock).registerRpcImplementation( + eq(EventSourceService.class), eq(eventSourceMock), any(Set.class)); + eventSourceTopology.register(eventSourceMock); + verify(rpcProviderRegistryMock, times(1)).registerRpcImplementation(eq(EventSourceService.class), + eq(eventSourceMock), any(Set.class)); + } - BindingAwareBroker.RoutedRpcRegistration routedRpcRegistrationMock = mock(BindingAwareBroker.RoutedRpcRegistration.class); - doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock).addRoutedRpcImplementation(EventSourceService.class, netconfEventSourceMock); - eventSourceTopology.register(nodeMock, netconfEventSourceMock); - verify(routedRpcRegistrationMock, times(1)).registerPath(eq(NodeContext.class), any(KeyedInstanceIdentifier.class)); + @Test + public void unregisterTest() throws Exception { + topicTestHelper(); + EventSource eventSourceMock = mock(EventSource.class); + NodeId nodeId = new NodeId("nodeIdValue1"); + nodeKey = new NodeKey(nodeId); + Map localMap = eventSourceTopology.getRoutedRpcRegistrations(); + NodeKey nodeKeyMock = mock(NodeKey.class); + doReturn(nodeKeyMock).when(eventSourceMock).getSourceNodeKey(); + ObjectRegistration routedRpcRegistrationMock = mock(ObjectRegistration.class); + localMap.put(nodeKeyMock, routedRpcRegistrationMock); + eventSourceTopology.unRegister(eventSourceMock); + verify(routedRpcRegistrationMock, times(1)).close(); } + @Test + public void registerEventSourceTest() throws Exception { + topicTestHelper(); + Node nodeMock = mock(Node.class); + EventSource eventSourceMock = mock(EventSource.class); + NodeId nodeId = new NodeId("nodeIdValue1"); + nodeKey = new NodeKey(nodeId); + doReturn(nodeKey).when(nodeMock).key(); + doReturn(nodeKey).when(eventSourceMock).getSourceNodeKey(); + ObjectRegistration routedRpcRegistrationMock = mock(ObjectRegistration.class); + doReturn(routedRpcRegistrationMock).when(rpcProviderRegistryMock) + .registerRpcImplementation(eq(EventSourceService.class), eq(eventSourceMock), any(Set.class)); + assertNotNull("Return value has not been created correctly.", + eventSourceTopology.registerEventSource(eventSourceMock)); + } }