fixed JUnits in 3 files
[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         assertNotNull(props);
175     }
176
177     /**
178      * Ensure that we set up the Domain property correctly
179      */
180     @Test
181     public void testDomainProperty() {
182         assertEquals(DOMAIN, tenantCache.getDomain());
183     }
184
185     /**
186      * Ensure that we set up the Provider property correctly
187      */
188     @Test
189     public void testProviderProperty() {
190         assertEquals(provider, tenantCache.getProvider());
191     }
192
193     /**
194      * Ensure that we set up the Password property correctly
195      */
196     @Test
197     public void testPasswordProperty() {
198         assertEquals(CREDENTIAL, tenantCache.getPassword());
199     }
200
201     /**
202      * Ensure that we set up the Tenant Id property correctly
203      */
204     @Test
205     public void testTenantIdProperty() {
206         assertEquals(TENANT_ID, tenantCache.getTenantId());
207     }
208
209     /**
210      * Ensure that we set up the Tenant Name property correctly
211      */
212     @Test
213     public void testTenantNameProperty() {
214         assertEquals(TENANT_NAME, tenantCache.getTenantName());
215     }
216
217     /**
218      * Ensure that we set up the User Id property correctly
219      */
220     @Test
221     public void testUserIdProperty() {
222         assertEquals(CREDENTIAL, tenantCache.getUserid());
223     }
224
225     /**
226      * Ensure that we set up the Pools property correctly
227      */
228     @Test
229     public void testPoolsProperty() {
230         assertNotNull(tenantCache.getPools());
231     }
232     
233     @Test
234     public void testProvider() {
235         tenantCache.setProvider(provider);
236         assertEquals(provider, tenantCache.getProvider());
237     }
238     
239     @Test
240     public void testServiceCatalog() {
241         assertTrue(tenantCache.getServiceCatalog() instanceof ServiceCatalog);
242     }
243 }