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 / TestTenantCache.java
1 /*
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
7  * 
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * the License.
14  *
15  * SPDX-License-Identifier: Apache-2.0
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.appc.adapter.iaas.impl;
20
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;
30 import java.util.Map;
31 import java.util.Properties;
32 import java.util.Set;
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;
48
49 /**
50  * This class is used to test methods and functions of the Tenant cache
51  */
52 @RunWith(MockitoJUnitRunner.class)
53 public class TestTenantCache {
54
55     private TenantCache tenantCache;
56
57     private VMURL url;
58
59     @Mock
60     private ServiceCatalog catalog;
61
62     @Mock
63     private Context context;
64
65     @Mock
66     Pool<Context> pool;
67
68     private ProviderCache provider;
69
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;
76
77     protected Set<String> regions = new HashSet<>(Arrays.asList("RegionOne"));
78
79     @BeforeClass
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");
89     }
90
91     @Before
92     public void setup() {
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);
106     }
107
108     @Test
109     public void testDetermineRegion() {
110         when(catalog.getVMRegion(url)).thenReturn(REGION_NAME);
111         assertEquals(REGION_NAME, tenantCache.determineRegion(url));
112     }
113
114     @Test
115     public void testDestroy() {
116         TenantCache spy = Mockito.spy(tenantCache);
117         spy.destroy(context, pool);
118     }
119
120     @Test
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);
125     }
126
127     @Test
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();
134     }
135
136     @Test
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();
145     }
146
147     @Test
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();
155     }
156
157     @Test
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();
166     }
167
168     /**
169      * Ensure that we set up the Domain property correctly
170      */
171     @Test
172     public void testDomainProperty() {
173         assertEquals(DOMAIN, tenantCache.getDomain());
174     }
175
176     /**
177      * Ensure that we set up the Provider property correctly
178      */
179     @Test
180     public void testProviderProperty() {
181         assertEquals(provider, tenantCache.getProvider());
182     }
183
184     /**
185      * Ensure that we set up the Password property correctly
186      */
187     @Test
188     public void testPasswordProperty() {
189         assertEquals(CREDENTIAL, tenantCache.getPassword());
190     }
191
192     /**
193      * Ensure that we set up the Tenant Id property correctly
194      */
195     @Test
196     public void testTenantIdProperty() {
197         assertEquals(TENANT_ID, tenantCache.getTenantId());
198     }
199
200     /**
201      * Ensure that we set up the Tenant Name property correctly
202      */
203     @Test
204     public void testTenantNameProperty() {
205         assertEquals(TENANT_NAME, tenantCache.getTenantName());
206     }
207
208     /**
209      * Ensure that we set up the User Id property correctly
210      */
211     @Test
212     public void testUserIdProperty() {
213         assertEquals(CREDENTIAL, tenantCache.getUserid());
214     }
215
216     /**
217      * Ensure that we set up the Pools property correctly
218      */
219     @Test
220     public void testPoolsProperty() {
221         assertNotNull(tenantCache.getPools());
222     }
223 }