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 / TestServiceCatalogV3.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.Mockito;\r
44 import org.mockito.runners.MockitoJUnitRunner;\r
45 import org.onap.appc.configuration.ConfigurationFactory;\r
46 import com.google.common.collect.ImmutableMap;\r
47 import com.woorea.openstack.keystone.v3.model.Token;\r
48 import com.woorea.openstack.keystone.v3.model.Token.Service;\r
49 import com.woorea.openstack.keystone.v3.model.Token.Service.Endpoint;\r
50 \r
51 /**\r
52  * This class tests the service catalog against a known provider.\r
53  */\r
54 @RunWith(MockitoJUnitRunner.class)\r
55 public class TestServiceCatalogV3 {\r
56 \r
57     // Number\r
58     private static int EXPECTED_REGIONS = 1;\r
59     private static int EXPECTED_ENDPOINTS = 1;\r
60 \r
61     private static String PRINCIPAL;\r
62     private static String CREDENTIAL;\r
63     private static String DOMAIN;\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 ServiceCatalogV3 catalog;\r
77 \r
78     private ServiceCatalogV3 spyCatalog;\r
79 \r
80     private Properties properties;\r
81 \r
82     private Token.Project project = new Token.Project();\r
83 \r
84     private final Set<String> regions = new HashSet<>(Arrays.asList("RegionOne"));\r
85 \r
86     private Map<String, Service> serviceTypes;\r
87 \r
88     private Map<String, List<Service.Endpoint>> serviceEndpoints;\r
89 \r
90     @BeforeClass\r
91     public static void before() {\r
92         Properties props = ConfigurationFactory.getConfiguration().getProperties();\r
93         IDENTITY_URL = props.getProperty("provider1.identity");\r
94         PRINCIPAL = props.getProperty("provider1.tenant1.userid", "appc");\r
95         CREDENTIAL = props.getProperty("provider1.tenant1.password", "appc");\r
96         DOMAIN = props.getProperty("provider1.tenant1.domain", "Default");\r
97         TENANT_NAME = props.getProperty("provider1.tenant1.name", "appc");\r
98         TENANT_ID = props.getProperty("provider1.tenant1.id",\r
99                 props.getProperty("test.tenantid", "abcde12345fghijk6789lmnopq123rst"));\r
100         REGION_NAME = props.getProperty("provider1.tenant1.region", "RegionOne");\r
101 \r
102         IP = props.getProperty("test.ip");\r
103         PORT = props.getProperty("test.port");\r
104         TENANTID = props.getProperty("test.tenantid");\r
105         VMID = props.getProperty("test.vmid");\r
106 \r
107         EXPECTED_REGIONS = Integer.valueOf(props.getProperty("test.expected-regions", "0"));\r
108         EXPECTED_ENDPOINTS = Integer.valueOf(props.getProperty("test.expected-endpoints", "0"));\r
109         PUBLIC_URL =\r
110                 "http://192.168.1.2:5000/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";\r
111 \r
112     }\r
113 \r
114     /**\r
115      * Use reflection to locate fields and methods so that they can be manipulated during the test\r
116      * to change the internal state accordingly.\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 ServiceCatalogV3(IDENTITY_URL, TENANT_NAME, PRINCIPAL, CREDENTIAL, DOMAIN, properties);\r
123         spyCatalog = Mockito.spy(catalog);\r
124         project.setId(TENANT_ID);\r
125         project.setName(TENANT_NAME);\r
126         final Service service = new Service();\r
127         serviceTypes = ImmutableMap.<String, Service>builder().put(ServiceCatalog.COMPUTE_SERVICE, service)\r
128                 .put(ServiceCatalog.IDENTITY_SERVICE, service).put(ServiceCatalog.IMAGE_SERVICE, service)\r
129                 .put(ServiceCatalog.NETWORK_SERVICE, service).put(ServiceCatalog.VOLUME_SERVICE, service).build();\r
130         final Service.Endpoint endpoint = new Service.Endpoint();\r
131         endpoint.setUrl(PUBLIC_URL);\r
132         endpoint.setRegion(REGION_NAME);\r
133         final List<Service.Endpoint> endpoints = Arrays.asList(endpoint);\r
134         serviceEndpoints = ImmutableMap.<String, List<Service.Endpoint>>builder()\r
135                 .put(ServiceCatalog.COMPUTE_SERVICE, endpoints).build();\r
136         Map<String, Object> privateFields =\r
137                 ImmutableMap.<String, Object>builder().put("project", project).put("regions", regions)\r
138                         .put("serviceTypes", serviceTypes).put("serviceEndpoints", serviceEndpoints).build();\r
139         CommonUtility.injectMockObjects(privateFields, catalog);\r
140         CommonUtility.injectMockObjectsInBaseClass(privateFields, catalog);\r
141     }\r
142 \r
143     /**\r
144      * Ensure that we get the Tenant Name & Tenant Id property are returned correctly\r
145      */\r
146     @Test\r
147     public void testKnownTenant() {\r
148         when(spyCatalog.getProject()).thenReturn(project);\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         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         String testString = catalog.toString();\r
201         assertNotNull(testString);\r
202     }\r
203 \r
204     /**\r
205      * Ensure that we can get the VM Region\r
206      */\r
207     @Test\r
208     public void testGetVMRegion() {\r
209         VMURL url = VMURL.parseURL(URL);\r
210         String region = catalog.getVMRegion(url);\r
211         assertEquals(REGION_NAME, region);\r
212     }\r
213 \r
214     /**\r
215      * Ensure that we can get the null region when no URL is passed\r
216      */\r
217     @Test\r
218     public void testGetVMRegionWithoutURL() {\r
219         String region = catalog.getVMRegion(null);\r
220         assertNull(region);\r
221     }\r
222 \r
223     @Ignore\r
224     @Test\r
225     public void liveConnectionTest() {\r
226         // this test should only be used by developers when testing against a live Openstack\r
227         // instance, otherwise it should be ignored\r
228         properties = new Properties();\r
229         String identity = "";\r
230         String tenantName = "";\r
231         String user = "";\r
232         String pass = "";\r
233 \r
234         catalog = new ServiceCatalogV3(IDENTITY_URL, TENANT_NAME, PRINCIPAL, CREDENTIAL, DOMAIN, properties);\r
235     }\r
236 }\r