Merge "Unit test for ovsdb.southbound.ovsdb.transact"
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / impl / BridgeConfigurationManagerImplTest.java
index bf57d6c772f512acfe67ea617cad2239f73342d8..dec0f7358e6e5f2a0ada969bf58375130711da19 100644 (file)
@@ -7,38 +7,45 @@
 */
 package org.opendaylight.ovsdb.openstack.netvirt.impl;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertEquals;
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyList;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.RETURNS_MOCKS;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.RETURNS_MOCKS;
 import static org.mockito.Mockito.reset;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
-import static org.powermock.api.mockito.PowerMockito.mockStatic;
 
+import java.lang.reflect.Field;
 import java.util.List;
-import org.junit.runner.RunWith;
+
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
-import org.opendaylight.neutron.spi.NeutronNetwork;
+import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronNetwork;
 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
+import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
+import org.opendaylight.ovsdb.utils.config.ConfigProperties;
+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.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;
@@ -47,10 +54,11 @@ import org.powermock.modules.junit4.PowerMockRunner;
  * Test class for BridgeConfigurationManagerImpl
  *
  * @author Marcus Koontz
- * @author Alexis Adetalhouet
+ * @author Alexis de Talhouet
  * @author Sam Hague (shague@redhat.com)
  */
 @RunWith(PowerMockRunner.class)
+@PrepareForTest({ServiceHelper.class, ConfigProperties.class})
 public class BridgeConfigurationManagerImplTest {
     @Mock private Node node;
     @Mock private OvsdbBridgeAugmentation bridge;
@@ -60,6 +68,7 @@ public class BridgeConfigurationManagerImplTest {
     @Mock private Southbound southbound;
     @InjectMocks public static BridgeConfigurationManagerImpl bridgeConfigurationManagerImpl;
 
+    private static final String ADDRESS = "127.0.0.1";
     private static final String BR_INT = "br-int";
     private static final String ETH1 = "eth1";
     private static final String ETH2 = "eth2";
@@ -147,27 +156,29 @@ public class BridgeConfigurationManagerImplTest {
 
     @Test
     public void testIsNodeTunnelReady() throws Exception {
-        when(southbound.getBridge(any(Node.class), anyString()))
-                .thenReturn(null)
-                .thenReturn(bridge);
+        when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
+                .thenReturn(false)
+                .thenReturn(true);
 
         verifyNoMoreInteractions(configurationService);
         assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
-                bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
+                bridgeConfigurationManagerImpl.isNodeTunnelReady(node, node));
 
+        when(configurationService.isL3ForwardingEnabled()).thenReturn(false);
         when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
-        assertTrue("Error, did not return correct boolean from isNodeNeutronReady",
-                bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
+        assertTrue("Error, did not return correct boolean from isNodeTunnelReady",
+                bridgeConfigurationManagerImpl.isNodeTunnelReady(node, node));
 
-        verify(configurationService, times(2)).getIntegrationBridgeName();
-        verify(southbound, times(2)).getBridge(any(Node.class), anyString());
+        verify(configurationService, times(1)).isL3ForwardingEnabled();
+        verify(configurationService, times(3)).getIntegrationBridgeName();
+        verify(southbound, times(2)).isBridgeOnOvsdbNode(any(Node.class), anyString());
     }
 
     @Test
     public void testIsNodeVlanReady() throws Exception {
-        when(southbound.getBridge(any(Node.class), anyString()))
-                .thenReturn(null)
-                .thenReturn(bridge);
+        when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
+                .thenReturn(false)
+                .thenReturn(true);
 
         when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString()))
                 .thenReturn(null)
@@ -177,7 +188,7 @@ public class BridgeConfigurationManagerImplTest {
 
         verifyNoMoreInteractions(configurationService);
         assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
-                bridgeConfigurationManagerImpl.isNodeVlanReady(node, neutronNetwork));
+                bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
 
         BridgeConfigurationManagerImpl bridgeConfigurationManagerImplSpy =
                 PowerMockito.spy(new BridgeConfigurationManagerImpl());
@@ -186,43 +197,69 @@ public class BridgeConfigurationManagerImplTest {
         bridgeConfigurationManagerImplSpy.setSouthbound(southbound);
 
         assertFalse("Error, did not return correct boolean from isNodeVlanReady",
-                bridgeConfigurationManagerImpl.isNodeVlanReady(node, neutronNetwork));
+                bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
 
         assertTrue("Error, did not return correct boolean from isNodeVlanReady",
-                bridgeConfigurationManagerImpl.isNodeVlanReady(node, neutronNetwork));
+                bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
 
         verify(configurationService, times(3)).getIntegrationBridgeName();
         verify(neutronNetwork, times(2)).getProviderPhysicalNetwork();
     }
 
+    @Test
+    public void testIsNodeL3Ready() {
+        when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString())).thenReturn(true);
+        when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString())).thenReturn(mock(OvsdbTerminationPointAugmentation.class));
+        when(southbound.readBridgeNode(any(Node.class), anyString())).thenReturn(mock(Node.class));
+
+        assertTrue("Error, isNodeL3Ready didn't return true", bridgeConfigurationManagerImpl.isNodeL3Ready(node, node));
+    }
+
+    @Test
+    public void testPrepareNode() {
+        when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
+        when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString())).thenReturn(false);
+
+        PowerMockito.mockStatic(ConfigProperties.class);
+        when(ConfigProperties.getProperty(any(Class.class), anyString())).thenReturn(ADDRESS);
+
+        when(southbound.addBridge(any(Node.class), anyString(), anyList())).thenReturn(true);
+        when(configurationService.isL3ForwardingEnabled()).thenReturn(true);
+
+        bridgeConfigurationManagerImpl.prepareNode(node);
+    }
+
     @Test
     public void testCreateLocalNetwork() throws Exception {
         NeutronNetwork neutronNetworkMock = mock(NeutronNetwork.class, RETURNS_MOCKS);
         String networkTypes[] = {"vlan", "vxlan", "gre"};
         BridgeConfigurationManagerImpl bridgeConfigurationManagerImplSpy =
                 PowerMockito.spy(new BridgeConfigurationManagerImpl());
+        bridgeConfigurationManagerImplSpy.setConfigurationService(configurationService);
+        bridgeConfigurationManagerImplSpy.setSouthbound(southbound);
 
         for (String networkType : networkTypes) {
             when(neutronNetworkMock.getProviderNetworkType()).thenReturn(networkType);
+            when(southbound.readOvsdbNode(any(Node.class))).thenReturn(node);
 
             doAnswer(new Answer<Boolean>() {
                 @Override
                 public Boolean answer(InvocationOnMock invocation) {
                     return Boolean.TRUE;
                 }
-            }).when(bridgeConfigurationManagerImplSpy).isNodeVlanReady(any(Node.class), any(NeutronNetwork.class));
+            }).when(bridgeConfigurationManagerImplSpy).isNodeVlanReady(any(Node.class), any(Node.class), any(NeutronNetwork.class));
 
             doAnswer(new Answer<Boolean>() {
                 @Override
                 public Boolean answer(InvocationOnMock invocation) {
                     return Boolean.TRUE;
                 }
-            }).when(bridgeConfigurationManagerImplSpy).isNodeTunnelReady(any(Node.class));
+            }).when(bridgeConfigurationManagerImplSpy).isNodeTunnelReady(any(Node.class), any(Node.class));
 
             assertTrue("bridgeConfigMock.isNodeVlanReady is not true",
-                    bridgeConfigurationManagerImplSpy.isNodeVlanReady(node, neutronNetworkMock));
+                    bridgeConfigurationManagerImplSpy.isNodeVlanReady(node, node, neutronNetworkMock));
             assertTrue("bridgeConfigMock.isNodeTunnelReady is not true",
-                    bridgeConfigurationManagerImplSpy.isNodeTunnelReady(node));
+                    bridgeConfigurationManagerImplSpy.isNodeTunnelReady(node, node));
 
             assertTrue("Error, isCreated is not true for " + networkType,
                     bridgeConfigurationManagerImplSpy.createLocalNetwork(node, neutronNetworkMock));
