Unit test cases for iaas impl package
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / test / java / org / onap / appc / adapter / iaas / impl / TestServiceCatalogV2.java
index 8021029..e0684f1 100644 (file)
@@ -26,21 +26,33 @@ package org.onap.appc.adapter.iaas.impl;
 import static org.junit.Assert.assertEquals;\r
 import static org.junit.Assert.assertFalse;\r
 import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.assertNull;\r
 import static org.junit.Assert.assertTrue;\r
-import com.att.cdp.exceptions.ZoneException;\r
-import com.woorea.openstack.keystone.model.Access.Service;\r
+import static org.mockito.Mockito.when;\r
+import java.util.Arrays;\r
+import java.util.HashSet;\r
 import java.util.List;\r
+import java.util.Map;\r
 import java.util.Properties;\r
+import java.util.Set;\r
 import org.junit.Before;\r
 import org.junit.BeforeClass;\r
 import org.junit.Ignore;\r
 import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+import org.mockito.Mock;\r
+import org.mockito.runners.MockitoJUnitRunner;\r
 import org.onap.appc.configuration.ConfigurationFactory;\r
+import com.att.cdp.exceptions.ZoneException;\r
+import com.google.common.collect.ImmutableMap;\r
+import com.woorea.openstack.keystone.model.Access.Service;\r
+import com.woorea.openstack.keystone.model.Access.Service.Endpoint;\r
+import com.woorea.openstack.keystone.model.Tenant;\r
 \r
 /**\r
  * This class tests the service catalog against a known provider.\r
  */\r
