2 * ============LICENSE_START=======================================================
\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 * Modifications Copyright (C) 2019 Ericsson
\r
10 * =============================================================================
\r
11 * Licensed under the Apache License, Version 2.0 (the "License");
\r
12 * you may not use this file except in compliance with the License.
\r
13 * You may obtain a copy of the License at
\r
15 * http://www.apache.org/licenses/LICENSE-2.0
\r
17 * Unless required by applicable law or agreed to in writing, software
\r
18 * distributed under the License is distributed on an "AS IS" BASIS,
\r
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
20 * See the License for the specific language governing permissions and
\r
21 * limitations under the License.
\r
23 * ============LICENSE_END=========================================================
\r
26 package org.onap.appc.adapter.iaas.impl;
\r
28 import static org.junit.Assert.assertEquals;
\r
29 import static org.junit.Assert.assertFalse;
\r
30 import static org.junit.Assert.assertNotNull;
\r
31 import static org.junit.Assert.assertNull;
\r
32 import static org.junit.Assert.assertTrue;
\r
33 import static org.mockito.Mockito.when;
\r
34 import java.util.ArrayList;
\r
35 import java.util.Arrays;
\r
36 import java.util.HashSet;
\r
37 import java.util.List;
\r
38 import java.util.Map;
\r
39 import java.util.Properties;
\r
40 import java.util.Set;
\r
41 import org.junit.Before;
\r
42 import org.junit.BeforeClass;
\r
43 import org.junit.Ignore;
\r
44 import org.junit.Test;
\r
45 import org.junit.runner.RunWith;
\r
46 import org.mockito.Mockito;
\r
47 import org.mockito.runners.MockitoJUnitRunner;
\r
48 import org.onap.appc.configuration.ConfigurationFactory;
\r
49 import org.powermock.reflect.Whitebox;
\r
50 import com.att.cdp.exceptions.ZoneException;
\r
51 import com.att.cdp.zones.ContextFactory;
\r
52 import com.google.common.collect.ImmutableMap;
\r
53 import com.woorea.openstack.base.client.OpenStackClientConnector;
\r
54 import com.woorea.openstack.base.client.OpenStackConnectException;
\r
55 import com.woorea.openstack.base.client.OpenStackResponseException;
\r
56 import com.woorea.openstack.keystone.v3.Keystone;
\r
57 import com.woorea.openstack.keystone.v3.api.TokensResource;
\r
58 import com.woorea.openstack.keystone.v3.model.Token;
\r
59 import com.woorea.openstack.keystone.v3.model.Token.Service;
\r
60 import com.woorea.openstack.keystone.v3.model.Token.Service.Endpoint;
\r
63 * This class tests the service catalog against a known provider.
\r
65 @RunWith(MockitoJUnitRunner.class)
\r
66 public class TestServiceCatalogV3 {
\r
69 private static int EXPECTED_REGIONS = 1;
\r
70 private static int EXPECTED_ENDPOINTS = 1;
\r
72 private static String PRINCIPAL;
\r
73 private static String CREDENTIAL;
\r
74 private static String DOMAIN;
\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
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
87 private ServiceCatalogV3 catalog;
\r
89 private ServiceCatalogV3 spyCatalog;
\r
91 private Properties properties;
\r
93 private Token.Project project = new Token.Project();
\r
95 private final Set<String> regions = new HashSet<>(Arrays.asList("RegionOne"));
\r
97 private Map<String, Service> serviceTypes;
\r
99 private Map<String, List<Service.Endpoint>> serviceEndpoints;
\r
102 public static void before() {
\r
103 Properties props = ConfigurationFactory.getConfiguration().getProperties();
\r
104 IDENTITY_URL = props.getProperty("provider1.identity");
\r
105 PRINCIPAL = props.getProperty("provider1.tenant1.userid", "appc");
\r
106 CREDENTIAL = props.getProperty("provider1.tenant1.password", "appc");
\r
107 DOMAIN = props.getProperty("provider1.tenant1.domain", "Default");
\r
108 TENANT_NAME = props.getProperty("provider1.tenant1.name", "appc");
\r
109 TENANT_ID = props.getProperty("provider1.tenant1.id",
\r
110 props.getProperty("test.tenantid", "abcde12345fghijk6789lmnopq123rst"));
\r
111 REGION_NAME = props.getProperty("provider1.tenant1.region", "RegionOne");
\r
113 IP = props.getProperty("test.ip");
\r
114 PORT = props.getProperty("test.port");
\r
115 TENANTID = props.getProperty("test.tenantid");
\r
116 VMID = props.getProperty("test.vmid");
\r
118 EXPECTED_REGIONS = Integer.valueOf(props.getProperty("test.expected-regions", "0"));
\r
119 EXPECTED_ENDPOINTS = Integer.valueOf(props.getProperty("test.expected-endpoints", "0"));
\r
121 "http://192.168.1.2:5000/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345";
\r
126 * Use reflection to locate fields and methods so that they can be manipulated during the test
\r
127 * to change the internal state accordingly.
\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 ServiceCatalogV3(IDENTITY_URL, TENANT_NAME, PRINCIPAL, CREDENTIAL, DOMAIN, properties);
\r
136 spyCatalog = Mockito.spy(catalog);
\r
137 project.setId(TENANT_ID);
\r
138 project.setName(TENANT_NAME);
\r
139 final Service service = new Service();
\r
140 serviceTypes = ImmutableMap.<String, Service>builder().put(ServiceCatalog.COMPUTE_SERVICE, service)
\r
141 .put(ServiceCatalog.IDENTITY_SERVICE, service).put(ServiceCatalog.IMAGE_SERVICE, service)
\r
142 .put(ServiceCatalog.NETWORK_SERVICE, service).put(ServiceCatalog.VOLUME_SERVICE, service).build();
\r
143 final Service.Endpoint endpoint = new Service.Endpoint();
\r
144 endpoint.setUrl(PUBLIC_URL);
\r
145 endpoint.setRegion(REGION_NAME);
\r
146 final List<Service.Endpoint> endpoints = Arrays.asList(endpoint);
\r
147 serviceEndpoints = ImmutableMap.<String, List<Service.Endpoint>>builder()
\r
148 .put(ServiceCatalog.COMPUTE_SERVICE, endpoints).build();
\r
149 Map<String, Object> privateFields =
\r
150 ImmutableMap.<String, Object>builder().put("project", project).put("regions", regions)
\r
151 .put("serviceTypes", serviceTypes).put("serviceEndpoints", serviceEndpoints).build();
\r
152 CommonUtility.injectMockObjects(privateFields, catalog);
\r
153 CommonUtility.injectMockObjectsInBaseClass(privateFields, catalog);
\r
157 * Ensure that we get the Tenant Name & Tenant Id property are returned correctly
\r
160 public void testKnownTenant() {
\r
161 when(spyCatalog.getProject()).thenReturn(project);
\r
162 assertEquals(TENANT_NAME, catalog.getProjectName());
\r
163 assertEquals(TENANT_ID, catalog.getProjectId());
\r
167 * Ensure that we set up the Region property correctly
\r
170 public void testKnownRegions() {
\r
171 assertEquals(EXPECTED_REGIONS, catalog.getRegions().size());
\r
172 assertEquals(REGION_NAME, catalog.getRegions().toArray()[0]);
\r
176 * Ensure that that we can check for published services correctly
\r
179 public void testServiceTypesPublished() {
\r
180 assertTrue(catalog.isServicePublished("compute"));
\r
181 assertFalse(catalog.isServicePublished("bogus"));
\r
185 * Ensure that we can get the list of published services
\r
188 public void testPublishedServicesList() {
\r
189 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
198 * Ensure that we can get the endpoint(s) for a service
\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
209 * Ensure that we override the toString method
\r
212 public void testToString() {
\r
213 String testString = catalog.toString();
\r
214 assertNotNull(testString);
\r
218 * Ensure that we can get the VM Region
\r
221 public void testGetVMRegion() {
\r
222 VMURL url = VMURL.parseURL(URL);
\r
223 String region = catalog.getVMRegion(url);
\r
224 assertEquals(REGION_NAME, region);
\r
228 * Ensure that we can get the null region when no URL is passed
\r
231 public void testGetVMRegionWithoutURL() {
\r
232 String region = catalog.getVMRegion(null);
\r
233 assertNull(region);
\r
238 public void liveConnectionTest() {
\r
239 // this test should only be used by developers when testing against a live Openstack
\r
240 // instance, otherwise it should be ignored
\r
241 properties = new Properties();
\r
242 String identity = "";
\r
243 String tenantName = "";
\r
247 catalog = new ServiceCatalogV3(IDENTITY_URL, TENANT_NAME, PRINCIPAL, CREDENTIAL, DOMAIN, properties);
\r
248 assertNotNull(catalog);
\r
252 public void testInit() throws ZoneException, ClassNotFoundException, InstantiationException, IllegalAccessException, OpenStackConnectException, OpenStackResponseException {
\r
253 ServiceCatalogV3 catalogSpy = Mockito.spy(catalog);
\r
254 Class<?> connectorClass = Class.forName(ServiceCatalogV2.CLIENT_CONNECTOR_CLASS);
\r
255 OpenStackClientConnector connector = (OpenStackClientConnector) connectorClass.newInstance();
\r
256 Keystone keystone = Mockito.spy(new Keystone(IDENTITY_URL, connector));
\r
257 TokensResource tokens = Mockito.mock(TokensResource.class);
\r
258 TokensResource.Authenticate authenticate = Mockito.mock(TokensResource.Authenticate.class);
\r
259 Mockito.when(keystone.tokens()).thenReturn(tokens);
\r
260 Mockito.when(tokens.authenticate(Mockito.any())).thenReturn(authenticate);
\r
261 Token token = Mockito.mock(Token.class);
\r
263 Mockito.when(authenticate.execute()).thenReturn(token);
\r
264 //Mockito.when(authenticate.withTenantName(Mockito.anyString())).thenReturn(authenticate);
\r
265 Mockito.when(catalogSpy.getKeystone(Mockito.anyString(), Mockito.any())).thenReturn(keystone);
\r
266 Endpoint endpoint = new Endpoint();
\r
267 List<Endpoint> endpointList = new ArrayList<>();
\r
268 endpointList.add(endpoint);
\r
269 List<Token.Service> serviceList = new ArrayList<>();
\r
270 Token.Service service = new Token.Service();
\r
271 Whitebox.setInternalState(service, "endpoints", endpointList);
\r
272 serviceList.add(service);
\r
273 Mockito.when(token.getCatalog()).thenReturn(serviceList);
\r
275 Mockito.verify(token).getCatalog();
\r