Update netvirt.impl unit tests. 49/17949/2
authorAlexis de Talhouët <adetalhouet@inocybe.com>
Wed, 8 Apr 2015 17:33:58 +0000 (13:33 -0400)
committerAlexis de Talhouët <adetalhouet@inocybe.com>
Wed, 8 Apr 2015 17:37:10 +0000 (17:37 +0000)
Basically I added tests, mainly Mockito.verify(). And I refactored, updated and cleaned up the classes.

Change-Id: I1d3f2c7966a7edb1ad7d41d53cdba1e37cb1082c
Signed-off-by: Alexis de Talhouët <adetalhouet@inocybe.com>
openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/EventDispatcherImplTest.java
openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/NeutronL3AdapterTest.java
openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/ProviderNetworkManagerImplTest.java
openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/SecurityServicesImplTest.java
openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/TenantNetworkManagerImplTest.java
openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/VlanConfigurationCacheImplTest.java

index 66ba75f8d9ca1b9e82668055b6037ab433487486..0796d73f546b4dc89bb38584c0341ab2358455df 100644 (file)
@@ -30,7 +30,7 @@ import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
 import org.osgi.framework.ServiceReference;
 
 /**
- * Unit test for class EventDispatcherImpl
+ * Unit test for {@link EventDispatcherImpl}
  */
 @RunWith(MockitoJUnitRunner.class)
 public class EventDispatcherImplTest {
@@ -39,7 +39,7 @@ public class EventDispatcherImplTest {
     @InjectMocks EventDispatcherImpl eventDispatcherImpl;
 
     private AbstractEvent.HandlerType handlerTypeObject = AbstractEvent.HandlerType.NEUTRON_FLOATING_IP;
-    public ServiceReference ref = mock(ServiceReference.class);
+    private ServiceReference ref = mock(ServiceReference.class);
 
     @Before
     public void setUp() {
@@ -53,10 +53,11 @@ public class EventDispatcherImplTest {
     }
 
     /**
-     * Test method {@link EventDispatcherImpl#eventHandlerAdded(ServiceReference, AbstractHandler)}
+     * Test methods {@link EventDispatcherImpl#eventHandlerRemoved(ServiceReference)}
+     * and {@link EventDispatcherImpl#eventHandlerAdded(ServiceReference, AbstractHandler)}
      */
     @Test
-    public void testeventHandlerAdded() throws Exception{
+    public void testHandlerAddedAndRemoved() throws Exception{
         AbstractHandler[] handlers = ( AbstractHandler[]) getClassField("handlers");
 
         assertNotEquals("Error, handler should be null", handlers[handlerTypeObject.ordinal()], handler);
@@ -64,18 +65,6 @@ public class EventDispatcherImplTest {
         eventDispatcherImpl.eventHandlerAdded(ref, handler);
 
         assertEquals("Error, did not return the added handler", handlers[handlerTypeObject.ordinal()], handler);
-    }
-
-    /**
-     * Test method {@link EventDispatcherImpl#eventHandlerRemoved(ServiceReference)}
-     */
-    @Test
-    public void testHandlerRemoved() throws Exception{
-        AbstractHandler[] handlers = ( AbstractHandler[]) getClassField("handlers");
-
-        eventDispatcherImpl.eventHandlerAdded(ref, handler);
-
-        assertEquals("Error, did not return the added handler", handlers[handlerTypeObject.ordinal()], handler);
 
         eventDispatcherImpl.eventHandlerRemoved(ref);
 
index cbda2c123e580355f38ec8a8ef5275ffd050c892..d269e8424f054400102a002227d2fc4c1354d252 100644 (file)
@@ -42,14 +42,7 @@ import org.opendaylight.ovsdb.lib.notation.Column;
 import org.opendaylight.ovsdb.lib.notation.Row;
 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
-import org.opendaylight.ovsdb.openstack.netvirt.api.ArpProvider;
 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
-import org.opendaylight.ovsdb.openstack.netvirt.api.InboundNatProvider;
-import org.opendaylight.ovsdb.openstack.netvirt.api.L3ForwardingProvider;
-import org.opendaylight.ovsdb.openstack.netvirt.api.MultiTenantAwareRouter;
-import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
-import org.opendaylight.ovsdb.openstack.netvirt.api.OutboundNatProvider;
-import org.opendaylight.ovsdb.openstack.netvirt.api.RoutingProvider;
 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
@@ -69,6 +62,8 @@ import org.powermock.modules.junit4.PowerMockRunner;
 @RunWith(PowerMockRunner.class)
 public class NeutronL3AdapterTest {
 
+    @InjectMocks NeutronL3Adapter neutronL3Adapter;
+
     @Mock private ConfigurationService configurationService;
     @Mock private TenantNetworkManager tenantNetworkManager;
     @Mock private OvsdbConfigurationService ovsdbConfigurationService;
@@ -76,19 +71,7 @@ public class NeutronL3AdapterTest {
     @Mock private INeutronNetworkCRUD neutronNetworkCache;
     @Mock private INeutronSubnetCRUD neutronSubnetCache;
     @Mock private INeutronPortCRUD neutronPortCache;
-
-    @Mock private NeutronPort neutronPort;
-    @Mock private Neutron_IPs neutronIP;
-    @Mock private NeutronRouter neutronRouter;
-    @Mock private NeutronRouter_Interface neutronRouterInterface;
-    @Mock private NeutronSubnet neutronSubnet;
-    @Mock private NeutronNetwork neutronNetwork;
-    @Mock private Node node;
-    @Mock private NodeId nodeID;
-    @Mock private Row row;
-    @Mock private Bridge bridge;
-    @Mock private Status status;
-    @InjectMocks NeutronL3Adapter neutronL3Adapter;
+    @Mock NeutronPort neutronPort;
 
     private Set<String> inboundIpRewriteCache;
     private Set<String> outboundIpRewriteCache;
@@ -134,6 +117,17 @@ public class NeutronL3AdapterTest {
     }
 
     private void setUpVar(){
+        Neutron_IPs neutronIP = mock(Neutron_IPs.class);
+        NeutronRouter neutronRouter = mock(NeutronRouter.class);
+
+        NeutronSubnet neutronSubnet = mock(NeutronSubnet.class);
+        NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
+        Node node = mock(Node.class);
+        NodeId nodeID = mock(NodeId.class);
+        Row row = mock(Row.class);
+        Bridge bridge = mock(Bridge.class);
+        Status status = mock(Status.class);
+
         List<Neutron_IPs> list_neutronIP = new ArrayList<Neutron_IPs>();
         list_neutronIP.add(neutronIP);
 
@@ -159,9 +153,6 @@ public class NeutronL3AdapterTest {
         when(neutronIP.getSubnetUUID()).thenReturn("subnetUUID");
         when(neutronIP.getIpAddress()).thenReturn(HOST_ADDRESS);
 
-        when(neutronRouterInterface.getPortUUID()).thenReturn("portUUID");
-        when(neutronRouterInterface.getSubnetUUID()).thenReturn("subnetUUID");
-
         when(neutronPortCache.getAllPorts()).thenReturn(list_neutronPort);
         when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
 
@@ -252,6 +243,9 @@ public class NeutronL3AdapterTest {
 
         // populate subnetIdToRouterInterfaceCache to pass the
         // if (neutronRouterInterface != null)
+        NeutronRouter_Interface neutronRouterInterface = mock(NeutronRouter_Interface.class);
+        when(neutronRouterInterface.getPortUUID()).thenReturn("portUUID");
+        when(neutronRouterInterface.getSubnetUUID()).thenReturn("subnetUUID");
         subnetIdToRouterInterfaceCache.put("subnetUUID", neutronRouterInterface);
 
         /* device owner = "" */
@@ -325,7 +319,6 @@ public class NeutronL3AdapterTest {
         assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 0, subnetIdToRouterInterfaceCache.size());
         assertEquals("Error, did not return the correct l3ForwardingCache size", 0, l3ForwardingCache.size());
         assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
-
     }
 
 }
index fb93983447d87b761b4789ab77c49170a0c63056..4a4c85e215caf4b996c497db97ef58e7faa83377 100644 (file)
@@ -11,24 +11,20 @@ import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.lang.reflect.Field;
 import java.util.HashMap;
-import java.util.Random;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Spy;
 import org.mockito.runners.MockitoJUnitRunner;
-import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProvider;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.osgi.framework.ServiceReference;
 
 /**
- * Unit test for class ProviderNetworkManagerImpl
- *
- * TODO In order to have deeper tests, private inner class
- *  ProviderEntry of ProviderNetworkManager should be public.
+ * Unit test for {@link ProviderNetworkManagerImpl}
  */
 @RunWith(MockitoJUnitRunner.class)
 public class ProviderNetworkManagerImplTest {
@@ -36,51 +32,40 @@ public class ProviderNetworkManagerImplTest {
     @InjectMocks private ProviderNetworkManagerImpl providerNetworkManagerImpl;
     @Spy private HashMap<Node, NetworkingProvider> nodeToProviderMapping;
 
-    private Random r = new Random();
-
     /**
      * Test method {@link ProviderNetworkManagerImpl#getProvider(Node)}
      */
     @Test
     public void testGetProvider(){
+        // TODO test the method with no networkingProvider in the map
+        // Could not be done as ProviderEntry is a private inner class of ProviderNetworkManagerImpl
+//        assertNotNull("Error, did not return the networkingProvider of the specified node", providerNetworkManagerImpl.getProvider(any(Node.class));
+
         Node node = mock(Node.class);
         NetworkingProvider networkingProvider = mock(NetworkingProvider.class);
-
-        // populate nodeToProviderMapping
         nodeToProviderMapping.put(node, networkingProvider);
-
-        // test when nodeToProviderMapping is populate
         assertEquals("Error, did not return the networkingProvider of the specified node", networkingProvider, providerNetworkManagerImpl.getProvider(node));
     }
 
     /**
-     * Test method {@link ProviderNetworkManagerImpl#providerAdded(ServiceReference, NetworkingProvider)}
+     * Test methods {@link ProviderNetworkManagerImpl#providerRemoved(ServiceReference)}
+     * and {@link ProviderNetworkManagerImpl#providerAdded(ServiceReference, NetworkingProvider)}
      */
     @Test
-    public void testProviderAdded(){
-        ServiceReference ref = mock(ServiceReference.class);
+    public void testProviderAddedAndRemoved() throws Exception {
+        Field field = ProviderNetworkManagerImpl.class.getDeclaredField("providers");
+        field.setAccessible(true);
+        HashMap map = (HashMap) field.get(providerNetworkManagerImpl);
 
-        // configure ref
-        when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(r.nextLong());
-        when(ref.getProperty(Constants.SOUTHBOUND_PROTOCOL_PROPERTY)).thenReturn("proto");
-        when(ref.getProperty(Constants.OPENFLOW_VERSION_PROPERTY)).thenReturn("of");
-        when(ref.getProperty(Constants.PROVIDER_TYPE_PROPERTY)).thenReturn("provider");
+        ServiceReference ref = mock(ServiceReference.class);
+        when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(Long.valueOf(1));
 
-        // nothing much to test
         providerNetworkManagerImpl.providerAdded(ref, mock(NetworkingProvider.class));
-    }
 
-    /**
-     * Test method {@link ProviderNetworkManagerImpl#providerRemoved(ServiceReference)}
-     */
-    @Test
-    public void testProviderRemoved(){
-        ServiceReference ref = mock(ServiceReference.class);
-
-        // configure ref
-        when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(r.nextLong());
+        assertEquals("Error, providerAdded() did not add the provider", 1, map.size());
 
-        // nothing much to test
         providerNetworkManagerImpl.providerRemoved(ref);
+
+        assertEquals("Error, providerRemoved() did not remove the provider", 0, map.size());
     }
 }
index db40bfb2daf9ec66251bb6c64db46163d2d4c5cb..6b3145a2b9e2725e366b8f8f77c06b14e22c2209 100644 (file)
@@ -33,34 +33,34 @@ import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
 
 /**
- * Unit test for class SecurityServicesImpl
+ * Unit test for {@link SecurityServicesImpl}
  */
 @RunWith(MockitoJUnitRunner.class)
 public class SecurityServicesImplTest {
 
-    @Mock private Interface intf;
-    @Mock private NeutronPort neutronPort;
-
     @InjectMocks private SecurityServicesImpl securityServicesImpl;
-    @InjectMocks private INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
+    @InjectMocks private INeutronPortCRUD neutronPortService = mock(INeutronPortCRUD.class);
+
+    @Mock Interface intf;
 
     private List<NeutronSecurityGroup> securityGroups = new ArrayList<NeutronSecurityGroup>();
 
     @Before
     public void setUp(){
+        NeutronPort neutronPort = mock(NeutronPort.class);
+
         Map<String, String> externalIds =new HashMap<String, String>();
         externalIds.put(Constants.EXTERNAL_ID_INTERFACE_ID, "mapValue");
         Column<GenericTableSchema, Map<String, String>> columnMock = mock(Column.class);
 
         securityGroups.add(mock(NeutronSecurityGroup.class));
 
-        // configure interface
         when(intf.getExternalIdsColumn()).thenReturn(columnMock);
         when(columnMock.getData()).thenReturn(externalIds);
 
-        // configure neutronPort
         when(neutronPort.getSecurityGroups()).thenReturn(securityGroups);
-        when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
+        when(neutronPort.getDeviceOwner()).thenReturn("deviceOwner");
+        when(neutronPortService.getPort(anyString())).thenReturn(neutronPort);
     }
 
     /**
@@ -68,10 +68,6 @@ public class SecurityServicesImplTest {
      */
     @Test
     public void testIsPortSecurityReady(){
-        // configure neutronPort
-        when(neutronPort.getDeviceOwner()).thenReturn("deviceOwner");
-
-        //test
         assertTrue("Error, did not return expected boolean for isPortSecurityReady", securityServicesImpl.isPortSecurityReady(intf));
     }
 
@@ -80,7 +76,6 @@ public class SecurityServicesImplTest {
      */
     @Test
     public void testSecurityGroupInPort(){
-        // test
         assertEquals("Error, did not return the good neutronSecurityGroup of securityGroups", securityGroups.toArray()[0], securityServicesImpl.getSecurityGroupInPort(intf));
     }
 }
index 12d6cb1d09b1dc5a465067c0b24e2579616df3dd..0b0ffb02e32ffd8a4003f6ebf048cbac4570dd3f 100644 (file)
@@ -16,10 +16,13 @@ import static org.mockito.Matchers.eq;
 import static org.mockito.Matchers.same;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -40,6 +43,7 @@ import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProvider;
 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
 import org.opendaylight.ovsdb.openstack.netvirt.api.VlanConfigurationCache;
 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
+import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
 import org.opendaylight.ovsdb.plugin.api.Status;
 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
@@ -47,7 +51,7 @@ import org.opendaylight.ovsdb.schema.openvswitch.Port;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 
 /**
- * Unit test for class TenantNetworkManagerImpl
+ * Unit test for {@link TenantNetworkManagerImpl}
  */
 @RunWith(MockitoJUnitRunner.class)
 public class TenantNetworkManagerImplTest {
@@ -56,6 +60,7 @@ public class TenantNetworkManagerImplTest {
     @Mock private INeutronPortCRUD neutronCache;
     @Mock private OvsdbConfigurationService ovsdbConfigurationService;
     @Mock private NetworkingProviderManager networkingProviderManager;
+    @Mock private OvsdbConnectionService ovsdbConnectionService;
 
     @InjectMocks private TenantNetworkManagerImpl tenantNetworkManagerImpl;
     @InjectMocks private INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
@@ -68,13 +73,12 @@ public class TenantNetworkManagerImplTest {
      */
     @Test
     public void testGetInternalVlan() {
-        // configure vlanConfigurationCache
         when(vlanConfigurationCache.getInternalVlan(any(Node.class), eq(NETWORK_ID))).thenReturn(10);
 
-        // test on an existing internal vlan
         assertEquals("Error, did not return the correct internalVlan" , 10, tenantNetworkManagerImpl.getInternalVlan(mock(Node.class), NETWORK_ID));
-        // test on a missing internal vlan
         assertEquals("Error, did not return the correct internalVlan", 0, tenantNetworkManagerImpl.getInternalVlan(mock(Node.class), "unexistingNetwork"));
+
+        verify(vlanConfigurationCache, times(2)).getInternalVlan(any(Node.class), anyString());
     }
 
     /**
@@ -82,13 +86,12 @@ public class TenantNetworkManagerImplTest {
      */
     @Test
     public void testReclaimInternalVlan() {
-        // configure vlanConfigurationCache
         when(vlanConfigurationCache.reclaimInternalVlan(any(Node.class), eq(NETWORK_ID))).thenReturn(10);
 
-        // test on an existing internal vlan
         tenantNetworkManagerImpl.reclaimInternalVlan(mock(Node.class), NETWORK_ID, mock(NeutronNetwork.class));
-        // test on a missing internal vlan
         tenantNetworkManagerImpl.reclaimInternalVlan(mock(Node.class), "unexistingNetwork", mock(NeutronNetwork.class));
+
+        verify(vlanConfigurationCache, times(2)).reclaimInternalVlan(any(Node.class), anyString());
     }
 
     /**
@@ -101,18 +104,16 @@ public class TenantNetworkManagerImplTest {
         GenericTableSchema tableSchema = mock(GenericTableSchema.class);
         Status status = mock(Status.class);
 
-        // configure port
         when(port.getRow()).thenReturn(row);
         when(port.getSchema()).thenReturn(tableSchema);
 
-        // configure vlanConfigurationCache
         when(vlanConfigurationCache.getInternalVlan(any(Node.class), anyString())).thenReturn(10);
-        // configure ovsdbConfigurationService
         when(ovsdbConfigurationService.createTypedRow(any(Node.class), same(Port.class))).thenReturn(port);
         when(ovsdbConfigurationService.updateRow(any(Node.class), anyString(), anyString(), anyString(), any(Row.class))).thenReturn(status);
 
-        // test
         tenantNetworkManagerImpl.programInternalVlan(mock(Node.class), NETWORK_ID, mock(NeutronNetwork.class));
+
+        verify(vlanConfigurationCache, times(1)).getInternalVlan(any(Node.class), anyString());
     }
 
     /**
@@ -139,36 +140,32 @@ public class TenantNetworkManagerImplTest {
         ArrayList<NeutronNetwork> listNeutronNetwork = new ArrayList<NeutronNetwork>();
         listNeutronNetwork.add(neutronNetwork);
 
-        // configure neutronNetwork
         when(neutronNetwork.getProviderSegmentationID()).thenReturn("segId");
         when(neutronNetwork.getNetworkUUID()).thenReturn("networkUUID");
         when(neutronNetworkCache.getAllNetworks()).thenReturn(listNeutronNetwork);
 
-        // test method getNetworkId
         assertEquals("Error, did not return the UUID of the correct network", listNeutronNetwork.get(0).getNetworkUUID(), tenantNetworkManagerImpl.getNetworkId("segId"));
 
-        // configure networkingProvider
         when(networkingProviderManager.getProvider(any(Node.class))).thenReturn(networkingProvider);
         when(networkingProvider.hasPerTenantTunneling()).thenReturn(true);
 
-        // configure vlanConfigurationCache
         when(vlanConfigurationCache.getInternalVlan(any(Node.class), anyString())).thenReturn(10);
 
-        // configure ovsdbConfigurationService
         when(ovsdbConfigurationService.getRows(any(Node.class), anyString())).thenReturn(map);
         when(ovsdbConfigurationService.getTypedRow(any(Node.class), same(Interface.class),
                 any(Row.class))).thenReturn(intf);
 
-        // configure interface
         when(intf.getExternalIdsColumn()).thenReturn(columnMock);
         when(columnMock.getData()).thenReturn(externalIds);
 
-        // configure neutronPort
         when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
         when(neutronPort.getNetworkUUID()).thenReturn("networkUUID");
 
-        // test method isTenantNetworkPresentInNode
         assertTrue("Error, did not return correct boolean for isTenantNetworkPresentInNode", tenantNetworkManagerImpl.isTenantNetworkPresentInNode(mock(Node.class), "segId"));
+
+
+        verify(networkingProviderManager, times(1)).getProvider(any(Node.class));
+        verify(vlanConfigurationCache, times(1)).getInternalVlan(any(Node.class), anyString());
     }
 
     /**
@@ -180,12 +177,13 @@ public class TenantNetworkManagerImplTest {
         ArrayList<NeutronNetwork> listNeutronNetwork = new ArrayList<NeutronNetwork>();
         listNeutronNetwork.add(neutronNetwork);
 
-        // configure neutronNetwork
         when(neutronNetwork.getProviderSegmentationID()).thenReturn("segId");
         when(neutronNetwork.getNetworkUUID()).thenReturn("networkUUID");
         when(neutronNetworkCache.getAllNetworks()).thenReturn(listNeutronNetwork);
 
         assertEquals("Error, did not return the UUID of the correct network", listNeutronNetwork.get(0).getNetworkUUID(), tenantNetworkManagerImpl.getNetworkId("segId"));
+
+        verify(neutronNetworkCache, times(1)).getAllNetworks();
     }
 
     /**
@@ -200,17 +198,17 @@ public class TenantNetworkManagerImplTest {
         NeutronPort neutronPort = mock(NeutronPort.class);
         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
 
-        // configure interface
         when(intf.getExternalIdsColumn()).thenReturn(columnMock);
         when(columnMock.getData()).thenReturn(externalIds);
 
-        // configure neutronPort
         when(neutronPort.getNetworkUUID()).thenReturn("neutronUUID");
         when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
-        // configure neutronNetwork
         when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
 
         assertEquals("Error, did not return the correct tenant", neutronNetwork, tenantNetworkManagerImpl.getTenantNetwork(intf));
+
+        verify(neutronPortCache, times(1)).getPort(anyString());
+        verify(neutronNetworkCache, times(1)).getNetwork(anyString());
     }
 
     /**
@@ -218,9 +216,20 @@ public class TenantNetworkManagerImplTest {
      */
     @Test
     public void testNetworkCreated() {
-        // configure vlanConfigurationCache
+        int numberOfNode = 10;
         when(vlanConfigurationCache.assignInternalVlan(any(Node.class), anyString())).thenReturn(10);
 
+        List<Node> nodes = new ArrayList();
+        for (int i = 0; i<numberOfNode; i++) {
+            nodes.add(mock(Node.class));
+        }
+        when(ovsdbConnectionService.getNodes()).thenReturn(nodes);
+
+        tenantNetworkManagerImpl.networkCreated(NETWORK_ID);
+
+        verify(ovsdbConnectionService, times(1)).getNodes();
+        verify(vlanConfigurationCache, times(numberOfNode)).assignInternalVlan(any(Node.class), anyString());
+
         assertEquals("Error, did not return the correct network id", 10,tenantNetworkManagerImpl.networkCreated(mock(Node.class), NETWORK_ID));
     }
 }
index 6925064c298d2be4fbb230cc683b2204b5e13cec..58d4d1f4ad5130e2b88f59dabb65276afb9b9f6b 100644 (file)
@@ -15,7 +15,6 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import java.util.HashSet;
-import java.util.Random;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -36,7 +35,7 @@ import org.opendaylight.ovsdb.schema.openvswitch.Port;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 
 /**
- * Unit test for class VlanConfigurationCacheImpl
+ * Unit test for {@link VlanConfigurationCacheImpl}
  */
 @RunWith(MockitoJUnitRunner.class)
 public class VlanConfigurationCacheImplTest {
@@ -61,10 +60,8 @@ public class VlanConfigurationCacheImplTest {
         ovsTable = new ConcurrentHashMap<>();
         ovsTable.put(NODE_UUID, row);
 
-        ConcurrentHashMap<String, Row> portRows = ovsTable;
-
         Set<Long> tags = new HashSet<Long>();
-        tags.add(new Random().nextLong());
+        tags.add(Long.valueOf(1));
 
         UUID uuid = mock(UUID.class);
         Set<UUID> uuidSet = new HashSet<>();
@@ -76,24 +73,19 @@ public class VlanConfigurationCacheImplTest {
         Interface iface = mock(Interface.class);
         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
 
-        // configure ovsdbConfigurationService
         when(ovsdbConfigurationService.getRows(any(Node.class), anyString())).thenReturn(ovsTable);
-        when(ovsdbConfigurationService.getRows(any(Node.class), anyString())).thenReturn(portRows);
         when(ovsdbConfigurationService.getTypedRow(any(Node.class), same(Port.class), any(Row.class))).thenReturn(port);
 
-        // configure port
         when(port.getTagColumn()).thenReturn(longColumnMock);
         when(longColumnMock.getData()).thenReturn(tags);
         when(port.getInterfacesColumn()).thenReturn(uuidColumnMock);
         when(uuidColumnMock.getData()).thenReturn(uuidSet);
 
-        // configure ovsdbConfigurationService
         when(ovsdbConfigurationService.getRow(any(Node.class), anyString(), anyString())).thenReturn(row);
         when(ovsdbConfigurationService.getTypedRow(any(Node.class), same(Interface.class), any(Row.class))).thenReturn(iface);
 
-        // configure neutronNetwork
         when(tenantNetworkManagerImpl.getTenantNetwork(any(Interface.class))).thenReturn(neutronNetwork);
-        when(neutronNetwork.getNetworkUUID()).thenReturn("networkUUID");
+        when(neutronNetwork.getNetworkUUID()).thenReturn(NETWORK_ID);
     }
 
     /**
@@ -101,17 +93,23 @@ public class VlanConfigurationCacheImplTest {
      */
     @Test
     public void testAssignInternalVlan() {
-        // test
-        assertEquals("Error, did not return the correct internalVlanId (first added)", 1, (int) vlanConfigurationCacheImpl.assignInternalVlan(mock(Node.class), NETWORK_ID));
-        assertEquals("Error, did not return the correct internalVlanId (second added)",2, (int) vlanConfigurationCacheImpl.assignInternalVlan(mock(Node.class), NETWORK_ID + "1"));
-        }
+        assertEquals("Error, did not return the correct internalVlanId (first added)", 1, (int) vlanConfigurationCacheImpl.assignInternalVlan(any(Node.class), NETWORK_ID));
+        assertEquals("Error, did not return the correct internalVlanId (second added)", 2, (int) vlanConfigurationCacheImpl.assignInternalVlan(any(Node.class), NETWORK_ID + "1"));
+    }
 
     /**
-     * Test method {@linkp VlanConfigurationCacheImpl#reclaimInternalVlan(Node, String)}
+     * Test method {@link VlanConfigurationCacheImpl#reclaimInternalVlan(Node, String)}
      */
     @Test
     public void testReclaimInternalVlan(){
-        // test
-        assertEquals("Error, did not return the correct internalVlanId",0, (int) vlanConfigurationCacheImpl.reclaimInternalVlan(any(Node.class), NETWORK_ID));
+        assertEquals("Error, did not return the correct internalVlanId", 1, (int) vlanConfigurationCacheImpl.reclaimInternalVlan(any(Node.class), NETWORK_ID));
+    }
+
+    /**
+     * Test method {@link VlanConfigurationCacheImpl#getInternalVlan(Node, String)}
+     */
+    @Test
+    public void testGetInternalVlan(){
+        assertEquals("Error, did not return the correct internalVlan", 1, (int) vlanConfigurationCacheImpl.getInternalVlan(any(Node.class), NETWORK_ID));
     }
 }