-@Ignore\r
+@RunWith(MockitoJUnitRunner.class)\r
 public class TestServiceCatalogV2 {\r
 \r
     // Number\r
@@ -53,14 +65,30 @@ public class TestServiceCatalogV2 {
     private static String TENANT_ID;\r
     private static String IDENTITY_URL;\r
     private static String REGION_NAME;\r
+    private static String PUBLIC_URL;\r
+\r
+    private static String IP;\r
+    private static String PORT;\r
+    private static String TENANTID;\r
+    private static String VMID;\r
+    private static String URL;\r
 \r
     private ServiceCatalogV2 catalog;\r
 \r
     private Properties properties;\r
 \r
+    @Mock\r
+    private Tenant tenant;\r
+\r
+    private final Set<String> regions = new HashSet<>(Arrays.asList("RegionOne"));\r
+\r
+    private Map<String, Service> serviceTypes;\r
+\r
+    private Map<String, List<Service.Endpoint>> serviceEndpoints;\r
+\r
     @BeforeClass\r
     public static void before() {\r
-        Properties props = ConfigurationFactory.getConfiguration().getProperties();\r
+        final Properties props = ConfigurationFactory.getConfiguration().getProperties();\r
         IDENTITY_URL = props.getProperty("provider1.identity", "appc");\r
         PRINCIPAL = props.getProperty("provider1.tenant1.userid", "appc");\r
         CREDENTIAL = props.getProperty("provider1.tenant1.password", "appc");\r
@@ -69,42 +97,70 @@ public class TestServiceCatalogV2 {
                 props.getProperty("test.tenantid", "abcde12345fghijk6789lmnopq123rst"));\r
         REGION_NAME = props.getProperty("provider1.tenant1.region", "RegionOne");\r
 \r
+        IP = props.getProperty("test.ip");\r
+        PORT = props.getProperty("test.port");\r
+        TENANTID = props.getProperty("test.tenantid");\r
+        VMID = props.getProperty("test.vmid");\r
+\r
         EXPECTED_REGIONS = Integer.valueOf(props.getProperty("test.expected-regions", "0"));\r
         EXPECTED_ENDPOINTS = Integer.valueOf(props.getProperty("test.expected-endpoints", "0"));\r
+\r
+        PUBLIC_URL =\r
+                "http://192.168.1.2:5000/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";\r
     }\r
 \r
     /**\r
-     * Setup the test environment by loading a new service catalog for each test\r
+     * Setup the test environment by loading a new service catalog for each test Use reflection to\r
+     * locate fields and methods so that they can be manipulated during the test to change the\r
+     * internal state accordingly.\r
      * \r
-     * @throws ZoneException\r
      */\r
     @Before\r
-    public void setup() throws ZoneException {\r
+    public void setup() {\r
+        URL = String.format("http://%s:%s/v2/%s/servers/%s", IP, PORT, TENANTID, VMID);\r
         properties = new Properties();\r
         catalog = new ServiceCatalogV2(IDENTITY_URL, TENANT_NAME, PRINCIPAL, CREDENTIAL, properties);\r
-        catalog.init();\r
+        final Service service = new Service();\r
+        serviceTypes = ImmutableMap.<String, Service>builder().put(ServiceCatalog.COMPUTE_SERVICE, service)\r
+                .put(ServiceCatalog.IDENTITY_SERVICE, service).put(ServiceCatalog.IMAGE_SERVICE, service)\r
+                .put(ServiceCatalog.NETWORK_SERVICE, service).put(ServiceCatalog.VOLUME_SERVICE, service).build();\r
+        Map<String, Object> endpointPrivateFields =\r
+                ImmutableMap.<String, Object>builder().put("publicURL", PUBLIC_URL).put("region", REGION_NAME).build();\r
+        Service.Endpoint endpoint = new Service.Endpoint();\r
+        CommonUtility.injectMockObjects(endpointPrivateFields, endpoint);\r
+        final List<Service.Endpoint> endpoints = Arrays.asList(endpoint);\r
+        serviceEndpoints = ImmutableMap.<String, List<Service.Endpoint>>builder()\r
+                .put(ServiceCatalog.COMPUTE_SERVICE, endpoints).build();\r
+        Map<String, Object> privateFields =\r
+                ImmutableMap.<String, Object>builder().put("regions", regions).put("tenant", tenant)\r
+                        .put("serviceTypes", serviceTypes).put("serviceEndpoints", serviceEndpoints).build();\r
+        CommonUtility.injectMockObjects(privateFields, catalog);\r
+        CommonUtility.injectMockObjectsInBaseClass(privateFields, catalog);\r
+\r
     }\r
 \r
     /**\r
-     * Test that the tenant name and ID are returned correctly\r
+     * Ensure that we get the Tenant Name & Tenant Id property are returned correctly\r
      */\r
     @Test\r
     public void testKnownTenant() {\r
+        when(tenant.getName()).thenReturn(TENANT_NAME);\r
+        when(tenant.getId()).thenReturn(TENANT_ID);\r
         assertEquals(TENANT_NAME, catalog.getProjectName());\r
         assertEquals(TENANT_ID, catalog.getProjectId());\r
     }\r
 \r
     /**\r
-     * Test that we find all of the expected region(s)\r
+     * Ensure that we set up the Region property correctly\r
      */\r
     @Test\r
     public void testKnownRegions() {\r
         assertEquals(EXPECTED_REGIONS, catalog.getRegions().size());\r
-        // assertEquals(REGION_NAME, catalog.getRegions().toArray()[0]);\r
+        assertEquals(REGION_NAME, catalog.getRegions().toArray()[0]);\r
     }\r
 \r
     /**\r
-     * Test that we can check for published services correctly\r
+     * Ensure that that we can check for published services correctly\r
      */\r
     @Test\r
     public void testServiceTypesPublished() {\r
@@ -113,40 +169,59 @@ public class TestServiceCatalogV2 {
     }\r
 \r
     /**\r
-     * Check that we can get the list of published services\r
+     * Ensure that we can get the list of published services\r
      */\r
     @Test\r
     public void testPublishedServicesList() {\r
-        // List<String> services = catalog.getServiceTypes();\r
-\r
-        // assertTrue(services.contains(ServiceCatalog.COMPUTE_SERVICE));\r
-        // assertTrue(services.contains(ServiceCatalog.IDENTITY_SERVICE));\r
-        // assertTrue(services.contains(ServiceCatalog.IMAGE_SERVICE));\r
-        // assertTrue(services.contains(ServiceCatalog.NETWORK_SERVICE));\r
-        // assertTrue(services.contains(ServiceCatalog.VOLUME_SERVICE));\r
+        final List<String> services = catalog.getServiceTypes();\r
+        assertTrue(services.contains(ServiceCatalog.COMPUTE_SERVICE));\r
+        assertTrue(services.contains(ServiceCatalog.IDENTITY_SERVICE));\r
+        assertTrue(services.contains(ServiceCatalog.IMAGE_SERVICE));\r
+        assertTrue(services.contains(ServiceCatalog.NETWORK_SERVICE));\r
+        assertTrue(services.contains(ServiceCatalog.VOLUME_SERVICE));\r
     }\r
 \r
     /**\r
-     * Test that we can get the endpoint(s) for a service\r
+     * Ensure that we can get the endpoint(s) for a service\r
      */\r
     @Test\r
     public void testEndpointList() {\r
-        List<Service.Endpoint> endpoints = catalog.getEndpoints(ServiceCatalog.COMPUTE_SERVICE);\r
-\r
+        List<Endpoint> endpoints = catalog.getEndpoints(ServiceCatalog.COMPUTE_SERVICE);\r
         assertNotNull(endpoints);\r
         assertFalse(endpoints.isEmpty());\r
         assertEquals(EXPECTED_ENDPOINTS, endpoints.size());\r
-\r
-        Service.Endpoint endpoint = endpoints.get(0);\r
-        // assertEquals(REGION_NAME, endpoint.getRegion());\r
     }\r
 \r
+    /**\r
+     * Ensure that we override the toString method\r
+     */\r
     @Test\r
     public void testToString() {\r
-        String testString = catalog.toString();\r
+        when(tenant.getId()).thenReturn(TENANT_ID);\r
+        when(tenant.getDescription()).thenReturn("Tenant one");\r
+        final String testString = catalog.toString();\r
         assertNotNull(testString);\r
     }\r
 \r
+    /**\r
+     * Ensure that we can get the VM Region\r
+     */\r
+    @Test\r
+    public void testGetVMRegion() {\r
+        VMURL url = VMURL.parseURL(URL);\r
+        String region = catalog.getVMRegion(url);\r
+        assertEquals(REGION_NAME, region);\r
+    }\r
+\r
+    /**\r
+     * Ensure that we can get the null region when no URL is passed\r
+     */\r
+    @Test\r
+    public void testGetVMRegionWithoutURL() {\r
+        String region = catalog.getVMRegion(null);\r
+        assertNull(region);\r
+    }\r
+\r
     @Ignore\r
     @Test\r
     public void liveConnectionTest() {\r