added test case to TestRestartServer.java
[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     }
122
123     @Test
124     public void testDestroyWithException() throws IOException {
125         doThrow(new IOException("I/O Exception occured while closing context")).when(context).close();
126         TenantCache spy = Mockito.spy(tenantCache);
127         spy.destroy(context, pool);
128     }
129
130     @Test
131     public void testInitialize() {
132         TenantCache spyTenant = Mockito.spy(tenantCache);
133         when(catalog.getRegions()).thenReturn(regions);
134         when(catalog.getProjectId()).thenReturn(TENANT_ID);
135         when(catalog.getProjectName()).thenReturn(TENANT_NAME);
136         spyTenant.initialize();
137     }
138
139     @Test
140     public void testInitializeWithOverLimit() {
141         Configuration props = ConfigurationFactory.getConfiguration();
142         props.setProperty(Constants.PROPERTY_RETRY_LIMIT, "1");
143         TenantCache spyTenant = Mockito.spy(tenantCache);
144         when(spyTenant.getServiceCatalogFactory(anyString(), anyString(), anyObject())).thenReturn(catalog);
145         when(spyTenant.getTenantName()).thenReturn(TENANT_NAME);
146         when(catalog.getRegions()).thenReturn(regions);
147         spyTenant.initialize();
148     }
149
150     @Test
151     public void testInitializeWithContextConnectionException() throws ZoneException {
152         Configuration props = ConfigurationFactory.getConfiguration();
153         props.setProperty(Constants.PROPERTY_RETRY_LIMIT, "2");
154         props.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
155         doThrow(new ContextConnectionException("Contex Connection Exception")).when(catalog).init();
156         TenantCache spyTenant = Mockito.spy(tenantCache);
157         spyTenant.initialize();
158     }
159
160     @Test
161     public void testInitializeWithZoneException() throws ZoneException {
162         Configuration props = ConfigurationFactory.getConfiguration();
163         props.setProperty(Constants.PROPERTY_RETRY_LIMIT, "2");
164         props.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
165         doThrow(new ZoneException("Zone Exception")).when(catalog).init();
166         TenantCache spyTenant = Mockito.spy(tenantCache);
167         when(spyTenant.getServiceCatalogFactory(anyString(), anyString(), anyObject())).thenReturn(catalog);
168         spyTenant.initialize();
169     }
170
171     /**
172      * Ensure that we set up the Domain property correctly
173      */
174     @Test
175     public void testDomainProperty() {
176         assertEquals(DOMAIN, tenantCache.getDomain());
177     }
178
179     /**
180      * Ensure that we set up the Provider property correctly
181      */
182     @Test
183     public void testProviderProperty() {
184         assertEquals(provider, tenantCache.getProvider());
185     }
186
187     /**
188      * Ensure that we set up the Password property correctly
189      */
190     @Test
191     public void testPasswordProperty() {
192         assertEquals(CREDENTIAL, tenantCache.getPassword());
193     }
194
195     /**
196      * Ensure that we set up the Tenant Id property correctly
197      */
198     @Test
199     public void testTenantIdProperty() {
200         assertEquals(TENANT_ID, tenantCache.getTenantId());
201     }
202
203     /**
204      * Ensure that we set up the Tenant Name property correctly
205      */
206     @Test
207     public void testTenantNameProperty() {
208         assertEquals(TENANT_NAME, tenantCache.getTenantName());
209     }
210
211     /**
212      * Ensure that we set up the User Id property correctly
213      */
214     @Test
215     public void testUserIdProperty() {
216         assertEquals(CREDENTIAL, tenantCache.getUserid());
217     }
218
219     /**
220      * Ensure that we set up the Pools property correctly
221      */
222     @Test
223     public void testPoolsProperty() {
224         assertNotNull(tenantCache.getPools());
225     }
226     
227     @Test
228     public void testProvider() {
229         tenantCache.setProvider(provider);
230         assertEquals(provider, tenantCache.getProvider());
231     }
232     
233     @Test
234     public void testServiceCatalog() {
235         assertTrue(tenantCache.getServiceCatalog() instanceof ServiceCatalog);
236     }
237 }