X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fmessagebus-netconf%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmessagebus%2Feventsources%2Fnetconf%2FNetconfEventSourceManagerTest.java;fp=opendaylight%2Fnetconf%2Fmessagebus-netconf%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmessagebus%2Feventsources%2Fnetconf%2FNetconfEventSourceManagerTest.java;h=c85bf4789ce91c7889f2ed28b3f679e3a4f581b1;hp=0000000000000000000000000000000000000000;hb=277612ebea9b441977cdb8460b2e76090df6f9e8;hpb=23fe9ca678ada6263fec5dd996f4025e4a32fcf5 diff --git a/opendaylight/netconf/messagebus-netconf/src/test/java/org/opendaylight/controller/messagebus/eventsources/netconf/NetconfEventSourceManagerTest.java b/opendaylight/netconf/messagebus-netconf/src/test/java/org/opendaylight/controller/messagebus/eventsources/netconf/NetconfEventSourceManagerTest.java new file mode 100644 index 0000000000..c85bf4789c --- /dev/null +++ b/opendaylight/netconf/messagebus-netconf/src/test/java/org/opendaylight/controller/messagebus/eventsources/netconf/NetconfEventSourceManagerTest.java @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.messagebus.eventsources.netconf; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.notNull; +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.base.Optional; +import com.google.common.util.concurrent.CheckedFuture; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.config.yang.messagebus.netconf.NamespaceToStream; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.MountPoint; +import org.opendaylight.controller.md.sal.binding.api.MountPointService; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; +import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker; +import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; +import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; +import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; +import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService; +import org.opendaylight.controller.messagebus.spi.EventSource; +import org.opendaylight.controller.messagebus.spi.EventSourceRegistration; +import org.opendaylight.controller.messagebus.spi.EventSourceRegistry; +import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams; +import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeFields.ConnectionStatus; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; +import org.opendaylight.yangtools.concepts.ListenerRegistration; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; + +public class NetconfEventSourceManagerTest { + + NetconfEventSourceManager netconfEventSourceManager; + ListenerRegistration listenerRegistrationMock; + DOMMountPointService domMountPointServiceMock; + MountPointService mountPointServiceMock; + EventSourceRegistry eventSourceTopologyMock; + AsyncDataChangeEvent asyncDataChangeEventMock; + RpcProviderRegistry rpcProviderRegistryMock; + EventSourceRegistry eventSourceRegistry; + @BeforeClass + public static void initTestClass() throws IllegalAccessException, InstantiationException { + } + + @Before + public void setUp() throws Exception { + DataBroker dataBrokerMock = mock(DataBroker.class); + DOMNotificationPublishService domNotificationPublishServiceMock = mock(DOMNotificationPublishService.class); + domMountPointServiceMock = mock(DOMMountPointService.class); + mountPointServiceMock = mock(MountPointService.class); + eventSourceTopologyMock = mock(EventSourceRegistry.class); + rpcProviderRegistryMock = mock(RpcProviderRegistry.class); + eventSourceRegistry = mock(EventSourceRegistry.class); + List namespaceToStreamList = new ArrayList<>(); + + listenerRegistrationMock = mock(ListenerRegistration.class); + doReturn(listenerRegistrationMock).when(dataBrokerMock).registerDataChangeListener(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), any(NetconfEventSourceManager.class), eq( + AsyncDataBroker.DataChangeScope.SUBTREE)); + + Optional optionalDomMountServiceMock = (Optional) mock(Optional.class); + doReturn(true).when(optionalDomMountServiceMock).isPresent(); + doReturn(optionalDomMountServiceMock).when(domMountPointServiceMock).getMountPoint((YangInstanceIdentifier)notNull()); + + DOMMountPoint domMountPointMock = mock(DOMMountPoint.class); + doReturn(domMountPointMock).when(optionalDomMountServiceMock).get(); + + + Optional optionalBindingMountMock = mock(Optional.class); + doReturn(true).when(optionalBindingMountMock).isPresent(); + + MountPoint mountPointMock = mock(MountPoint.class); + doReturn(optionalBindingMountMock).when(mountPointServiceMock).getMountPoint(any(InstanceIdentifier.class)); + doReturn(mountPointMock).when(optionalBindingMountMock).get(); + + Optional optionalMpDataBroker = mock(Optional.class); + DataBroker mpDataBroker = mock(DataBroker.class); + doReturn(optionalMpDataBroker).when(mountPointMock).getService(DataBroker.class); + doReturn(true).when(optionalMpDataBroker).isPresent(); + doReturn(mpDataBroker).when(optionalMpDataBroker).get(); + + ReadOnlyTransaction rtx = mock(ReadOnlyTransaction.class); + doReturn(rtx).when(mpDataBroker).newReadOnlyTransaction(); + CheckedFuture, ReadFailedException> checkFeature = (CheckedFuture, ReadFailedException>)mock(CheckedFuture.class); + InstanceIdentifier pathStream = InstanceIdentifier.builder(Netconf.class).child(Streams.class).build(); + doReturn(checkFeature).when(rtx).read(LogicalDatastoreType.OPERATIONAL, pathStream); + Optional avStreams = NetconfTestUtils.getAvailableStream("stream01", true); + doReturn(avStreams).when(checkFeature).checkedGet(); + + EventSourceRegistration esrMock = mock(EventSourceRegistration.class); + + netconfEventSourceManager = + NetconfEventSourceManager + .create(dataBrokerMock, domNotificationPublishServiceMock, domMountPointServiceMock, + mountPointServiceMock, eventSourceRegistry, namespaceToStreamList); + } + + @Test + public void onDataChangedCreateEventSourceTestByCreateEntry() throws Exception { + onDataChangedTestHelper(true,false,true, NetconfTestUtils.notification_capability_prefix); + netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock); + verify(eventSourceRegistry, times(1)).registerEventSource(any(EventSource.class)); + } + + @Test + public void onDataChangedCreateEventSourceTestByUpdateEntry() throws Exception { + onDataChangedTestHelper(false,true,true, NetconfTestUtils.notification_capability_prefix); + netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock); + verify(eventSourceRegistry, times(1)).registerEventSource(any(EventSource.class)); + } + + @Test + public void onDataChangedCreateEventSourceTestNotNeconf() throws Exception { + onDataChangedTestHelper(false,true,false, NetconfTestUtils.notification_capability_prefix); + netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock); + verify(eventSourceRegistry, times(0)).registerEventSource(any(EventSource.class)); + } + + @Test + public void onDataChangedCreateEventSourceTestNotNotificationCapability() throws Exception { + onDataChangedTestHelper(true,false,true,"bad-prefix"); + netconfEventSourceManager.onDataChanged(asyncDataChangeEventMock); + verify(eventSourceRegistry, times(0)).registerEventSource(any(EventSource.class)); + } + + private void onDataChangedTestHelper(boolean create, boolean update, boolean isNetconf, String notificationCapabilityPrefix) throws Exception{ + asyncDataChangeEventMock = mock(AsyncDataChangeEvent.class); + Map mapCreate = new HashMap<>(); + Map mapUpdate = new HashMap<>(); + + Node node01; + String nodeId = "Node01"; + doReturn(mapCreate).when(asyncDataChangeEventMock).getCreatedData(); + doReturn(mapUpdate).when(asyncDataChangeEventMock).getUpdatedData(); + + if(isNetconf){ + node01 = NetconfTestUtils + .getNetconfNode(nodeId, "node01.test.local", ConnectionStatus.Connected, notificationCapabilityPrefix); + + } else { + node01 = NetconfTestUtils.getNode(nodeId); + } + + if(create){ + mapCreate.put(NetconfTestUtils.getInstanceIdentifier(node01), node01); + } + if(update){ + mapUpdate.put(NetconfTestUtils.getInstanceIdentifier(node01), node01); + } + + } + +} \ No newline at end of file