Use the memorized service reference
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / NetworkHandlerTest.java
index b123cda6d7809e121ecb18d851beaea29ac4b362..8890cfaa9a1d2d11f584da7fd9d12cd5337cf331 100644 (file)
@@ -22,6 +22,7 @@ import java.net.HttpURLConnection;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
@@ -39,7 +40,7 @@ import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
 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.osgi.framework.ServiceReference;
 import org.powermock.api.mockito.PowerMockito;
 import org.powermock.core.classloader.annotations.PrepareForTest;
@@ -54,7 +55,8 @@ public class NetworkHandlerTest {
 
     @InjectMocks private NetworkHandler networkHandler;
 
-    @Mock private NeutronNetwork neutronNetwork;
+    @Mock private NeutronNetwork sharedNeutronNetwork;
+    @Mock private NeutronNetwork nonSharedNeutronNetwork;
 
     @Mock private NeutronL3Adapter neutronL3Adapter;
     @Mock private TenantNetworkManager tenantNetworkManager;
@@ -63,16 +65,19 @@ public class NetworkHandlerTest {
     @Mock private NodeCacheManager nodeCacheManager;
     @Mock private Southbound southbound;
 
+    @Before
+    public void setup() {
+        when(sharedNeutronNetwork.isShared()).thenReturn(true);
+        when(nonSharedNeutronNetwork.isShared()).thenReturn(false);
+    }
+
     /**
      * Test method {@link NetworkHandler#canCreateNetwork(NeutronNetwork)}
      */
     @Test
     public void testCanCreateNetwork() {
-        when(neutronNetwork.isShared())
-                                    .thenReturn(true)
-                                    .thenReturn(false);
-        assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_NOT_ACCEPTABLE, networkHandler.canCreateNetwork(neutronNetwork));
-        assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, networkHandler.canCreateNetwork(neutronNetwork));
+        assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_NOT_ACCEPTABLE, networkHandler.canCreateNetwork(sharedNeutronNetwork));
+        assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, networkHandler.canCreateNetwork(nonSharedNeutronNetwork));
     }
 
     /**
@@ -80,11 +85,8 @@ public class NetworkHandlerTest {
      */
     @Test
     public void testCanUpdateNetwork() {
-        when(neutronNetwork.isShared())
-                                    .thenReturn(true)
-                                    .thenReturn(false);
-        assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_NOT_ACCEPTABLE, networkHandler.canUpdateNetwork(neutronNetwork, neutronNetwork));
-        assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, networkHandler.canUpdateNetwork(neutronNetwork, neutronNetwork));
+        assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_NOT_ACCEPTABLE, networkHandler.canUpdateNetwork(sharedNeutronNetwork, sharedNeutronNetwork));
+        assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, networkHandler.canUpdateNetwork(nonSharedNeutronNetwork, nonSharedNeutronNetwork));
     }
 
     /**
@@ -92,7 +94,7 @@ public class NetworkHandlerTest {
      */
     @Test
     public void testCanDeleteNetwork() {
-        assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, networkHandler.canDeleteNetwork(neutronNetwork));
+        assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, networkHandler.canDeleteNetwork(nonSharedNeutronNetwork));
     }
 
     /**
@@ -103,7 +105,7 @@ public class NetworkHandlerTest {
         NetworkHandler networkHandlerSpy = Mockito.spy(networkHandler);
 
         NorthboundEvent ev = mock(NorthboundEvent.class);
-        when(ev.getNeutronNetwork()).thenReturn(neutronNetwork);
+        when(ev.getNeutronNetwork()).thenReturn(nonSharedNeutronNetwork);
 
         when(ev.getAction()).thenReturn(Action.ADD);
         networkHandlerSpy.processEvent(ev);
@@ -115,27 +117,27 @@ public class NetworkHandlerTest {
 
         String portName = "portName";
 
-        List<NeutronNetwork> networks = new ArrayList<NeutronNetwork>();
+        List<NeutronNetwork> networks = new ArrayList<>();
         when(neutronNetworkCache.getAllNetworks()).thenReturn(networks);
 
-        List<Node> nodes = new ArrayList<Node>();
+        List<Node> nodes = new ArrayList<>();
         nodes.add(mock(Node.class));
         when(nodeCacheManager.getNodes()).thenReturn(nodes);
 
-        List<String> phyIfName = new ArrayList<String>();
+        List<String> phyIfName = new ArrayList<>();
         phyIfName.add(portName);
         when(bridgeConfigurationManager.getAllPhysicalInterfaceNames(any(Node.class))).thenReturn(phyIfName );
 
-        List<OvsdbTerminationPointAugmentation> ports = new ArrayList<OvsdbTerminationPointAugmentation>();
+        List<OvsdbTerminationPointAugmentation> ports = new ArrayList<>();
         OvsdbTerminationPointAugmentation port = mock(OvsdbTerminationPointAugmentation.class);
         when(port.getName()).thenReturn(portName);
         ports.add(port);
         when(southbound.getTerminationPointsOfBridge(any(Node.class))).thenReturn(ports);
 
-        when(southbound.isTunnel(any(OvsdbTerminationPointAugmentation.class))).thenReturn(true, false);
-
         when(ev.getAction()).thenReturn(Action.DELETE);
+        when(southbound.isTunnel(any(OvsdbTerminationPointAugmentation.class))).thenReturn(true);
         networkHandlerSpy.processEvent(ev); // test delete with southbound.isTunnel(true)
+        when(southbound.isTunnel(any(OvsdbTerminationPointAugmentation.class))).thenReturn(false);
         networkHandlerSpy.processEvent(ev); // test delete with southbound.isTunnel(false)
         // the functions are called once per call to processEvent()
         verify(neutronL3Adapter, times(2)).handleNeutronNetworkEvent(any(NeutronNetwork.class), same(Action.DELETE));
@@ -162,7 +164,7 @@ public class NetworkHandlerTest {
         PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, networkHandler)).thenReturn(southbound);
         PowerMockito.when(ServiceHelper.getGlobalInstance(EventDispatcher.class, networkHandler)).thenReturn(eventDispatcher);
 
-        networkHandler.setDependencies(mock(BundleContext.class), mock(ServiceReference.class));
+        networkHandler.setDependencies(mock(ServiceReference.class));
 
         assertEquals("Error, did not return the correct object", getField("tenantNetworkManager"), tenantNetworkManager);
         assertEquals("Error, did not return the correct object", getField("bridgeConfigurationManager"), bridgeConfigurationManager);