d7267183ba676f481488d295bf99eb58b37e6088
[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.ArrayList;\r
33 import java.util.Arrays;\r
34 import java.util.HashSet;\r
35 import java.util.List;\r
36 import java.util.Map;\r
37 import java.util.Properties;\r
38 import java.util.Set;\r
39 import org.junit.Before;\r
40 import org.junit.BeforeClass;\r
41 import org.junit.Ignore;\r
42 import org.junit.Test;\r
43 import org.junit.runner.RunWith;\r
44 import org.mockito.Mock;\r
45 import org.mockito.Mockito;\r
46 import org.mockito.runners.MockitoJUnitRunner;\r
47 import org.onap.appc.configuration.ConfigurationFactory;\r
48 import org.powermock.reflect.Whitebox;\r
49 import com.att.cdp.exceptions.ZoneException;\r
50 import com.att.cdp.zones.ContextFactory;\r
51 import com.google.common.collect.ImmutableMap;\r
52 import com.woorea.openstack.keystone.model.Access;\r
53 import com.woorea.openstack.keystone.model.Access.Service;\r
54 import com.woorea.openstack.keystone.model.Access.Service.Endpoint;\r
55 import com.woorea.openstack.base.client.OpenStackClientConnector;\r
56 import com.woorea.openstack.base.client.OpenStackConnectException;\r
57 import com.woorea.openstack.base.client.OpenStackResponseException;\r
58 import com.woorea.openstack.keystone.Keystone;\r
59 import com.woorea.openstack.keystone.api.TokensResource;\r
60 import com.woorea.openstack.keystone.model.Tenant;\r
61 import com.woorea.openstack.keystone.model.Token;\r
62 \r
63 /**\r
64  * This class tests the service catalog against a known provider.\r
65  */\r
66 @RunWith(MockitoJUnitRunner.class)\r
67 public class TestServiceCatalogV2 {\r
68 \r
69     // Number\r
70     private static int EXPECTED_REGIONS = 1;\r
71     private static int EXPECTED_ENDPOINTS = 1;\r
72 \r
73     private static String PRINCIPAL;\r
74     private static String CREDENTIAL;\r
75     private static String TENANT_NAME;\r
76     private static String TENANT_ID;\r
77     private static String IDENTITY_URL;\r
78     private static String REGION_NAME;\r
79     private static String PUBLIC_URL;\r
80 \r
81     private static String IP;\r
82     private static String PORT;\r
83     private static String TENANTID;\r
84     private static String VMID;\r
85     private static String URL;\r
86 \r
87     private ServiceCatalogV2 catalog;\r
88 \r
89     private Properties properties;\r
90 \r
91     @Mock\r
92     private Tenant tenant;\r
93 \r
94     private final Set<String> regions = new HashSet<>(Arrays.asList("RegionOne"));\r
95 \r
96     private Map<String, Service> serviceTypes;\r
97 \r
98     private Map<String, List<Service.Endpoint>> serviceEndpoints;\r
99 \r
100     @BeforeClass\r
101     public static void before() {\r
102         final Properties props = ConfigurationFactory.getConfiguration().getProperties();\r
103         IDENTITY_URL = props.getProperty("provider1.identity", "appc");\r
104         PRINCIPAL = props.getProperty("provider1.tenant1.userid", "appc");\r
105         CREDENTIAL = props.getProperty("provider1.tenant1.password", "appc");\r
106         TENANT_NAME = props.getProperty("provider1.tenant1.name", "appc");\r
107         TENANT_ID = props.getProperty("provider1.tenant1.id",\r
108                 props.getProperty("test.tenantid", "abcde12345fghijk6789lmnopq123rst"));\r
109         REGION_NAME = props.getProperty("provider1.tenant1.region", "RegionOne");\r
110 \r
111         IP = props.getProperty("test.ip");\r
112         PORT = props.getProperty("test.port");\r
113         TENANTID = props.getProperty("test.tenantid");\r
114         VMID = props.getProperty("test.vmid");\r
115 \r
116         EXPECTED_REGIONS = Integer.valueOf(props.getProperty("test.expected-regions", "0"));\r
117         EXPECTED_ENDPOINTS = Integer.valueOf(props.getProperty("test.expected-endpoints", "0"));\r
118 \r
119         PUBLIC_URL =\r
120                 "http://192.168.1.2:5000/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";\r
121     }\r
122 \r
123     /**\r
124      * Setup the test environment by loading a new service catalog for each test Use reflection to\r
125      * locate fields and methods so that they can be manipulated during the test to change the\r
126      * internal state accordingly.\r
127      * \r
128      */\r
129     @Before\r
130     public void setup() {\r
131         URL = String.format("http://%s:%s/v2/%s/servers/%s", IP, PORT, TENANTID, VMID);\r
132         properties = new Properties();\r
133         properties.setProperty(ContextFactory.PROPERTY_PROXY_HOST, "PROXY_HOST");\r
134         properties.setProperty(ContextFactory.PROPERTY_PROXY_PORT, "PROXY_PORT");\r
135         catalog = new ServiceCatalogV2(IDENTITY_URL, TENANT_NAME, PRINCIPAL, CREDENTIAL, properties);\r
136         final Service service = new Service();\r
137         serviceTypes = ImmutableMap.<String, Service>builder().put(ServiceCatalog.COMPUTE_SERVICE, service)\r
138                 .put(ServiceCatalog.IDENTITY_SERVICE, service).put(ServiceCatalog.IMAGE_SERVICE, service)\r
139                 .put(ServiceCatalog.NETWORK_SERVICE, service).put(ServiceCatalog.VOLUME_SERVICE, service).build();\r
140         Map<String, Object> endpointPrivateFields =\r
141                 ImmutableMap.<String, Object>builder().put("publicURL", PUBLIC_URL).put("region", REGION_NAME).build();\r
142         Service.Endpoint endpoint = new Service.Endpoint();\r
143         CommonUtility.injectMockObjects(endpointPrivateFields, endpoint);\r
144         final List<Service.Endpoint> endpoints = Arrays.asList(endpoint);\r
145         serviceEndpoints = ImmutableMap.<String, List<Service.Endpoint>>builder()\r
146                 .put(ServiceCatalog.COMPUTE_SERVICE, endpoints).build();\r
147         Map<String, Object> privateFields =\r
148                 ImmutableMap.<String, Object>builder().put("regions", regions).put("tenant", tenant)\r
149                         .put("serviceTypes", serviceTypes).put("serviceEndpoints", serviceEndpoints).build();\r
150         CommonUtility.injectMockObjects(privateFields, catalog);\r
151         CommonUtility.injectMockObjectsInBaseClass(privateFields, catalog);\r
152 \r
153     }\r
154 \r
155     /**\r
156      * Ensure that we get the Tenant Name & Tenant Id property are returned correctly\r
157      */\r
158     @Test\r
159     public void testKnownTenant() {\r
160         when(tenant.getName()).thenReturn(TENANT_NAME);\r
161         when(tenant.getId()).thenReturn(TENANT_ID);\r
162         assertEquals(TENANT_NAME, catalog.getProjectName());\r
163         assertEquals(TENANT_ID, catalog.getProjectId());\r
164     }\r
165 \r
166     /**\r
167      * Ensure that we set up the Region property correctly\r
168      */\r
169     @Test\r
170     public void testKnownRegions() {\r
171         assertEquals(EXPECTED_REGIONS, catalog.getRegions().size());\r
172         assertEquals(REGION_NAME, catalog.getRegions().toArray()[0]);\r
173     }\r
174 \r
175     /**\r
176      * Ensure that that we can check for published services correctly\r
177      */\r
178     @Test\r
179     public void testServiceTypesPublished() {\r
180         assertTrue(catalog.isServicePublished("compute"));\r
181         assertFalse(catalog.isServicePublished("bogus"));\r
182     }\r
183 \r
184     /**\r
185      * Ensure that we can get the list of published services\r
186      */\r
187     @Test\r
188     public void testPublishedServicesList() {\r
189         final List<String> services = catalog.getServiceTypes();\r
190         assertTrue(services.contains(ServiceCatalog.COMPUTE_SERVICE));\r
191         assertTrue(services.contains(ServiceCatalog.IDENTITY_SERVICE));\r
192         assertTrue(services.contains(ServiceCatalog.IMAGE_SERVICE));\r
193         assertTrue(services.contains(ServiceCatalog.NETWORK_SERVICE));\r
194         assertTrue(services.contains(ServiceCatalog.VOLUME_SERVICE));\r
195     }\r
196 \r
197     /**\r
198      * Ensure that we can get the endpoint(s) for a service\r
199      */\r
200     @Test\r
201     public void testEndpointList() {\r
202         List<Endpoint> endpoints = catalog.getEndpoints(ServiceCatalog.COMPUTE_SERVICE);\r
203         assertNotNull(endpoints);\r
204         assertFalse(endpoints.isEmpty());\r
205         assertEquals(EXPECTED_ENDPOINTS, endpoints.size());\r
206     }\r
207 \r
208     /**\r
209      * Ensure that we override the toString method\r
210      */\r
211     @Test\r
212     public void testToString() {\r
213         when(tenant.getId()).thenReturn(TENANT_ID);\r
214         when(tenant.getDescription()).thenReturn("Tenant one");\r
215         final String testString = catalog.toString();\r
216         assertNotNull(testString);\r
217     }\r
218 \r
219     /**\r
220      * Ensure that we can get the VM Region\r
221      */\r
222     @Test\r
223     public void testGetVMRegion() {\r
224         VMURL url = VMURL.parseURL(URL);\r
225         String region = catalog.getVMRegion(url);\r
226         assertEquals(REGION_NAME, region);\r
227     }\r
228 \r
229     /**\r
230      * Ensure that we can get the null region when no URL is passed\r
231      */\r
232     @Test\r
233     public void testGetVMRegionWithoutURL() {\r
234         String region = catalog.getVMRegion(null);\r
235         assertNull(region);\r
236     }\r
237 \r
238     @Ignore\r
239     @Test\r
240     public void liveConnectionTest() {\r
241         // this test should only be used by developers when testing against a live Openstack\r
242         // instance, otherwise it should be ignored\r
243         properties = new Properties();\r
244         String identity = "http://192.168.0.1:5000/v2.0";\r
245         String tenantName = "Tenant";\r
246         String user = "user";\r
247         String pass = "pass";\r
248 \r
249         ServiceCatalogV2 catalog = new ServiceCatalogV2(identity, tenantName, user, pass, properties);\r
250 \r
251         try {\r
252             catalog.init();\r
253         } catch (ZoneException e) {\r
254             // TODO Auto-generated catch block\r
255             e.printStackTrace();\r
256         }\r
257 \r
258         String out = catalog.toString();\r
259         System.out.println(out);\r
260     }\r
261 \r
262     @Test\r
263     public void testInit() throws ZoneException, ClassNotFoundException, InstantiationException, IllegalAccessException, OpenStackConnectException, OpenStackResponseException {\r
264         ServiceCatalogV2 catalogSpy = Mockito.spy(catalog);\r
265         Class<?> connectorClass = Class.forName(ServiceCatalogV2.CLIENT_CONNECTOR_CLASS);\r
266         OpenStackClientConnector connector = (OpenStackClientConnector) connectorClass.newInstance();\r
267         Keystone keystone = Mockito.spy(new Keystone(IDENTITY_URL, connector));\r
268         TokensResource tokens = Mockito.mock(TokensResource.class);\r
269         TokensResource.Authenticate authenticate = Mockito.mock(TokensResource.Authenticate.class);\r
270         Mockito.when(keystone.tokens()).thenReturn(tokens);\r
271         Mockito.when(tokens.authenticate(Mockito.any())).thenReturn(authenticate);\r
272         Access access = Mockito.mock(Access.class);\r
273 \r
274         Token token = new Token();\r
275         Mockito.when(access.getToken()).thenReturn(token);\r
276         Mockito.when(authenticate.execute()).thenReturn(access);\r
277         Mockito.when(authenticate.withTenantName(Mockito.anyString())).thenReturn(authenticate);\r
278         Mockito.when(catalogSpy.getKeystone(Mockito.anyString(), Mockito.any())).thenReturn(keystone);\r
279         Access.Service service = new Access.Service();\r
280         Endpoint endpoint = new Endpoint();\r
281         List<Endpoint> endpointList = new ArrayList<>();\r
282         endpointList.add(endpoint);\r
283         Whitebox.setInternalState(service, "endpoints", endpointList);\r
284         List<Service> serviceList = new ArrayList<>();\r
285         serviceList.add(service);\r
286         Mockito.when(access.getServiceCatalog()).thenReturn(serviceList);\r
287         catalogSpy.init();\r
288         Mockito.verify(access).getServiceCatalog();\r
289     }\r
290 }\r