BUG-4892:fixed Not getting Arp flows if there is no Router
[ovsdb.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / PortHandlerTest.java
index db98417f9a2324e7bd8a47119b511f3a2471db30..f5f0d9ae77a1a0f328a9ffd8775ede14e7cbc05c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Inocybe and others.  All rights reserved.
+ * Copyright (c) 2015, 2016 Inocybe 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,
@@ -26,31 +26,31 @@ import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
-import org.opendaylight.neutron.spi.NeutronPort;
+import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
+import org.opendaylight.ovsdb.openstack.netvirt.impl.DistributedArpService;
 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
+import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronPort;
 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
-import org.osgi.framework.BundleContext;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105
+        .OvsdbTerminationPointAugmentation;
+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;
 
 /**
  * Unit test fort {@link PortHandler}
  */
-@RunWith(PowerMockRunner.class)
-@PrepareForTest(ServiceHelper.class)
+@RunWith(MockitoJUnitRunner.class)
 public class PortHandlerTest {
 
     @InjectMocks private PortHandler portHandler;
 
     @Mock private NeutronL3Adapter neutronL3Adapter;
+    @Mock private DistributedArpService distributedArpService;
     @Mock private NodeCacheManager nodeCacheManager;
     @Mock private Southbound southbound;
 
@@ -84,17 +84,19 @@ public class PortHandlerTest {
 
         when(ev.getAction()).thenReturn(Action.ADD);
         portHandlerSpy.processEvent(ev);
+        verify(distributedArpService, times(1)).handlePortEvent(neutronPort, Action.ADD);
         verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.ADD);
 
         when(ev.getAction()).thenReturn(Action.UPDATE);
         portHandlerSpy.processEvent(ev);
+        verify(distributedArpService, times(1)).handlePortEvent(neutronPort, Action.UPDATE);
         verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.UPDATE);
 
-        List<Node> nodes = new ArrayList<Node>();
+        List<Node> nodes = new ArrayList<>();
         nodes.add(mock(Node.class));
         when(nodeCacheManager.getNodes()).thenReturn(nodes);
 
-        List<OvsdbTerminationPointAugmentation> ports = new ArrayList<OvsdbTerminationPointAugmentation>();
+        List<OvsdbTerminationPointAugmentation> ports = new ArrayList<>();
         OvsdbTerminationPointAugmentation port = mock(OvsdbTerminationPointAugmentation.class);
         ports.add(port);
         when(southbound.getTerminationPointsOfBridge(any(Node.class))).thenReturn(ports);
@@ -103,6 +105,7 @@ public class PortHandlerTest {
 
         when(ev.getAction()).thenReturn(Action.DELETE);
         portHandlerSpy.processEvent(ev);
+        verify(distributedArpService, times(1)).handlePortEvent(neutronPort, Action.DELETE);
         verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.DELETE);
         verify(southbound, times(1)).deleteTerminationPoint(any(Node.class), anyString());
     }
@@ -110,19 +113,21 @@ public class PortHandlerTest {
     @Test
     public void testSetDependencies() throws Exception {
         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
+        DistributedArpService distributedArpService = mock(DistributedArpService.class);
         NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
         Southbound southbound = mock(Southbound.class);
         EventDispatcher eventDispatcher = mock(EventDispatcher.class);
 
-        PowerMockito.mockStatic(ServiceHelper.class);
-        PowerMockito.when(ServiceHelper.getGlobalInstance(NodeCacheManager.class, portHandler)).thenReturn(nodeCacheManager);
-        PowerMockito.when(ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, portHandler)).thenReturn(neutronL3Adapter);
-        PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, portHandler)).thenReturn(southbound);
-        PowerMockito.when(ServiceHelper.getGlobalInstance(EventDispatcher.class, portHandler)).thenReturn(eventDispatcher);
+        ServiceHelper.overrideGlobalInstance(NodeCacheManager.class, nodeCacheManager);
+        ServiceHelper.overrideGlobalInstance(DistributedArpService.class, distributedArpService);
+        ServiceHelper.overrideGlobalInstance(NeutronL3Adapter.class, neutronL3Adapter);
+        ServiceHelper.overrideGlobalInstance(Southbound.class, southbound);
+        ServiceHelper.overrideGlobalInstance(EventDispatcher.class, eventDispatcher);
 
-        portHandler.setDependencies(mock(BundleContext.class), mock(ServiceReference.class));
+        portHandler.setDependencies(mock(ServiceReference.class));
 
         assertEquals("Error, did not return the correct object", getField("nodeCacheManager"), nodeCacheManager);
+        assertEquals("Error, did not return the correct object", getField("distributedArpService"), distributedArpService);
         assertEquals("Error, did not return the correct object", getField("neutronL3Adapter"), neutronL3Adapter);
         assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
         assertEquals("Error, did not return the correct object", portHandler.eventDispatcher, eventDispatcher);