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
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP : APPC\r
4  * ================================================================================\r
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Copyright (C) 2017 Amdocs\r
8  * =============================================================================\r
9  * Licensed under the Apache License, Version 2.0 (the "License");\r
10  * you may not use this file except in compliance with the License.\r
11  * You may obtain a copy of the License at\r
12  * \r
13  *      http://www.apache.org/licenses/LICENSE-2.0\r
14  * \r
15  * Unless required by applicable law or agreed to in writing, software\r
16  * distributed under the License is distributed on an "AS IS" BASIS,\r
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
18  * See the License for the specific language governing permissions and\r
19  * limitations under the License.\r
20  * \r
21  * ============LICENSE_END=========================================================\r
22  */\r
23 \r
24 package org.onap.appc.adapter.iaas.impl;\r
25 \r
26 import static org.junit.Assert.assertEquals;\r
27 import static org.junit.Assert.assertFalse;\r
28 import static org.junit.Assert.assertNotNull;\r
29 import static org.junit.Assert.assertNull;\r
30 import static org.junit.Assert.assertTrue;\r
31 import static org.mockito.Mockito.when;\r
32 import java.util.Arrays;\r
33 import java.util.HashSet;\r
34 import java.util.List;\r
35 import java.util.Map;\r
36 import java.util.Properties;\r
37 import java.util.Set;\r
38 import org.junit.Before;\r
39 import org.junit.BeforeClass;\r
40 import org.junit.Ignore;\r
41 import org.junit.Test;\r
42 import org.junit.runner.RunWith;\r
43 import org.mockito.Mock;\r
44 import org.mockito.runners.MockitoJUnitRunner;\r
45 import org.onap.appc.configuration.ConfigurationFactory;\r
46 import com.att.cdp.exceptions.ZoneException;\r
47 import com.google.common.collect.ImmutableMap;\r
48 import com.woorea.openstack.keystone.model.Access.Service;\r
49 import com.woorea.openstack.keystone.model.Access.Service.Endpoint;\r
50 import com.woorea.openstack.keystone.model.Tenant;\r
51 \r
52 /**\r
53  * This class tests the service catalog against a known provider.\r
54  */\r
55 @RunWith(MockitoJUnitRunner.class)\r
56 public class TestServiceCatalogV2 {\r
57 \r
58     // Number\r
59     private static int EXPECTED_REGIONS = 1;\r
60     private static int EXPECTED_ENDPOINTS = 1;\r
61 \r
62     private static String PRINCIPAL;\r
63     private static String CREDENTIAL;\r
64     private static String TENANT_NAME;\r
65     private static String TENANT_ID;\r
66     private static String IDENTITY_URL;\r
67     private static String REGION_NAME;\r
68     private static String PUBLIC_URL;\r
69 \r
70     private static String IP;\r
71     private static String PORT;\r
72     private static String TENANTID;\r
73     private static String VMID;\r
74     private static String URL;\r
75 \r
76     private ServiceCatalogV2 catalog;\r
77 \r
78     private Properties properties;\r
79 \r
80     @Mock\r
81     private Tenant tenant;\r
82 \r
83     private final Set<String> regions = new HashSet<>(Arrays.asList("RegionOne"));\r
84 \r
85     private Map<String, Service> serviceTypes;\r
86 \r
87     private Map<String, List<Service.Endpoint>> serviceEndpoints;\r
88 \r
89     @BeforeClass\r
90     public static void before() {\r
91         final Properties props = ConfigurationFactory.getConfiguration().getProperties();\r
92         IDENTITY_URL = props.getProperty("provider1.identity", "appc");\r
93         PRINCIPAL = props.getProperty("provider1.tenant1.userid", "appc");\r
94         CREDENTIAL = props.getProperty("provider1.tenant1.password", "appc");\r
95         TENANT_NAME = props.getProperty("provider1.tenant1.name", "appc");\r
96         TENANT_ID = props.getProperty("provider1.tenant1.id",\r
97                 props.getProperty("test.tenantid", "abcde12345fghijk6789lmnopq123rst"));\r
98         REGION_NAME = props.getProperty("provider1.tenant1.region", "RegionOne");\r
99 \r
100         IP = props.getProperty("test.ip");\r
101         PORT = props.getProperty("test.port");\r
102         TENANTID = props.getProperty("test.tenantid");\r
103         VMID = props.getProperty("test.vmid");\r
104 \r
105         EXPECTED_REGIONS = Integer.valueOf(props.getProperty("test.expected-regions", "0"));\r
106         EXPECTED_ENDPOINTS = Integer.valueOf(props.getProperty("test.expected-endpoints", "0"));\r
107 \r
108         PUBLIC_URL =\r
109                 "http://192.168.1.2:5000/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";\r
110     }\r
111 \r
112     /**\r
113      * Setup the test environment by loading a new service catalog for each test Use reflection to\r
114      * locate fields and methods so that they can be manipulated during the test to change the\r
115      * internal state accordingly.\r
116      * \r
117      */\r
118     @Before\r
119     public void setup() {\r
120         URL = String.format("http://%s:%s/v2/%s/servers/%s", IP, PORT, TENANTID, VMID);\r
121         properties = new Properties();\r
122         catalog = new ServiceCatalogV2(IDENTITY_URL, TENANT_NAME, PRINCIPAL, CREDENTIAL, properties);\r
123         final Service service = new Service();\r
124         serviceTypes = ImmutableMap.<String, Service>builder().put(ServiceCatalog.COMPUTE_SERVICE, service)\r
125                 .put(ServiceCatalog.IDENTITY_SERVICE, service).put(ServiceCatalog.IMAGE_SERVICE, service)\r
126                 .put(ServiceCatalog.NETWORK_SERVICE, service).put(ServiceCatalog.VOLUME_SERVICE, service).build();\r
127         Map<String, Object> endpointPrivateFields =\r
128                 ImmutableMap.<String, Object>builder().put("publicURL", PUBLIC_URL).put("region", REGION_NAME).build();\r
129         Service.Endpoint endpoint = new Service.Endpoint();\r
130         CommonUtility.injectMockObjects(endpointPrivateFields, endpoint);\r
131         final List<Service.Endpoint> endpoints = Arrays.asList(endpoint);\r
132         serviceEndpoints = ImmutableMap.<String, List<Service.Endpoint>>builder()\r
133                 .put(ServiceCatalog.COMPUTE_SERVICE, endpoints).build();\r
134         Map<String, Object> privateFields =\r
135                 ImmutableMap.<String, Object>builder().put("regions", regions).put("tenant", tenant)\r
136                         .put("serviceTypes", serviceTypes).put("serviceEndpoints", serviceEndpoints).build();\r
137         CommonUtility.injectMockObjects(privateFields, catalog);\r
138         CommonUtility.injectMockObjectsInBaseClass(privateFields, catalog);\r
139 \r
140     }\r
141 \r
142     /**\r
143      * Ensure that we get the Tenant Name & Tenant Id property are returned correctly\r
144      */\r
145     @Test\r
146     public void testKnownTenant() {\r
147         when(tenant.getName()).thenReturn(TENANT_NAME);\r
148         when(tenant.getId()).thenReturn(TENANT_ID);\r
149         assertEquals(TENANT_NAME, catalog.getProjectName());\r
150         assertEquals(TENANT_ID, catalog.getProjectId());\r
151     }\r
152 \r
153     /**\r
154      * Ensure that we set up the Region property correctly\r
155      */\r
156     @Test\r
157     public void testKnownRegions() {\r
158         assertEquals(EXPECTED_REGIONS, catalog.getRegions().size());\r
159         assertEquals(REGION_NAME, catalog.getRegions().toArray()[0]);\r
160     }\r
161 \r
162     /**\r
163      * Ensure that that we can check for published services correctly\r
164      */\r
165     @Test\r
166     public void testServiceTypesPublished() {\r
167         assertTrue(catalog.isServicePublished("compute"));\r
168         assertFalse(catalog.isServicePublished("bogus"));\r
169     }\r
170 \r
171     /**\r
172      * Ensure that we can get the list of published services\r
173      */\r
174     @Test\r
175     public void testPublishedServicesList() {\r
176         final List<String> services = catalog.getServiceTypes();\r
177         assertTrue(services.contains(ServiceCatalog.COMPUTE_SERVICE));\r
178         assertTrue(services.contains(ServiceCatalog.IDENTITY_SERVICE));\r
179         assertTrue(services.contains(ServiceCatalog.IMAGE_SERVICE));\r
180         assertTrue(services.contains(ServiceCatalog.NETWORK_SERVICE));\r
181         assertTrue(services.contains(ServiceCatalog.VOLUME_SERVICE));\r
182     }\r
183 \r
184     /**\r
185      * Ensure that we can get the endpoint(s) for a service\r
186      */\r
187     @Test\r
188     public void testEndpointList() {\r
189         List<Endpoint> endpoints = catalog.getEndpoints(ServiceCatalog.COMPUTE_SERVICE);\r
190         assertNotNull(endpoints);\r
191         assertFalse(endpoints.isEmpty());\r
192         assertEquals(EXPECTED_ENDPOINTS, endpoints.size());\r
193     }\r
194 \r
195     /**\r
196      * Ensure that we override the toString method\r
197      */\r
198     @Test\r
199     public void testToString() {\r
200         when(tenant.getId()).thenReturn(TENANT_ID);\r
201         when(tenant.getDescription()).thenReturn("Tenant one");\r
202         final String testString = catalog.toString();\r
203         assertNotNull(testString);\r
204     }\r
205 \r
206     /**\r
207      * Ensure that we can get the VM Region\r
208      */\r
209     @Test\r
210     public void testGetVMRegion() {\r
211         VMURL url = VMURL.parseURL(URL);\r
212         String region = catalog.getVMRegion(url);\r
213         assertEquals(REGION_NAME, region);\r
214     }\r
215 \r
216     /**\r
217      * Ensure that we can get the null region when no URL is passed\r
218      */\r
219     @Test\r
220     public void testGetVMRegionWithoutURL() {\r
221         String region = catalog.getVMRegion(null);\r
222         assertNull(region);\r
223     }\r
224 \r
225     @Ignore\r
226     @Test\r
227     public void liveConnectionTest() {\r
228         // this test should only be used by developers when testing against a live Openstack\r
229         // instance, otherwise it should be ignored\r
230         properties = new Properties();\r
231         String identity = "http://192.168.0.1:5000/v2.0";\r
232         String tenantName = "Tenant";\r
233         String user = "user";\r
234         String pass = "pass";\r
235 \r
236         ServiceCatalogV2 catalog = new ServiceCatalogV2(identity, tenantName, user, pass, properties);\r
237 \r
238         try {\r
239             catalog.init();\r
240         } catch (ZoneException e) {\r
241             // TODO Auto-generated catch block\r
242             e.printStackTrace();\r
243         }\r
244 \r
245         String out = catalog.toString();\r
246         System.out.println(out);\r
247     }\r
248 }\r