added assert statements in jUnits
[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======================================================= 
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
9  * 
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.appc.adapter.iaas.impl;
22
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;
33 import java.util.Map;
34 import java.util.Properties;
35 import java.util.Set;
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;
51
52 /**
53  * This class is used to test methods and functions of the Tenant cache
54  */
55 @RunWith(MockitoJUnitRunner.class)
56 public class TestTenantCache {
57
58     private TenantCache tenantCache;
59
60     private VMURL url;
61
62     @Mock
63     private ServiceCatalog catalog;
64
65     @Mock
66     private Context context;
67
68     @Mock
69     Pool<Context> pool;
70
71     private ProviderCache provider;
72
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;
79
80     protected Set<String> regions = new HashSet<>(Arrays.asList("RegionOne"));
81
82     @BeforeClass
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");
92     }
93
94     @Before
95     public void setup() {
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);
109     }
110
111     @Test
112     public void testDetermineRegion() {
113         when(catalog.getVMRegion(url)).thenReturn(REGION_NAME);
114         assertEquals(REGION_NAME, tenantCache.determineRegion(url));
115     }
116
117     @Test
118     public void testDestroy() {
119         TenantCache spy = Mockito.spy(tenantCache);
120         spy.destroy(context, pool);
121         assertNotNull(spy);
122     }
123
124     @Test
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);
129         assertNotNull(spy);
130     }
131
132     @Test
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);
140     }
141
142     @Test
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);
152     }
153
154     @Test
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);
163     }
164
165     @Test
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();
174     }
175
176     /**
177      * Ensure that we set up the Domain property correctly
178      */
179     @Test
180     public void testDomainProperty() {
181         assertEquals(DOMAIN, tenantCache.getDomain());
182     }
183
184     /**
185      * Ensure that we set up the Provider property correctly
186      */
187     @Test
188     public void testProviderProperty() {
189         assertEquals(provider, tenantCache.getProvider());
190     }
191
192     /**
193      * Ensure that we set up the Password property correctly
194      */
195     @Test
196     public void testPasswordProperty() {
197         assertEquals(CREDENTIAL, tenantCache.getPassword());
198     }
199
200     /**
201      * Ensure that we set up the Tenant Id property correctly
202      */
203     @Test
204     public void testTenantIdProperty() {
205         assertEquals(TENANT_ID, tenantCache.getTenantId());
206     }
207
208     /**
209      * Ensure that we set up the Tenant Name property correctly
210      */
211     @Test
212     public void testTenantNameProperty() {
213         assertEquals(TENANT_NAME, tenantCache.getTenantName());
214     }
215
216     /**
217      * Ensure that we set up the User Id property correctly
218      */
219     @Test
220     public void testUserIdProperty() {
221         assertEquals(CREDENTIAL, tenantCache.getUserid());
222     }
223
224     /**
225      * Ensure that we set up the Pools property correctly
226      */
227     @Test
228     public void testPoolsProperty() {
229         assertNotNull(tenantCache.getPools());
230     }
231     
232     @Test
233     public void testProvider() {
234         tenantCache.setProvider(provider);
235         assertEquals(provider, tenantCache.getProvider());
236     }
237     
238     @Test
239     public void testServiceCatalog() {
240         assertTrue(tenantCache.getServiceCatalog() instanceof ServiceCatalog);
241     }
242 }