2 * ============LICENSE_START======================================================= Copyright (C)
3 * 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================ Licensed under
5 * the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
15 * SPDX-License-Identifier: Apache-2.0
16 * ============LICENSE_END=========================================================
19 package org.onap.appc.adapter.iaas.impl;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.mockito.Matchers.anyObject;
24 import static org.mockito.Matchers.anyString;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.when;
27 import java.io.IOException;
28 import java.util.Arrays;
29 import java.util.HashSet;
31 import java.util.Properties;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.appc.Constants;
41 import org.onap.appc.configuration.Configuration;
42 import org.onap.appc.configuration.ConfigurationFactory;
43 import org.onap.appc.pool.Pool;
44 import com.att.cdp.exceptions.ContextConnectionException;
45 import com.att.cdp.exceptions.ZoneException;
46 import com.att.cdp.zones.Context;
47 import com.google.common.collect.ImmutableMap;
50 * This class is used to test methods and functions of the Tenant cache
52 @RunWith(MockitoJUnitRunner.class)
53 public class TestTenantCache {
55 private TenantCache tenantCache;
60 private ServiceCatalog catalog;
63 private Context context;
68 private ProviderCache provider;
70 private static String TENANT_NAME;
71 private static String TENANT_ID;
72 private static String IDENTITY_URL;
73 private static String REGION_NAME;
74 private static String CREDENTIAL;
75 private static String DOMAIN;
77 protected Set<String> regions = new HashSet<>(Arrays.asList("RegionOne"));
80 public static void before() {
81 Properties props = ConfigurationFactory.getConfiguration().getProperties();
82 IDENTITY_URL = props.getProperty("provider1.identity");
83 TENANT_NAME = props.getProperty("provider1.tenant1.name", "appc");
84 TENANT_ID = props.getProperty("provider1.tenant1.id",
85 props.getProperty("test.tenantid", "abcde12345fghijk6789lmnopq123rst"));
86 DOMAIN = props.getProperty("provider1.tenant1.domain", "Default");
87 CREDENTIAL = props.getProperty("provider1.tenant1.password", "appc");
88 REGION_NAME = props.getProperty("provider1.tenant1.region", "RegionOne");
93 Configuration props = ConfigurationFactory.getConfiguration();
94 props.setProperty(Constants.PROPERTY_RETRY_LIMIT, "3");
95 provider = new ProviderCache();
96 provider.setIdentityURL(IDENTITY_URL);
97 tenantCache = new TenantCache(provider);
98 tenantCache.setDomain(DOMAIN);
99 tenantCache.setPassword(CREDENTIAL);
100 tenantCache.setTenantId(TENANT_ID);
101 tenantCache.setTenantName(TENANT_NAME);
102 tenantCache.setUserid(CREDENTIAL);
103 props.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
104 Map<String, Object> privateFields = ImmutableMap.<String, Object>builder().put("catalog", catalog).build();
105 CommonUtility.injectMockObjects(privateFields, tenantCache);
109 public void testDetermineRegion() {
110 when(catalog.getVMRegion(url)).thenReturn(REGION_NAME);
111 assertEquals(REGION_NAME, tenantCache.determineRegion(url));
115 public void testDestroy() {
116 TenantCache spy = Mockito.spy(tenantCache);
117 spy.destroy(context, pool);
121 public void testDestroyWithException() throws IOException {
122 doThrow(new IOException("I/O Exception occured while closing context")).when(context).close();
123 TenantCache spy = Mockito.spy(tenantCache);
124 spy.destroy(context, pool);
128 public void testInitialize() {
129 TenantCache spyTenant = Mockito.spy(tenantCache);
130 when(catalog.getRegions()).thenReturn(regions);
131 when(catalog.getProjectId()).thenReturn(TENANT_ID);
132 when(catalog.getProjectName()).thenReturn(TENANT_NAME);
133 spyTenant.initialize();
137 public void testInitializeWithOverLimit() {
138 Configuration props = ConfigurationFactory.getConfiguration();
139 props.setProperty(Constants.PROPERTY_RETRY_LIMIT, "1");
140 TenantCache spyTenant = Mockito.spy(tenantCache);
141 when(spyTenant.getServiceCatalogFactory(anyString(), anyString(), anyObject())).thenReturn(catalog);
142 when(spyTenant.getTenantName()).thenReturn(TENANT_NAME);
143 when(catalog.getRegions()).thenReturn(regions);
144 spyTenant.initialize();
148 public void testInitializeWithContextConnectionException() throws ZoneException {
149 Configuration props = ConfigurationFactory.getConfiguration();
150 props.setProperty(Constants.PROPERTY_RETRY_LIMIT, "2");
151 props.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
152 doThrow(new ContextConnectionException("Contex Connection Exception")).when(catalog).init();
153 TenantCache spyTenant = Mockito.spy(tenantCache);
154 spyTenant.initialize();
158 public void testInitializeWithZoneException() throws ZoneException {
159 Configuration props = ConfigurationFactory.getConfiguration();
160 props.setProperty(Constants.PROPERTY_RETRY_LIMIT, "2");
161 props.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
162 doThrow(new ZoneException("Zone Exception")).when(catalog).init();
163 TenantCache spyTenant = Mockito.spy(tenantCache);
164 when(spyTenant.getServiceCatalogFactory(anyString(), anyString(), anyObject())).thenReturn(catalog);
165 spyTenant.initialize();
169 * Ensure that we set up the Domain property correctly
172 public void testDomainProperty() {
173 assertEquals(DOMAIN, tenantCache.getDomain());
177 * Ensure that we set up the Provider property correctly
180 public void testProviderProperty() {
181 assertEquals(provider, tenantCache.getProvider());
185 * Ensure that we set up the Password property correctly
188 public void testPasswordProperty() {
189 assertEquals(CREDENTIAL, tenantCache.getPassword());
193 * Ensure that we set up the Tenant Id property correctly
196 public void testTenantIdProperty() {
197 assertEquals(TENANT_ID, tenantCache.getTenantId());
201 * Ensure that we set up the Tenant Name property correctly
204 public void testTenantNameProperty() {
205 assertEquals(TENANT_NAME, tenantCache.getTenantName());
209 * Ensure that we set up the User Id property correctly
212 public void testUserIdProperty() {
213 assertEquals(CREDENTIAL, tenantCache.getUserid());
217 * Ensure that we set up the Pools property correctly
220 public void testPoolsProperty() {
221 assertNotNull(tenantCache.getPools());