@@ -290,4 +327,28 @@ public class BridgeConfigurationManagerImplTest {
         verify(configurationService, times(1)).getDefaultProviderMapping();
         verify(southbound, times(2)).getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString());
     }
+
+    @Test
+    public void testSetDependencies() throws Exception {
+        ConfigurationService configurationService = mock(ConfigurationService.class);
+        NetworkingProviderManager networkingProviderManager = mock(NetworkingProviderManager.class);
+        Southbound southbound = mock(Southbound.class);
+
+        PowerMockito.mockStatic(ServiceHelper.class);
+        PowerMockito.when(ServiceHelper.getGlobalInstance(ConfigurationService.class, bridgeConfigurationManagerImpl)).thenReturn(configurationService);
+        PowerMockito.when(ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, bridgeConfigurationManagerImpl)).thenReturn(networkingProviderManager);
+        PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, bridgeConfigurationManagerImpl)).thenReturn(southbound);
+
+        bridgeConfigurationManagerImpl.setDependencies(mock(ServiceReference.class));
+
+        assertEquals("Error, did not return the correct object", getField("configurationService"), configurationService);
+        assertEquals("Error, did not return the correct object", getField("networkingProviderManager"), networkingProviderManager);
+        assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
+    }
+
+    private Object getField(String fieldName) throws Exception {
+        Field field = BridgeConfigurationManagerImpl.class.getDeclaredField(fieldName);
+        field.setAccessible(true);
+        return field.get(bridgeConfigurationManagerImpl);
+    }
 }
\ No newline at end of file