X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openstack%2Fnet-virt%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fovsdb%2Fopenstack%2Fnetvirt%2Fimpl%2FNodeCacheManagerImplTest.java;h=95a5ef5b6f20455067d6a7490e0fc268f90e51ab;hb=40e96665e15a45826ee3a15a5b4be520ea01c893;hp=23114ec845672ee796b873b2ce007d0db4b821cb;hpb=05aae7c5c4373ce25b4225208a985fbfb310228f;p=ovsdb.git diff --git a/openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/NodeCacheManagerImplTest.java b/openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/NodeCacheManagerImplTest.java index 23114ec84..95a5ef5b6 100644 --- a/openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/NodeCacheManagerImplTest.java +++ b/openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/NodeCacheManagerImplTest.java @@ -8,21 +8,32 @@ package org.opendaylight.ovsdb.openstack.netvirt.impl; import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.lang.reflect.Field; import java.util.Map; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.Spy; import org.opendaylight.ovsdb.openstack.netvirt.NodeCacheManagerEvent; import org.opendaylight.ovsdb.openstack.netvirt.api.Action; +import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher; import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheListener; -//import org.opendaylight.ovsdb.utils.mdsal.node.NodeUtils; +import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound; +import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation; +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.Node; + import org.osgi.framework.ServiceReference; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import com.google.common.collect.Maps; @@ -30,29 +41,29 @@ import com.google.common.collect.Maps; /** * Unit test for {@link NodeCacheManagerImpl} */ -/* TODO SB_MIGRATION */ @Ignore @RunWith(PowerMockRunner.class) -//@PrepareForTest(NodeUtils.class) +@PrepareForTest(ServiceHelper.class) public class NodeCacheManagerImplTest { - @InjectMocks NodeCacheManagerImpl nodeCacheManagerImpl; + @InjectMocks private NodeCacheManagerImpl nodeCacheManagerImpl; @Spy private Map handlers = Maps.newHashMap(); + @Mock private Southbound southbound; + @Test public void testProcessEvent() { NodeCacheManagerEvent ev = mock(NodeCacheManagerEvent.class); - when(ev.getNodeIdentifier()).thenReturn("node_identifier"); + Node node = mock(Node.class); + when(node.getNodeId()).thenReturn(mock(NodeId.class)); + when(ev.getNode()).thenReturn(node); - //PowerMockito.mockStatic(NodeUtils.class); - //when(NodeUtils.getOpenFlowNode(anyString())).thenReturn(mock(Node.class)); - - when(ev.getAction()).thenReturn(Action.ADD); + when(ev.getAction()).thenReturn(Action.UPDATE); nodeCacheManagerImpl.processEvent(ev); - assertEquals("Error, did not add the event", 1, nodeCacheManagerImpl.getBridgeNodes().size()); + assertEquals("Error, did not delete the event", 1, nodeCacheManagerImpl.getNodes().size()); when(ev.getAction()).thenReturn(Action.DELETE); nodeCacheManagerImpl.processEvent(ev); - assertEquals("Error, did not delete the event", 0, nodeCacheManagerImpl.getBridgeNodes().size()); + assertEquals("Error, did not delete the event", 0, nodeCacheManagerImpl.getNodes().size()); } @Test @@ -67,4 +78,66 @@ public class NodeCacheManagerImplTest { nodeCacheManagerImpl.cacheListenerRemoved(ref); assertEquals("Error, cacheListenerAdded() did not remove any listener", 0, handlers.size()); } + + @Test + public void testGetOvsdbNodes() { + addItem(); + + when(southbound.extractOvsdbNode(any(Node.class))).thenReturn(mock(OvsdbNodeAugmentation.class)); + + assertEquals("Error, getOvsdbNodes() did not return the correct value", 1, nodeCacheManagerImpl.getOvsdbNodes().size()); + } + + @Test + public void testGetBridgeNodes() { + addItem(); + + when(southbound.getBridge(any(Node.class))).thenReturn(mock(OvsdbBridgeAugmentation.class)); + + assertEquals("Error, getBridgeNodes() did not return the correct value", 1, nodeCacheManagerImpl.getBridgeNodes().size()); + } + + @Test + public void testGetNodes() { + addItem(); + + assertEquals("Error, getNodes() did not return the correct value", 1, nodeCacheManagerImpl.getNodes().size()); + } + + private void addItem() { + NodeCacheManagerEvent ev = mock(NodeCacheManagerEvent.class); + Node node = mock(Node.class); + when(node.getNodeId()).thenReturn(mock(NodeId.class)); + when(ev.getNode()).thenReturn(node); + + when(ev.getAction()).thenReturn(Action.UPDATE); + nodeCacheManagerImpl.processEvent(ev); + } + + @Test + public void testSetDependencies() throws Exception { + Southbound southbound = mock(Southbound.class); + EventDispatcher eventDispatcher = mock(EventDispatcher.class); + + PowerMockito.mockStatic(ServiceHelper.class); + PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, nodeCacheManagerImpl)).thenReturn(southbound); + PowerMockito.when(ServiceHelper.getGlobalInstance(EventDispatcher.class, nodeCacheManagerImpl)).thenReturn(eventDispatcher); + + nodeCacheManagerImpl.setDependencies(mock(ServiceReference.class)); + + assertEquals("Error, did not return the correct object", getField("southbound"), southbound); + assertEquals("Error, did not return the correct object", getSuperField("eventDispatcher"), eventDispatcher); + } + + private Object getField(String fieldName) throws Exception { + Field field = NodeCacheManagerImpl.class.getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(nodeCacheManagerImpl); + } + + private Object getSuperField(String fieldName) throws Exception { + Field field = NodeCacheManagerImpl.class.getSuperclass().getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(nodeCacheManagerImpl); + } }