2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 * in compliance 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.provider.operation.impl.base;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23 import static org.junit.Assert.assertNull;
24 import static org.mockito.Mockito.any;
25 import static org.mockito.Mockito.when;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.doThrow;
30 import static org.mockito.Mockito.verify;
32 import org.junit.Test;
33 import org.mockito.Mockito;
34 import com.att.cdp.openstack.OpenStackContext;
35 import com.att.cdp.zones.model.Server.Status;
36 import org.onap.appc.adapter.iaas.impl.ProviderCache;
37 import org.onap.appc.adapter.iaas.impl.RequestContext;
38 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
39 import org.onap.appc.adapter.iaas.impl.TenantCache;
40 import org.onap.appc.adapter.iaas.impl.VMURL;
41 import org.onap.appc.adapter.iaas.provider.operation.impl.AttachVolumeServer;
42 import org.onap.appc.adapter.iaas.provider.operation.impl.MockGenerator;
43 import org.onap.appc.configuration.ConfigurationFactory;
44 import org.onap.appc.exceptions.APPCException;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
46 import org.onap.appc.pool.Pool;
47 import org.onap.appc.pool.PoolDrainedException;
48 import org.onap.appc.pool.PoolExtensionException;
49 import org.glassfish.grizzly.http.util.HttpStatus;
50 import java.util.HashMap;
52 import java.util.Properties;
55 public class TestProviderOperation {
57 ProviderServerOperation underTest = spy(new AttachVolumeServer());
60 public void testDoFailureRequestContextHttpStatusString() throws APPCException {
61 RequestContext rc = mock(RequestContext.class);
62 MockGenerator mg = new MockGenerator(Status.RUNNING);
63 SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
64 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
65 HttpStatus code = HttpStatus.NOT_FOUND_404;
66 String message = "PALOS\n";
67 underTest.doFailure(rc, code, message);
68 verify(underTest).doFailure(rc, code, message, null);
71 @Test(expected = APPCException.class)
72 public void testDoFailureRequestContextHttpStatusStringException() throws APPCException {
73 RequestContext rc = mock(RequestContext.class);
74 MockGenerator mg = new MockGenerator(Status.RUNNING);
75 SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
76 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
77 HttpStatus code = spy(HttpStatus.NOT_FOUND_404);
78 doThrow(new RuntimeException("TEST")).when(code).getStatusCode();
79 String message = "PALOS\n";
80 underTest.doFailure(rc, code, message, new Throwable("TEST"));
81 verify(underTest).doFailure(rc, code, message, null);
85 public void testDoFailureRequestContextHttpStatusStringAPPCException() throws APPCException {
86 RequestContext rc = mock(RequestContext.class);
87 MockGenerator mg = new MockGenerator(Status.RUNNING);
88 SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
89 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
90 HttpStatus code = HttpStatus.NOT_FOUND_404;
91 String message = "PALOS\n";
92 doThrow(new APPCException("TEST")).when(underTest).doFailure(rc, code, message, null);
93 underTest.doFailure(rc, code, message);
94 verify(underTest).doFailure(rc, code, message, null);
97 @Test(expected = APPCException.class)
98 public void testDoFailureRequestContextHttpStatusStringThrowableAPPCException() throws APPCException {
99 RequestContext rc = mock(RequestContext.class);
100 MockGenerator mg = new MockGenerator(Status.RUNNING);
101 SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
102 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
103 HttpStatus code = HttpStatus.NOT_FOUND_404;
104 String message = "PALOS\n";
105 underTest.doFailure(rc, code, message, new Throwable());
109 public void testValidateVM() throws RequestFailedException {
110 MockGenerator mg = new MockGenerator(Status.RUNNING);
111 SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
112 RequestContext rc = mock(RequestContext.class);
113 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
114 assertTrue(underTest.validateVM(rc, "TEST", "TEST", null));
118 public void testGetContextNullVM() {
119 MockGenerator mg = new MockGenerator(Status.RUNNING);
120 RequestContext rc = mock(RequestContext.class);
121 SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
122 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
123 assertNull(underTest.getContext(rc, "%£$%^$", "%£$%^$"));
124 verify(underTest).doFailure(Mockito.any(RequestContext.class), Mockito.any(HttpStatus.class),
125 Mockito.anyString());
129 public void testGetContextNullCache() {
130 MockGenerator mg = new MockGenerator(Status.RUNNING);
131 RequestContext rc = mock(RequestContext.class);
132 SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
133 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
134 underTest.setProviderCache(mg.getProviderCacheMap());
135 assertNull(underTest.getContext(rc, "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
136 + "abc12345-1234-5678-890a-abcdefb12345", "%£$%^$"));
137 verify(underTest).doFailure(Mockito.any(RequestContext.class), Mockito.any(HttpStatus.class),
138 Mockito.anyString());
142 public void testGetContextNullTenantCache() {
143 MockGenerator mg = new MockGenerator(Status.RUNNING);
144 RequestContext rc = mock(RequestContext.class);
145 SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
146 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
147 Map<String, ProviderCache> providerCacheMap = mg.getProviderCacheMap();
148 providerCacheMap.put("TEST", mock(ProviderCache.class));
149 underTest.setProviderCache(mg.getProviderCacheMap());
150 assertNull(underTest.getContext(rc, "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
151 + "abc12345-1234-5678-890a-abcdefb12345", "TEST"));
152 verify(underTest).doFailure(Mockito.any(RequestContext.class), Mockito.any(HttpStatus.class),
153 Mockito.anyString());
157 public void testGetContextPoolNullRegion() {
158 MockGenerator mg = new MockGenerator(Status.RUNNING);
159 RequestContext rc = mock(RequestContext.class);
160 SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
161 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
162 Map<String, ProviderCache> providerCacheMap = mg.getProviderCacheMap();
163 TenantCache tenantCache = mock(TenantCache.class);
164 when(tenantCache.getTenantName()).thenReturn("TEST");
165 when(tenantCache.getTenantId()).thenReturn("TEST");
166 when(tenantCache.determineRegion(Mockito.anyObject())).thenReturn(null);
167 ProviderCache providerCache = mock(ProviderCache.class);
168 when(providerCache.getTenant(Mockito.anyString())).thenReturn(tenantCache);
169 providerCacheMap.put("TEST", providerCache);
170 underTest.setProviderCache(mg.getProviderCacheMap());
171 assertNull(underTest.getContext(rc, "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
172 + "abc12345-1234-5678-890a-abcdefb12345", "TEST"));
173 verify(underTest).doFailure(Mockito.any(RequestContext.class), Mockito.any(HttpStatus.class),
174 Mockito.anyString());
178 public void testGetContextAttemptFailed() {
179 MockGenerator mg = new MockGenerator(Status.RUNNING);
180 RequestContext rc = mock(RequestContext.class);
181 SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
182 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
183 underTest.setProviderCache(mg.getProviderCacheMap());
184 assertNull(underTest.getContext(rc,
185 "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
186 + "abc12345-1234-5678-890a-abcdefb12345",
187 "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3"));
191 public void testGetContextRelogin() throws PoolExtensionException, PoolDrainedException {
192 RequestContext rc = mock(RequestContext.class);
193 SvcLogicContext svcLogicContext = mock(SvcLogicContext.class);
194 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
195 ProviderCache providerCache = mock(ProviderCache.class);
196 Map<String, ProviderCache> providerCacheMap = new HashMap<String, ProviderCache>();
197 providerCacheMap.put("http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3", providerCache);
198 when(rc.attempt()).thenReturn(true).thenReturn(false);
199 OpenStackContext context = mock(OpenStackContext.class);
200 when(context.isStale()).thenReturn(true);
201 TenantCache tenantCache = mock(TenantCache.class);
202 doReturn("cloudowner_region").when(tenantCache).determineRegion(any(VMURL.class));
203 doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantId();
204 doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantName();
205 Pool pool = mock(Pool.class);
206 Map<String, Pool> tenantCachePools = new HashMap<String, Pool>();
207 tenantCachePools.put("cloudowner_region", pool);
208 doReturn(tenantCachePools).when(tenantCache).getPools();
209 when(providerCache.getTenant(Mockito.anyString())).thenReturn(tenantCache);
210 doReturn(tenantCache).when(providerCache).getTenant(Mockito.anyString());
211 doReturn(context).when(pool).reserve();
212 underTest.setProviderCache(providerCacheMap);
213 assertTrue(underTest.getContext(rc,
214 "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
215 + "abc12345-1234-5678-890a-abcdefb12345",
216 "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3") instanceof OpenStackContext);
220 public void testGetContextPoolException() throws PoolExtensionException, PoolDrainedException {
221 RequestContext rc = mock(RequestContext.class);
222 SvcLogicContext svcLogicContext = mock(SvcLogicContext.class);
223 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
224 ProviderCache providerCache = mock(ProviderCache.class);
225 Map<String, ProviderCache> providerCacheMap = new HashMap<String, ProviderCache>();
226 providerCacheMap.put("http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3", providerCache);
228 when(rc.attempt()).thenReturn(true).thenReturn(false);
229 OpenStackContext context = mock(OpenStackContext.class);
230 when(context.isStale()).thenReturn(true);
231 TenantCache tenantCache = mock(TenantCache.class);
232 doReturn("cloudowner_region").when(tenantCache).determineRegion(any(VMURL.class));
233 doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantId();
234 doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantName();
235 Pool pool = mock(Pool.class);
236 Map<String, Pool> tenantCachePools = new HashMap<String, Pool>();
237 tenantCachePools.put("cloudowner_region", pool);
238 doReturn(tenantCachePools).when(tenantCache).getPools();
239 when(providerCache.getTenant(Mockito.anyString())).thenReturn(tenantCache);
240 doReturn(tenantCache).when(providerCache).getTenant(Mockito.anyString());
241 doThrow(new PoolExtensionException("TEST")).when(pool).reserve();
242 underTest.setProviderCache(providerCacheMap);
243 underTest.getContext(rc,
244 "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
245 + "abc12345-1234-5678-890a-abcdefb12345",
246 "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3");
251 public void testGetContextException() {
252 RequestContext rc = mock(RequestContext.class);
253 SvcLogicContext svcLogicContext = mock(SvcLogicContext.class);
254 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
255 ProviderCache providerCache = mock(ProviderCache.class);
256 Map<String, ProviderCache> providerCacheMap = new HashMap<String, ProviderCache>();
257 providerCacheMap.put("http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3", providerCache);
259 when(rc.attempt()).thenReturn(true).thenReturn(false);
260 OpenStackContext context = mock(OpenStackContext.class);
261 when(context.isStale()).thenReturn(true);
262 TenantCache tenantCache = mock(TenantCache.class);
263 doReturn("cloudowner_region").when(tenantCache).determineRegion(any(VMURL.class));
264 doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantId();
265 doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantName();
266 Pool pool = mock(Pool.class);
267 Map<String, Pool> tenantCachePools = new HashMap<String, Pool>();
268 tenantCachePools.put("cloudowner_region", pool);
269 doReturn(tenantCachePools).when(tenantCache).getPools();
270 when(providerCache.getTenant(Mockito.anyString())).thenReturn(tenantCache);
271 doReturn(tenantCache).when(providerCache).getTenant(Mockito.anyString());
272 doThrow(new RuntimeException("TEST")).when(rc).delay();
273 underTest.setProviderCache(providerCacheMap);
274 assertNull(underTest.getContext(rc,
275 "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
276 + "abc12345-1234-5678-890a-abcdefb12345",
277 "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3"));
281 public void testValidateVMURLRequestFailedExceptionWellFormed() {
282 VMURL vm = mock(VMURL.class);
284 underTest.validateVMURL(vm);
285 fail("Exception not thrown");
286 } catch (RequestFailedException rfe) {
287 assert (rfe.getMessage().startsWith("The value vm-id is not well formed"));
292 public void testValidateVMURLRequestFailedExceptionTenantId() throws RequestFailedException {
293 VMURL vm = mock(VMURL.class);
294 when(vm.toString()).thenReturn("192.168.0.1");
295 when(vm.getTenantId()).thenReturn("%£$%^$");
297 underTest.validateVMURL(vm);
298 fail("Exception not thrown");
299 } catch (RequestFailedException rfe) {
300 assert (rfe.getMessage().startsWith("The value vm-id has an invalid tenantId"));
305 public void testValidateVMURLRequestFailedExceptionServerId() throws RequestFailedException {
306 VMURL vm = mock(VMURL.class);
307 when(vm.toString()).thenReturn("192.168.0.1");
308 when(vm.getTenantId()).thenReturn("0000000000000000000000000000000a");
309 when(vm.getServerId()).thenReturn("%£$%^$");
311 underTest.validateVMURL(vm);
312 fail("Exception not thrown");
313 } catch (RequestFailedException rfe) {
314 assert (rfe.getMessage().startsWith("The value vm-id has an invalid serverId"));
319 public void testResolveContext() throws RequestFailedException {
320 MockGenerator mg = new MockGenerator(Status.RUNNING);
321 SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
322 RequestContext rc = mock(RequestContext.class);
323 when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
324 assertNull(underTest.resolveContext(rc, new HashMap<String, String>(), "TEST", "TEST"));
327 @Test(expected = RequestFailedException.class)
328 public void testValidateParameters() throws RequestFailedException {
329 Properties properties = new Properties();
330 properties.putAll(ConfigurationFactory.getConfiguration().getProperties());
331 properties.put("TEST", "");
332 Map<String, String> propertyMap = new HashMap<String, String>();
333 for (String keys: properties.stringPropertyNames()) { propertyMap.put(keys, properties.getProperty(keys) );}
334 underTest.validateParametersExist(propertyMap, org.onap.appc.Constants.PROPERTY_APPLICATION_NAME, "TEST");