diff options
author | root <joss.armstrong@ericsson.com> | 2018-05-25 10:17:02 +0100 |
---|---|---|
committer | Takamune Cho <tc012c@att.com> | 2018-06-08 14:49:09 +0000 |
commit | 368228139718c7c166153a4ce56c8ef5f8ebb259 (patch) | |
tree | 3b117e140a05302ebdca7b97a2cdc80bfdc381b3 /appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test | |
parent | a2457a70add5a477f0f8d67fb8e25ea99556cae9 (diff) |
Unit Tests for APPC Adapters
onap/appc/adapter/iaas/provider/operation/impl/base classes
New test cases added to increase coverage to 96%
Change-Id: Ied377274d4407a3d0f0ccdfca9cbf2e11e46e1c8
Issue-ID: APPC-438
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
Diffstat (limited to 'appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test')
3 files changed, 1125 insertions, 0 deletions
diff --git a/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/base/TestProviderOperation.java b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/base/TestProviderOperation.java new file mode 100755 index 000000000..496bbce51 --- /dev/null +++ b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/base/TestProviderOperation.java @@ -0,0 +1,337 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.adapter.iaas.provider.operation.impl.base; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.verify; + +import org.junit.Test; +import org.mockito.Mockito; +import com.att.cdp.openstack.OpenStackContext; +import com.att.cdp.zones.model.Server.Status; +import org.onap.appc.adapter.iaas.impl.ProviderCache; +import org.onap.appc.adapter.iaas.impl.RequestContext; +import org.onap.appc.adapter.iaas.impl.RequestFailedException; +import org.onap.appc.adapter.iaas.impl.TenantCache; +import org.onap.appc.adapter.iaas.impl.VMURL; +import org.onap.appc.adapter.iaas.provider.operation.impl.AttachVolumeServer; +import org.onap.appc.adapter.iaas.provider.operation.impl.MockGenerator; +import org.onap.appc.configuration.ConfigurationFactory; +import org.onap.appc.exceptions.APPCException; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.appc.pool.Pool; +import org.onap.appc.pool.PoolDrainedException; +import org.onap.appc.pool.PoolExtensionException; +import org.glassfish.grizzly.http.util.HttpStatus; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + + +public class TestProviderOperation { + + ProviderServerOperation underTest = spy(new AttachVolumeServer()); + + @Test + public void testDoFailureRequestContextHttpStatusString() throws APPCException { + RequestContext rc = mock(RequestContext.class); + MockGenerator mg = new MockGenerator(Status.RUNNING); + SvcLogicContext svcLogicContext = mg.getSvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + HttpStatus code = HttpStatus.NOT_FOUND_404; + String message = "PALOS\n"; + underTest.doFailure(rc, code, message); + verify(underTest).doFailure(rc, code, message, null); + } + + @Test(expected = APPCException.class) + public void testDoFailureRequestContextHttpStatusStringException() throws APPCException { + RequestContext rc = mock(RequestContext.class); + MockGenerator mg = new MockGenerator(Status.RUNNING); + SvcLogicContext svcLogicContext = mg.getSvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + HttpStatus code = spy(HttpStatus.NOT_FOUND_404); + doThrow(new RuntimeException("TEST")).when(code).getStatusCode(); + String message = "PALOS\n"; + underTest.doFailure(rc, code, message, new Throwable("TEST")); + verify(underTest).doFailure(rc, code, message, null); + } + + @Test + public void testDoFailureRequestContextHttpStatusStringAPPCException() throws APPCException { + RequestContext rc = mock(RequestContext.class); + MockGenerator mg = new MockGenerator(Status.RUNNING); + SvcLogicContext svcLogicContext = mg.getSvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + HttpStatus code = HttpStatus.NOT_FOUND_404; + String message = "PALOS\n"; + doThrow(new APPCException("TEST")).when(underTest).doFailure(rc, code, message, null); + underTest.doFailure(rc, code, message); + verify(underTest).doFailure(rc, code, message, null); + } + + @Test(expected = APPCException.class) + public void testDoFailureRequestContextHttpStatusStringThrowableAPPCException() throws APPCException { + RequestContext rc = mock(RequestContext.class); + MockGenerator mg = new MockGenerator(Status.RUNNING); + SvcLogicContext svcLogicContext = mg.getSvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + HttpStatus code = HttpStatus.NOT_FOUND_404; + String message = "PALOS\n"; + underTest.doFailure(rc, code, message, new Throwable()); + } + + @Test + public void testValidateVM() throws RequestFailedException { + MockGenerator mg = new MockGenerator(Status.RUNNING); + SvcLogicContext svcLogicContext = mg.getSvcLogicContext(); + RequestContext rc = mock(RequestContext.class); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + assertTrue(underTest.validateVM(rc, "TEST", "TEST", null)); + } + + @Test + public void testGetContextNullVM() { + MockGenerator mg = new MockGenerator(Status.RUNNING); + RequestContext rc = mock(RequestContext.class); + SvcLogicContext svcLogicContext = mg.getSvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + assertNull(underTest.getContext(rc, "%£$%^$", "%£$%^$")); + verify(underTest).doFailure(Mockito.any(RequestContext.class), Mockito.any(HttpStatus.class), + Mockito.anyString()); + } + + @Test + public void testGetContextNullCache() { + MockGenerator mg = new MockGenerator(Status.RUNNING); + RequestContext rc = mock(RequestContext.class); + SvcLogicContext svcLogicContext = mg.getSvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + underTest.setProviderCache(mg.getProviderCacheMap()); + assertNull(underTest.getContext(rc, "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/" + + "abc12345-1234-5678-890a-abcdefb12345", "%£$%^$")); + verify(underTest).doFailure(Mockito.any(RequestContext.class), Mockito.any(HttpStatus.class), + Mockito.anyString()); + } + + @Test + public void testGetContextNullTenantCache() { + MockGenerator mg = new MockGenerator(Status.RUNNING); + RequestContext rc = mock(RequestContext.class); + SvcLogicContext svcLogicContext = mg.getSvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + Map<String, ProviderCache> providerCacheMap = mg.getProviderCacheMap(); + providerCacheMap.put("TEST", mock(ProviderCache.class)); + underTest.setProviderCache(mg.getProviderCacheMap()); + assertNull(underTest.getContext(rc, "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/" + + "abc12345-1234-5678-890a-abcdefb12345", "TEST")); + verify(underTest).doFailure(Mockito.any(RequestContext.class), Mockito.any(HttpStatus.class), + Mockito.anyString()); + } + + @Test + public void testGetContextPoolNullRegion() { + MockGenerator mg = new MockGenerator(Status.RUNNING); + RequestContext rc = mock(RequestContext.class); + SvcLogicContext svcLogicContext = mg.getSvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + Map<String, ProviderCache> providerCacheMap = mg.getProviderCacheMap(); + TenantCache tenantCache = mock(TenantCache.class); + when(tenantCache.getTenantName()).thenReturn("TEST"); + when(tenantCache.getTenantId()).thenReturn("TEST"); + when(tenantCache.determineRegion(Mockito.anyObject())).thenReturn(null); + ProviderCache providerCache = mock(ProviderCache.class); + when(providerCache.getTenant(Mockito.anyString())).thenReturn(tenantCache); + providerCacheMap.put("TEST", providerCache); + underTest.setProviderCache(mg.getProviderCacheMap()); + assertNull(underTest.getContext(rc, "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/" + + "abc12345-1234-5678-890a-abcdefb12345", "TEST")); + verify(underTest).doFailure(Mockito.any(RequestContext.class), Mockito.any(HttpStatus.class), + Mockito.anyString()); + } + + @Test + public void testGetContextAttemptFailed() { + MockGenerator mg = new MockGenerator(Status.RUNNING); + RequestContext rc = mock(RequestContext.class); + SvcLogicContext svcLogicContext = mg.getSvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + underTest.setProviderCache(mg.getProviderCacheMap()); + assertNull(underTest.getContext(rc, + "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/" + + "abc12345-1234-5678-890a-abcdefb12345", + "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3")); + } + + @Test + public void testGetContextRelogin() throws PoolExtensionException, PoolDrainedException { + RequestContext rc = mock(RequestContext.class); + SvcLogicContext svcLogicContext = mock(SvcLogicContext.class); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + ProviderCache providerCache = mock(ProviderCache.class); + Map<String, ProviderCache> providerCacheMap = new HashMap<String, ProviderCache>(); + providerCacheMap.put("http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3", providerCache); + when(rc.attempt()).thenReturn(true).thenReturn(false); + OpenStackContext context = mock(OpenStackContext.class); + when(context.isStale()).thenReturn(true); + TenantCache tenantCache = mock(TenantCache.class); + doReturn("cloudowner_region").when(tenantCache).determineRegion(any(VMURL.class)); + doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantId(); + doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantName(); + Pool pool = mock(Pool.class); + Map<String, Pool> tenantCachePools = new HashMap<String, Pool>(); + tenantCachePools.put("cloudowner_region", pool); + doReturn(tenantCachePools).when(tenantCache).getPools(); + when(providerCache.getTenant(Mockito.anyString())).thenReturn(tenantCache); + doReturn(tenantCache).when(providerCache).getTenant(Mockito.anyString()); + doReturn(context).when(pool).reserve(); + underTest.setProviderCache(providerCacheMap); + assertTrue(underTest.getContext(rc, + "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/" + + "abc12345-1234-5678-890a-abcdefb12345", + "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3") instanceof OpenStackContext); + } + + @Test + public void testGetContextPoolException() throws PoolExtensionException, PoolDrainedException { + RequestContext rc = mock(RequestContext.class); + SvcLogicContext svcLogicContext = mock(SvcLogicContext.class); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + ProviderCache providerCache = mock(ProviderCache.class); + Map<String, ProviderCache> providerCacheMap = new HashMap<String, ProviderCache>(); + providerCacheMap.put("http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3", providerCache); + + when(rc.attempt()).thenReturn(true).thenReturn(false); + OpenStackContext context = mock(OpenStackContext.class); + when(context.isStale()).thenReturn(true); + TenantCache tenantCache = mock(TenantCache.class); + doReturn("cloudowner_region").when(tenantCache).determineRegion(any(VMURL.class)); + doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantId(); + doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantName(); + Pool pool = mock(Pool.class); + Map<String, Pool> tenantCachePools = new HashMap<String, Pool>(); + tenantCachePools.put("cloudowner_region", pool); + doReturn(tenantCachePools).when(tenantCache).getPools(); + when(providerCache.getTenant(Mockito.anyString())).thenReturn(tenantCache); + doReturn(tenantCache).when(providerCache).getTenant(Mockito.anyString()); + doThrow(new PoolExtensionException("TEST")).when(pool).reserve(); + underTest.setProviderCache(providerCacheMap); + underTest.getContext(rc, + "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/" + + "abc12345-1234-5678-890a-abcdefb12345", + "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3"); + verify(rc).delay(); + } + + @Test + public void testGetContextException() { + RequestContext rc = mock(RequestContext.class); + SvcLogicContext svcLogicContext = mock(SvcLogicContext.class); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + ProviderCache providerCache = mock(ProviderCache.class); + Map<String, ProviderCache> providerCacheMap = new HashMap<String, ProviderCache>(); + providerCacheMap.put("http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3", providerCache); + + when(rc.attempt()).thenReturn(true).thenReturn(false); + OpenStackContext context = mock(OpenStackContext.class); + when(context.isStale()).thenReturn(true); + TenantCache tenantCache = mock(TenantCache.class); + doReturn("cloudowner_region").when(tenantCache).determineRegion(any(VMURL.class)); + doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantId(); + doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantName(); + Pool pool = mock(Pool.class); + Map<String, Pool> tenantCachePools = new HashMap<String, Pool>(); + tenantCachePools.put("cloudowner_region", pool); + doReturn(tenantCachePools).when(tenantCache).getPools(); + when(providerCache.getTenant(Mockito.anyString())).thenReturn(tenantCache); + doReturn(tenantCache).when(providerCache).getTenant(Mockito.anyString()); + doThrow(new RuntimeException("TEST")).when(rc).delay(); + underTest.setProviderCache(providerCacheMap); + assertNull(underTest.getContext(rc, + "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/" + + "abc12345-1234-5678-890a-abcdefb12345", + "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3")); + } + + @Test + public void testValidateVMURLRequestFailedExceptionWellFormed() { + VMURL vm = mock(VMURL.class); + try { + underTest.validateVMURL(vm); + fail("Exception not thrown"); + } catch (RequestFailedException rfe) { + assert (rfe.getMessage().startsWith("The value vm-id is not well formed")); + } + } + + @Test + public void testValidateVMURLRequestFailedExceptionTenantId() throws RequestFailedException { + VMURL vm = mock(VMURL.class); + when(vm.toString()).thenReturn("192.168.0.1"); + when(vm.getTenantId()).thenReturn("%£$%^$"); + try { + underTest.validateVMURL(vm); + fail("Exception not thrown"); + } catch (RequestFailedException rfe) { + assert (rfe.getMessage().startsWith("The value vm-id has an invalid tenantId")); + } + } + + @Test + public void testValidateVMURLRequestFailedExceptionServerId() throws RequestFailedException { + VMURL vm = mock(VMURL.class); + when(vm.toString()).thenReturn("192.168.0.1"); + when(vm.getTenantId()).thenReturn("0000000000000000000000000000000a"); + when(vm.getServerId()).thenReturn("%£$%^$"); + try { + underTest.validateVMURL(vm); + fail("Exception not thrown"); + } catch (RequestFailedException rfe) { + assert (rfe.getMessage().startsWith("The value vm-id has an invalid serverId")); + } + } + + @Test + public void testResolveContext() throws RequestFailedException { + MockGenerator mg = new MockGenerator(Status.RUNNING); + SvcLogicContext svcLogicContext = mg.getSvcLogicContext(); + RequestContext rc = mock(RequestContext.class); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + assertNull(underTest.resolveContext(rc, new HashMap<String, String>(), "TEST", "TEST")); + } + + @Test(expected = RequestFailedException.class) + public void testValidateParameters() throws RequestFailedException { + Properties properties = new Properties(); + properties.putAll(ConfigurationFactory.getConfiguration().getProperties()); + properties.put("TEST", ""); + Map<String, String> propertyMap = new HashMap<String, String>(); + for (String keys: properties.stringPropertyNames()) { propertyMap.put(keys, properties.getProperty(keys) );} + underTest.validateParametersExist(propertyMap, org.onap.appc.Constants.PROPERTY_APPLICATION_NAME, "TEST"); + } + +} diff --git a/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/base/TestProviderServerOperation.java b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/base/TestProviderServerOperation.java new file mode 100755 index 000000000..870763c18 --- /dev/null +++ b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/base/TestProviderServerOperation.java @@ -0,0 +1,591 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.adapter.iaas.provider.operation.impl.base; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.appc.adapter.iaas.impl.RequestContext; +import org.onap.appc.adapter.iaas.impl.RequestFailedException; +import org.onap.appc.adapter.iaas.provider.operation.impl.MockGenerator; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.appc.adapter.iaas.provider.operation.impl.AttachVolumeServer; +import com.att.cdp.exceptions.ContextClosedException; +import com.att.cdp.exceptions.ContextConnectionException; +import com.att.cdp.exceptions.InvalidRequestException; +import com.att.cdp.exceptions.NotLoggedInException; +import com.att.cdp.exceptions.NotNavigableException; +import com.att.cdp.exceptions.TimeoutException; +import com.att.cdp.exceptions.ZoneException; +import com.att.cdp.openstack.OpenStackContext; +import com.att.cdp.zones.ComputeService; +import com.att.cdp.zones.Context; +import com.att.cdp.zones.ImageService; +import com.att.cdp.zones.NetworkService; +import com.att.cdp.zones.Provider; +import com.att.cdp.zones.model.Hypervisor; +import com.att.cdp.zones.model.Image; +import com.att.cdp.zones.model.Network; +import com.att.cdp.zones.model.Server; +import com.att.cdp.zones.model.Server.Status; +import com.att.cdp.zones.model.Tenant; +import com.att.cdp.zones.model.Port; + +public class TestProviderServerOperation { + + ProviderServerOperation underTest = spy(AttachVolumeServer.class); + + @Test + public void testHasImageAccess() throws NotLoggedInException { + RequestContext rc = mock(RequestContext.class); + ImageService imageService = mock(ImageService.class); + Context context = mock(OpenStackContext.class); + when(context.getImageService()).thenReturn(imageService); + assertTrue(underTest.hasImageAccess(rc, context)); + } + + @Test + public void testHasImageAccessZoneException() throws ZoneException { + RequestContext rc = mock(RequestContext.class); + ImageService imageService = mock(ImageService.class); + Context context = mock(OpenStackContext.class); + when(context.getImageService()).thenReturn(imageService); + when(imageService.getImageByName("CHECK_IMAGE_ACCESS")).thenThrow(new ZoneException("TEST_ZONE_EXCEPTION")); + assertFalse(underTest.hasImageAccess(rc, context)); + } + + @Test + public void testWaitForStateChangeRequestContextImageStatusArray() throws ZoneException { + Image image = mock(Image.class); + Image.Status imageStatus = Image.Status.ACTIVE; + image.setStatus(imageStatus); + RequestContext rc = mock(RequestContext.class); + ImageService imageService = mock(ImageService.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + when(context.getProvider()).thenReturn(provider); + when(provider.getName()).thenReturn("TEST Provider Name"); + when(image.getContext()).thenReturn(context); + when(rc.isFailed()).thenReturn(true); + when(context.getImageService()).thenReturn(imageService); + boolean requestFailedExceptionThrown = false; + try { + underTest.waitForStateChange(rc, image, imageStatus); + fail("Exception not thrown"); + } catch (RequestFailedException requestFailedException) { + requestFailedExceptionThrown = (requestFailedException.getOperation().equals("Waiting for State Change")); + } + assertTrue(requestFailedExceptionThrown); + } + + @Test + public void testWaitForStateChangeRequestContextImageStatusArrayTimeoutException() throws ZoneException, + NotNavigableException, InvalidRequestException, ContextClosedException, RequestFailedException { + Image image = mock(Image.class); + Image.Status imageStatus = Image.Status.ACTIVE; + image.setStatus(imageStatus); + RequestContext rc = mock(RequestContext.class); + ImageService imageService = mock(ImageService.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + when(context.getProvider()).thenReturn(provider); + when(provider.getName()).thenReturn("TEST Provider Name"); + when(image.getContext()).thenReturn(context); + when(rc.isFailed()).thenReturn(false); + Tenant tenant = spy(new Tenant()); + when(tenant.getName()).thenReturn("TEST_TENANT_NAME"); + when(tenant.getId()).thenReturn("TEST_TENANT_ID"); + when(rc.attempt()).thenReturn(true).thenReturn(false); + when(context.getTenant()).thenReturn(tenant); + when(context.getImageService()).thenReturn(imageService); + doThrow(new TimeoutException("TEST")).when(image).waitForStateChange(Mockito.anyInt(), Mockito.anyInt(), + Mockito.anyObject()); + underTest.waitForStateChange(rc, image, imageStatus); + verify(rc, times(1)).delay(); + } + + @Test + public void testWaitForStateChangeRequestContextImageStatusArrayZoneException() + throws ZoneException, RequestFailedException { + Image image = mock(Image.class); + Image.Status imageStatus = Image.Status.ACTIVE; + image.setStatus(imageStatus); + RequestContext rc = mock(RequestContext.class); + ImageService imageService = mock(ImageService.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + when(context.getProvider()).thenReturn(provider); + when(provider.getName()).thenReturn("TEST Provider Name"); + when(image.getContext()).thenReturn(context); + when(image.getName()).thenReturn("TEST_IMAGE_NAME"); + when(image.getId()).thenReturn("TEST_IMAGE_ID"); + when(image.getStatus()).thenReturn(imageStatus); + when(rc.isFailed()).thenReturn(false); + Tenant tenant = spy(new Tenant()); + when(rc.attempt()).thenReturn(true).thenReturn(false); + when(context.getTenant()).thenReturn(tenant).thenThrow(new ZoneException("TEST_ZONE_EXCEPTION")); + when(context.getImageService()).thenReturn(imageService); + doThrow(new TimeoutException("TEST")).when(image).waitForStateChange(Mockito.anyInt(), Mockito.anyInt(), + Mockito.anyObject()); + underTest.waitForStateChange(rc, image, imageStatus); + verify(rc, times(1)).delay(); + } + + @Test + public void testWaitForStateChangeRequestContextServerStatusArray() throws NotLoggedInException { + Server server = mock(Server.class); + Status serverStatus = Status.RUNNING; + server.setStatus(serverStatus); + RequestContext rc = mock(RequestContext.class); + ImageService imageService = mock(ImageService.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + when(context.getProvider()).thenReturn(provider); + when(provider.getName()).thenReturn("TEST Provider Name"); + when(server.getContext()).thenReturn(context); + when(rc.isFailed()).thenReturn(true); + boolean requestFailedExceptionThrown = false; + try { + when(context.getImageService()).thenReturn(imageService); + underTest.waitForStateChange(rc, server, serverStatus); + } catch (RequestFailedException requestFailedException) { + requestFailedExceptionThrown = (requestFailedException.getOperation().equals("Waiting for State Change")); + } + assertTrue(requestFailedExceptionThrown); + } + + @Test + public void testWaitForStateChangeRequestContextServerStatusArrayTimeoutException() + throws ZoneException, RequestFailedException { + Server server = mock(Server.class); + Status serverStatus = Status.RUNNING; + server.setStatus(serverStatus); + when(server.getStatus()).thenReturn(serverStatus); + RequestContext rc = mock(RequestContext.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + when(context.getProvider()).thenReturn(provider); + when(provider.getName()).thenReturn("TEST Provider Name"); + when(server.getContext()).thenReturn(context); + when(rc.isFailed()).thenReturn(false); + Tenant tenant = spy(new Tenant()); + when(tenant.getName()).thenReturn("TEST_TENANT_NAME"); + when(tenant.getId()).thenReturn("TEST_TENANT_ID"); + when(rc.attempt()).thenReturn(true).thenReturn(false); + when(context.getTenant()).thenReturn(tenant); + doThrow(new TimeoutException("TEST")).when(server).waitForStateChange(Mockito.anyInt(), Mockito.anyInt(), + Mockito.anyObject()); + underTest.waitForStateChange(rc, server, serverStatus); + verify(rc, times(1)).delay(); + } + + @Test + public void testWaitForStateChangeRequestContextServerStatusArrayZoneException() + throws ZoneException, RequestFailedException { + Server server = mock(Server.class); + Status serverStatus = Status.RUNNING; + MockGenerator mg = new MockGenerator(serverStatus); + server.setStatus(serverStatus); + when(server.getStatus()).thenReturn(serverStatus); + RequestContext rc = mock(RequestContext.class); + ImageService imageService = mock(ImageService.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + when(context.getProvider()).thenReturn(provider); + when(provider.getName()).thenReturn("TEST Provider Name"); + when(server.getContext()).thenReturn(context); + when(rc.isFailed()).thenReturn(false); + Tenant tenant = spy(new Tenant()); + when(rc.attempt()).thenReturn(true).thenReturn(false); + when(context.getTenant()).thenReturn(tenant).thenThrow(new ZoneException("TEST_ZONE_EXCEPTION")); + when(context.getImageService()).thenReturn(imageService); + doThrow(new TimeoutException("TEST")).when(server).waitForStateChange(Mockito.anyInt(), Mockito.anyInt(), + Mockito.anyObject()); + underTest.waitForStateChange(rc, server, serverStatus); + verify(rc, times(1)).delay(); + } + + @Test + public void testLookupServer() throws ZoneException { + MockGenerator mg = new MockGenerator(Status.RUNNING); + Server server = mock(Server.class); + RequestContext rc = mock(RequestContext.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + when(context.getProvider()).thenReturn(provider); + when(server.getContext()).thenReturn(context); + when(provider.getName()).thenReturn("TEST Provider Name"); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + when(rc.isFailed()).thenReturn(true); + SvcLogicContext slc = new SvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(slc); + rc.setSvcLogicContext(slc); + String id = mg.SERVER_ID; + boolean requestFailedExceptionThrown = false; + try { + underTest.lookupServer(rc, context, id); + fail("Exception not thrown"); + } catch (RequestFailedException requestFailedException) { + System.out.println(requestFailedException.getOperation()); + requestFailedExceptionThrown = (requestFailedException.getOperation().equals("Lookup Server")); + } + assertTrue(requestFailedExceptionThrown); + } + + @Test + public void testLookupServerContextConnectionException() throws ZoneException, RequestFailedException { + MockGenerator mg = new MockGenerator(Status.RUNNING); + Server server = spy(new Server()); + RequestContext rc = mock(RequestContext.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + when(context.getProvider()).thenReturn(provider); + when(server.getContext()).thenReturn(context); + when(provider.getName()).thenReturn("TEST Provider Name"); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + Tenant tenant = spy(new Tenant()); + when(tenant.getName()).thenReturn("TEST_TENANT_NAME"); + when(tenant.getId()).thenReturn("TEST_TENANT_ID"); + when(rc.attempt()).thenReturn(true).thenReturn(false); + when(context.getTenant()).thenReturn(tenant); + doThrow(new ContextConnectionException("TEST")).when(computeService).getServer(Mockito.anyString()); + assertNull(underTest.lookupServer(rc, context, mg.SERVER_ID)); + verify(rc, times(1)).delay(); + } + + @Test + public void testResumeServer() throws ZoneException { + Server server = spy(new Server()); + RequestContext rc = mock(RequestContext.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + when(context.getProvider()).thenReturn(provider); + when(server.getContext()).thenReturn(context); + when(provider.getName()).thenReturn("TEST Provider Name"); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + when(rc.isFailed()).thenReturn(true); + boolean requestFailedExceptionThrown = false; + try { + underTest.resumeServer(rc, server); + fail("Exception not thrown"); + } catch (RequestFailedException requestFailedException) { + requestFailedExceptionThrown = (requestFailedException.getOperation().equals("Resume Server")); + } + assertTrue(requestFailedExceptionThrown); + } + + @Test + public void testResumeServerContextConnectionException() throws ZoneException, RequestFailedException { + Server server = spy(new Server()); + RequestContext rc = mock(RequestContext.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + when(context.getProvider()).thenReturn(provider); + when(server.getContext()).thenReturn(context); + when(provider.getName()).thenReturn("TEST Provider Name"); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + Tenant tenant = spy(new Tenant()); + when(tenant.getName()).thenReturn("TEST_TENANT_NAME"); + when(tenant.getId()).thenReturn("TEST_TENANT_ID"); + when(rc.attempt()).thenReturn(true).thenReturn(false); + when(context.getTenant()).thenReturn(tenant); + doThrow(new ContextConnectionException("TEST")).when(server).resume(); + underTest.resumeServer(rc, server); + verify(rc, times(1)).delay(); + } + + @Test + public void testStopServer() throws ZoneException { + Server server = spy(new Server()); + RequestContext rc = mock(RequestContext.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + when(context.getProvider()).thenReturn(provider); + when(server.getContext()).thenReturn(context); + when(provider.getName()).thenReturn("TEST Provider Name"); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + when(rc.isFailed()).thenReturn(true); + boolean requestFailedExceptionThrown = false; + try { + underTest.stopServer(rc, server); + fail("Exception not thrown"); + } catch (RequestFailedException requestFailedException) { + requestFailedExceptionThrown = (requestFailedException.getOperation().equals("Stop Server")) ? true : false; + } + assertTrue(requestFailedExceptionThrown); + } + + @Test + public void testStopServerContextConnectionException() throws ZoneException, RequestFailedException { + Server server = spy(new Server()); + RequestContext rc = mock(RequestContext.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + when(context.getProvider()).thenReturn(provider); + when(server.getContext()).thenReturn(context); + when(provider.getName()).thenReturn("TEST Provider Name"); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + Tenant tenant = spy(new Tenant()); + when(tenant.getName()).thenReturn("TEST_TENANT_NAME"); + when(tenant.getId()).thenReturn("TEST_TENANT_ID"); + when(rc.attempt()).thenReturn(true).thenReturn(false); + when(context.getTenant()).thenReturn(tenant); + doThrow(new ContextConnectionException("TEST")).when(server).stop(); + underTest.stopServer(rc, server); + verify(rc, times(1)).delay(); + } + + @Test + public void testStartServer() throws ZoneException { + Server server = spy(new Server()); + RequestContext rc = mock(RequestContext.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + when(context.getProvider()).thenReturn(provider); + when(server.getContext()).thenReturn(context); + when(provider.getName()).thenReturn("TEST Provider Name"); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + when(rc.isFailed()).thenReturn(true); + boolean requestFailedExceptionThrown = false; + try { + underTest.startServer(rc, server); + fail("Exception not thrown"); + } catch (RequestFailedException requestFailedException) { + System.out.println(requestFailedException.getOperation()); + requestFailedExceptionThrown = (requestFailedException.getOperation().equals("Start Server")); + } + assertTrue(requestFailedExceptionThrown); + } + + @Test + public void testStartServerContextConnectionException() throws ZoneException, RequestFailedException { + Server server = spy(new Server()); + RequestContext rc = mock(RequestContext.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + when(context.getProvider()).thenReturn(provider); + when(server.getContext()).thenReturn(context); + when(provider.getName()).thenReturn("TEST Provider Name"); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + Tenant tenant = spy(new Tenant()); + when(tenant.getName()).thenReturn("TEST_TENANT_NAME"); + when(tenant.getId()).thenReturn("TEST_TENANT_ID"); + when(rc.attempt()).thenReturn(true).thenReturn(false); + when(context.getTenant()).thenReturn(tenant); + doThrow(new ContextConnectionException("TEST")).when(server).start(); + underTest.startServer(rc, server); + verify(rc, times(1)).delay(); + } + + @Test + public void testUnpauseServer() throws ZoneException { + Server server = spy(new Server()); + RequestContext rc = mock(RequestContext.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + when(context.getProvider()).thenReturn(provider); + when(server.getContext()).thenReturn(context); + when(provider.getName()).thenReturn("TEST Provider Name"); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + when(rc.isFailed()).thenReturn(true); + boolean requestFailedExceptionThrown = false; + try { + underTest.unpauseServer(rc, server); + } catch (RequestFailedException requestFailedException) { + System.out.println(requestFailedException.getOperation()); + requestFailedExceptionThrown = (requestFailedException.getOperation().equals("Unpause Server")); + } + assertTrue(requestFailedExceptionThrown); + } + + @Test + public void testUnpauseServerContextConnectionException() throws ZoneException, RequestFailedException { + Server server = spy(new Server()); + RequestContext rc = mock(RequestContext.class); + Context context = mock(OpenStackContext.class); + Provider provider = mock(Provider.class); + when(context.getProvider()).thenReturn(provider); + when(server.getContext()).thenReturn(context); + when(provider.getName()).thenReturn("TEST Provider Name"); + ComputeService computeService = mock(ComputeService.class); + when(context.getComputeService()).thenReturn(computeService); + when(computeService.getURL()).thenReturn("TEST URL"); + Tenant tenant = spy(new Tenant()); + when(tenant.getName()).thenReturn("TEST_TENANT_NAME"); + when(tenant.getId()).thenReturn("TEST_TENANT_ID"); + when(rc.attempt()).thenReturn(true).thenReturn(false); + when(context.getTenant()).thenReturn(tenant); + doThrow(new ContextConnectionException("TEST")).when(server).unpause(); + underTest.unpauseServer(rc, server); + verify(rc, times(1)).delay(); + } + + @Test + public void testCheckVirtualMachineNetworkStatusOnlinePort() throws ZoneException { + MockGenerator mg = new MockGenerator(Status.RUNNING); + RequestContext rc = mock(RequestContext.class); + SvcLogicContext slc = new SvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(slc); + rc.setSvcLogicContext(slc); + OpenStackContext context = mock(OpenStackContext.class); + NetworkService netSvc = mock(NetworkService.class); + when(context.getNetworkService()).thenReturn(netSvc); + String id = mg.SERVER_ID; + Network network = mock(Network.class); + Server server = mock(Server.class); + Port onlinePort = new Port(); + onlinePort.setName("Online Port"); + onlinePort.setPortState(Port.Status.ONLINE); + onlinePort.setNetwork("ONLINE Port Network"); + List<Port> portList = Arrays.asList(onlinePort); + System.out.println(Arrays.toString(portList.toArray())); + boolean requestFailedExceptionThrown = false; + try { + when(netSvc.getNetworkById(Mockito.anyString())).thenReturn(network); + when(server.getPorts()).thenReturn(portList); + when(network.getStatus()).thenReturn(Network.Status.OFFLINE.toString()); + underTest.checkVirtualMachineNetworkStatus(rc, server, context); + fail("Exception not thrown"); + } catch (RequestFailedException requestFailedException) { + System.out.println(requestFailedException.getOperation()); + requestFailedExceptionThrown = (requestFailedException.getOperation().equals("VM Server Network is DOWN")); + } + assertTrue(requestFailedExceptionThrown); + + } + + @Test + public void testCheckVirtualMachineNetworkStatusOfflinePort() throws ZoneException { + Port offlinePort = new Port(); + offlinePort.setName("Offline Port"); + offlinePort.setId("Offline Port"); + offlinePort.setPortState(Port.Status.OFFLINE); + testCheckVirtualMachineNetworkStatusBaseTest("OFFLINE", offlinePort); + } + + @Test + public void testCheckVirtualMachineNetworkStatusPendingPort() throws ZoneException { + Port pendingPort = new Port(); + pendingPort.setName("Pending Port"); + pendingPort.setId("Pending Port"); + pendingPort.setPortState(Port.Status.PENDING); + testCheckVirtualMachineNetworkStatusBaseTest("PENDING", pendingPort); + } + + @Test + public void testCheckVirtualMachineNetworkStatusUnkownPort() throws ZoneException { + Port unknownPort = new Port(); + unknownPort.setName("Unknown Port"); + unknownPort.setId("Unknown Port"); + unknownPort.setPortState(Port.Status.UNKNOWN); + testCheckVirtualMachineNetworkStatusBaseTest("UNKNOWN", unknownPort); + } + + private void testCheckVirtualMachineNetworkStatusBaseTest(String name, Port port) throws ZoneException { + MockGenerator mg = new MockGenerator(Status.RUNNING); + RequestContext rc = mock(RequestContext.class); + SvcLogicContext slc = new SvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(slc); + rc.setSvcLogicContext(slc); + OpenStackContext context = mg.getContext(); + String id = mg.SERVER_ID; + Server server = mock(Server.class); + List<Port> portList = Arrays.asList(port); + boolean requestFailedExceptionThrown = false; + try { + when(server.getPorts()).thenReturn(portList); + underTest.checkVirtualMachineNetworkStatus(rc, server, context); + fail("Exception not thrown"); + } catch (RequestFailedException requestFailedException) { + requestFailedExceptionThrown = (requestFailedException.getOperation() + .equals("VM Server Port status is " + name)) ? true : false; + } + assertTrue(requestFailedExceptionThrown); + } + + @Test + public void testCheckHypervisorDown() throws ZoneException { + testCheckHypervisorBaseTest(Hypervisor.State.DOWN, "Hypervisor status DOWN or NOT ENABLED"); + } + + @Test + public void testCheckHypervisorUnknown() throws ZoneException { + testCheckHypervisorBaseTest(null, "Unable to determine Hypervisor status"); + } + + public void testCheckHypervisorBaseTest(Hypervisor.State state, String expectedExceptionOperation) throws ZoneException { + Server server = mock(Server.class); + Hypervisor hypervisor = new Hypervisor(); + hypervisor.setStatus(Hypervisor.Status.DISABLED); + hypervisor.setState(state); + when(server.getHypervisor()).thenReturn(hypervisor); + boolean requestFailedExceptionThrown = false; + try { + underTest.checkHypervisor(server); + fail("Exception not thrown"); + } catch (RequestFailedException requestFailedException) { + requestFailedExceptionThrown = (requestFailedException.getOperation().equals(expectedExceptionOperation)); + } + assertTrue(requestFailedExceptionThrown); + } +} diff --git a/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/base/TestProviderStackOperation.java b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/base/TestProviderStackOperation.java new file mode 100755 index 000000000..6739078b9 --- /dev/null +++ b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/base/TestProviderStackOperation.java @@ -0,0 +1,197 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.adapter.iaas.provider.operation.impl.base; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.util.LinkedList; +import java.util.List; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.appc.adapter.iaas.ProviderAdapter; +import org.onap.appc.adapter.iaas.impl.RequestContext; +import org.onap.appc.adapter.iaas.impl.RequestFailedException; +import org.onap.appc.adapter.iaas.provider.operation.impl.MockGenerator; +import org.onap.appc.adapter.iaas.provider.operation.impl.RestoreStack; +import org.onap.appc.adapter.openstack.heat.StackResource; +import org.onap.appc.adapter.openstack.heat.StackResource.ShowStack; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import com.att.cdp.exceptions.ContextConnectionException; +import com.att.cdp.exceptions.ZoneException; +import com.att.cdp.openstack.v1.OpenStackStackService; +import com.att.cdp.zones.Context; +import com.att.cdp.zones.StackService; +import com.att.cdp.zones.model.Server; +import com.att.cdp.zones.model.Stack; +import com.att.cdp.zones.model.Tenant; +import com.att.cdp.zones.model.Server.Status; +import com.att.cdp.zones.spi.AbstractService; +import com.woorea.openstack.base.client.OpenStackBaseException; + + +public class TestProviderStackOperation { + + ProviderStackOperation underTest = spy(RestoreStack.class); + + @Test + public void testTrackRequest() { + MockGenerator mg = new MockGenerator(Server.Status.RUNNING); + Context context = mg.getContext(); + AbstractService.State state = new OpenStackStackService(context).new State("STATE_NAME", "STATE_VALUE"); + underTest.trackRequest(context, state); + verify(context).getPrincipal(); + } + + @Test + public void testWaitForStack() throws Exception{ + MockGenerator mg = new MockGenerator(Status.SUSPENDED); + StackService stackService = mock(StackService.class); + StackResource stackResource = mock(StackResource.class); + ShowStack showStack = mock(ShowStack.class); + Stack stack1 = mock(Stack.class); + com.woorea.openstack.heat.model.Stack openstackHeatModelStack = mock( + com.woorea.openstack.heat.model.Stack.class); + doReturn("stack1").when(stack1).getId(); + doReturn("stack1").when(stack1).getName(); + com.att.cdp.zones.model.Stack.Status stackStatus = com.att.cdp.zones.model.Stack.Status.DELETED; + doReturn(stackStatus).when(stack1).getStatus(); + doReturn(mg.getContext()).when(stack1).getContext(); + List<Stack> stackList = new LinkedList<Stack>(); + stackList.add(stack1); + doReturn(stackList).when(stackService).getStacks(); + doReturn(stack1).when(stackService).getStack("stack1", "stack1"); + doReturn(showStack).when(stackResource).show(Mockito.anyString(), Mockito.anyString()); + doReturn(openstackHeatModelStack).when(showStack).execute(); + doReturn(stackService).when(mg.getContext()).getStackService(); + doReturn("ONLINE").when(openstackHeatModelStack).getStackStatus(); + mg.getParams().put(ProviderAdapter.PROPERTY_STACK_ID, "stack1"); + underTest.setProviderCache(mg.getProviderCacheMap()); + assertTrue(underTest.waitForStack(stack1, stackResource, "ONLINE")); + } + + @Test + public void testWaitForStackFailed() throws ZoneException, OpenStackBaseException { + MockGenerator mg = new MockGenerator(Status.SUSPENDED); + StackService stackService = mock(StackService.class); + StackResource stackResource = mock(StackResource.class); + ShowStack showStack = mock(ShowStack.class); + Stack stack1 = mock(Stack.class); + com.woorea.openstack.heat.model.Stack openstackHeatModelStack = mock( + com.woorea.openstack.heat.model.Stack.class); + doReturn("stack1").when(stack1).getId(); + doReturn("stack1").when(stack1).getName(); + com.att.cdp.zones.model.Stack.Status stackStatus = com.att.cdp.zones.model.Stack.Status.DELETED; + doReturn(stackStatus).when(stack1).getStatus(); + doReturn(mg.getContext()).when(stack1).getContext(); + List<Stack> stackList = new LinkedList<Stack>(); + stackList.add(stack1); + doReturn(stackList).when(stackService).getStacks(); + doReturn(stack1).when(stackService).getStack("stack1", "stack1"); + doReturn(showStack).when(stackResource).show(Mockito.anyString(), Mockito.anyString()); + doReturn(openstackHeatModelStack).when(showStack).execute(); + doReturn(stackService).when(mg.getContext()).getStackService(); + doReturn("FAILED").when(openstackHeatModelStack).getStackStatus(); + mg.getParams().put(ProviderAdapter.PROPERTY_STACK_ID, "stack1"); + underTest.setProviderCache(mg.getProviderCacheMap()); + assertFalse(underTest.waitForStack(stack1, stackResource, "ONLINE")); + } + + @Test + public void testLookupStack() throws ZoneException, RequestFailedException { + MockGenerator mg = new MockGenerator(Status.RUNNING); + Server server = spy(new Server()); + RequestContext rc = mock(RequestContext.class); + Context context = mg.getContext(); + when(server.getContext()).thenReturn(context); + Tenant tenant = spy(new Tenant()); + when(tenant.getName()).thenReturn("TEST_TENANT_NAME"); + when(tenant.getId()).thenReturn("TEST_TENANT_ID"); + when(rc.attempt()).thenReturn(true).thenReturn(false); + Stack stack1 = mock(Stack.class); + String id = mg.SERVER_ID; + when(stack1.getId()).thenReturn(id); + StackService stackService = mock(StackService.class); + List<Stack> stackList = new LinkedList<Stack>(); + stackList.add(stack1); + when(tenant.getName()).thenReturn("TEST_TENANT_NAME"); + when(tenant.getId()).thenReturn("TEST_TENANT_ID"); + when(context.getTenant()).thenReturn(tenant); + doReturn(stackList).when(stackService).getStacks(); + doReturn(stackService).when(context).getStackService(); + underTest.lookupStack(rc, context, id); + verify(rc, times(1)).isFailed(); + } + + @Test(expected = RequestFailedException.class) + public void testLookupStackRcFailed() throws RequestFailedException, ZoneException { + MockGenerator mg = new MockGenerator(Status.RUNNING); + Server server = spy(new Server()); + RequestContext rc = mock(RequestContext.class); + Context context = mg.getContext(); + SvcLogicContext svcLogicContext = mg.getSvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + when(server.getContext()).thenReturn(context); + Tenant tenant = spy(new Tenant()); + when(tenant.getName()).thenReturn("TEST_TENANT_NAME"); + when(tenant.getId()).thenReturn("TEST_TENANT_ID"); + when(rc.attempt()).thenReturn(true).thenReturn(false); + Stack stack1 = mock(Stack.class); + String id = mg.SERVER_ID; + when(stack1.getId()).thenReturn(id); + StackService stackService = mock(StackService.class); + List<Stack> stackList = new LinkedList<Stack>(); + stackList.add(stack1); + when(tenant.getName()).thenReturn("TEST_TENANT_NAME"); + when(tenant.getId()).thenReturn("TEST_TENANT_ID"); + when(rc.isFailed()).thenReturn(true); + when(context.getTenant()).thenReturn(tenant); + doReturn(stackService).when(context).getStackService(); + doThrow(new ContextConnectionException("TEST")).when(stackService).getStacks(); + underTest.lookupStack(rc, context, id); + } + + @Test + public void testWaitForStackStatus() throws ZoneException, RequestFailedException { + MockGenerator mg = new MockGenerator(Server.Status.ERROR); + RequestContext rc = mock(RequestContext.class); + Context context = mg.getContext(); + Stack stack1 = mock(Stack.class); + String id = mg.SERVER_ID; + when(stack1.getId()).thenReturn(id); + StackService stackService = mock(StackService.class); + List<Stack> stackList = new LinkedList<Stack>(); + stackList.add(stack1); + when(stack1.getContext()).thenReturn(context); + com.att.cdp.zones.model.Stack.Status stackStatus = com.att.cdp.zones.model.Stack.Status.FAILED; + doReturn(stackStatus).when(stack1).getStatus(); + SvcLogicContext svcLogicContext = mg.getSvcLogicContext(); + when(rc.getSvcLogicContext()).thenReturn(svcLogicContext); + doReturn(stackService).when(context).getStackService(); + doReturn(stack1).when(stackService).getStack(Mockito.anyString(), Mockito.anyString()); + assertFalse(underTest.waitForStackStatus(rc, stack1, Stack.Status.DELETED)); + } + +} |