2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Modifications Copyright (C) 2019 IBM.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
8 * file except in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.appc.adapter.iaas.impl;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Matchers.anyObject;
27 import static org.mockito.Matchers.anyString;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.when;
30 import java.io.IOException;
31 import java.util.Arrays;
32 import java.util.HashSet;
34 import java.util.Properties;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.Mockito;
42 import org.mockito.runners.MockitoJUnitRunner;
43 import org.onap.appc.Constants;
44 import org.onap.appc.configuration.Configuration;
45 import org.onap.appc.configuration.ConfigurationFactory;
46 import org.onap.appc.pool.Pool;
47 import com.att.cdp.exceptions.ContextConnectionException;
48 import com.att.cdp.exceptions.ZoneException;
49 import com.att.cdp.zones.Context;
50 import com.google.common.collect.ImmutableMap;
53 * This class is used to test methods and functions of the Tenant cache
55 @RunWith(MockitoJUnitRunner.class)
56 public class TestTenantCache {
58 private TenantCache tenantCache;
63 private ServiceCatalog catalog;
66 private Context context;
71 private ProviderCache provider;
73 private static String TENANT_NAME;
74 private static String TENANT_ID;
75 private static String IDENTITY_URL;
76 private static String REGION_NAME;
77 private static String CREDENTIAL;
78 private static String DOMAIN;
80 protected Set<String> regions = new HashSet<>(Arrays.asList("RegionOne"));
83 public static void before() {
84 Properties props = ConfigurationFactory.getConfiguration().getProperties();
85 IDENTITY_URL = props.getProperty("provider1.identity");
86 TENANT_NAME = props.getProperty("provider1.tenant1.name", "appc");
87 TENANT_ID = props.getProperty("provider1.tenant1.id",
88 props.getProperty("test.tenantid", "abcde12345fghijk6789lmnopq123rst"));
89 DOMAIN = props.getProperty("provider1.tenant1.domain", "Default");
90 CREDENTIAL = props.getProperty("provider1.tenant1.password", "appc");
91 REGION_NAME = props.getProperty("provider1.tenant1.region", "RegionOne");
96 Configuration props = ConfigurationFactory.getConfiguration();
97 props.setProperty(Constants.PROPERTY_RETRY_LIMIT, "3");
98 provider = new ProviderCache();
99 provider.setIdentityURL(IDENTITY_URL);
100 tenantCache = new TenantCache(provider);
101 tenantCache.setDomain(DOMAIN);
102 tenantCache.setPassword(CREDENTIAL);
103 tenantCache.setTenantId(TENANT_ID);
104 tenantCache.setTenantName(TENANT_NAME);
105 tenantCache.setUserid(CREDENTIAL);
106 props.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
107 Map<String, Object> privateFields = ImmutableMap.<String, Object>builder().put("catalog", catalog).build();
108 CommonUtility.injectMockObjects(privateFields, tenantCache);
112 public void testDetermineRegion() {
113 when(catalog.getVMRegion(url)).thenReturn(REGION_NAME);
114 assertEquals(REGION_NAME, tenantCache.determineRegion(url));
118 public void testDestroy() {
119 TenantCache spy = Mockito.spy(tenantCache);
120 spy.destroy(context, pool);
125 public void testDestroyWithException() throws IOException {
126 doThrow(new IOException("I/O Exception occured while closing context")).when(context).close();
127 TenantCache spy = Mockito.spy(tenantCache);
128 spy.destroy(context, pool);
133 public void testInitialize() {
134 TenantCache spyTenant = Mockito.spy(tenantCache);
135 when(catalog.getRegions()).thenReturn(regions);
136 when(catalog.getProjectId()).thenReturn(TENANT_ID);
137 when(catalog.getProjectName()).thenReturn(TENANT_NAME);
138 spyTenant.initialize();
139 assertNotNull(spyTenant);
143 public void testInitializeWithOverLimit() {
144 Configuration props = ConfigurationFactory.getConfiguration();
145 props.setProperty(Constants.PROPERTY_RETRY_LIMIT, "1");
146 TenantCache spyTenant = Mockito.spy(tenantCache);
147 when(spyTenant.getServiceCatalogFactory(anyString(), anyString(), anyObject())).thenReturn(catalog);
148 when(spyTenant.getTenantName()).thenReturn(TENANT_NAME);
149 when(catalog.getRegions()).thenReturn(regions);
150 spyTenant.initialize();
151 assertNotNull(props);
155 public void testInitializeWithContextConnectionException() throws ZoneException {
156 Configuration props = ConfigurationFactory.getConfiguration();
157 props.setProperty(Constants.PROPERTY_RETRY_LIMIT, "2");
158 props.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
159 doThrow(new ContextConnectionException("Contex Connection Exception")).when(catalog).init();
160 TenantCache spyTenant = Mockito.spy(tenantCache);
161 spyTenant.initialize();
162 assertNotNull(props);
166 public void testInitializeWithZoneException() throws ZoneException {
167 Configuration props = ConfigurationFactory.getConfiguration();
168 props.setProperty(Constants.PROPERTY_RETRY_LIMIT, "2");
169 props.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
170 doThrow(new ZoneException("Zone Exception")).when(catalog).init();
171 TenantCache spyTenant = Mockito.spy(tenantCache);
172 when(spyTenant.getServiceCatalogFactory(anyString(), anyString(), anyObject())).thenReturn(catalog);
173 spyTenant.initialize();
177 * Ensure that we set up the Domain property correctly
180 public void testDomainProperty() {
181 assertEquals(DOMAIN, tenantCache.getDomain());
185 * Ensure that we set up the Provider property correctly
188 public void testProviderProperty() {
189 assertEquals(provider, tenantCache.getProvider());
193 * Ensure that we set up the Password property correctly
196 public void testPasswordProperty() {
197 assertEquals(CREDENTIAL, tenantCache.getPassword());
201 * Ensure that we set up the Tenant Id property correctly
204 public void testTenantIdProperty() {
205 assertEquals(TENANT_ID, tenantCache.getTenantId());
209 * Ensure that we set up the Tenant Name property correctly
212 public void testTenantNameProperty() {
213 assertEquals(TENANT_NAME, tenantCache.getTenantName());
217 * Ensure that we set up the User Id property correctly
220 public void testUserIdProperty() {
221 assertEquals(CREDENTIAL, tenantCache.getUserid());
225 * Ensure that we set up the Pools property correctly
228 public void testPoolsProperty() {
229 assertNotNull(tenantCache.getPools());
233 public void testProvider() {
234 tenantCache.setProvider(provider);
235 assertEquals(provider, tenantCache.getProvider());
239 public void testServiceCatalog() {
240 assertTrue(tenantCache.getServiceCatalog() instanceof ServiceCatalog);