From 8548b9a6401c1397f3a9cdf6bca98ff56971247d Mon Sep 17 00:00:00 2001 From: Michal Kabaj Date: Mon, 17 Sep 2018 15:10:43 +0200 Subject: DeploymentInfo tests and construction improvement - Improved construction by adding DeploymentInfoBuilder - DeploymentInfo immutable - added detailed unit tests Change-Id: I3dabb91d5190ef932e77f0c76e18fde8ca73c64d Issue-ID: SO-1055 Signed-off-by: Michal Kabaj --- .../cloudify/beans/DeploymentInfoBuilderTest.java | 168 +++++++++++++++++++++ .../onap/so/cloudify/beans/DeploymentInfoTest.java | 76 ---------- .../so/cloudify/utils/MsoCloudifyUtilsTest2.java | 60 ++++---- 3 files changed, 197 insertions(+), 107 deletions(-) create mode 100644 adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/beans/DeploymentInfoBuilderTest.java delete mode 100644 adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/beans/DeploymentInfoTest.java (limited to 'adapters/mso-adapter-utils/src/test') diff --git a/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/beans/DeploymentInfoBuilderTest.java b/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/beans/DeploymentInfoBuilderTest.java new file mode 100644 index 0000000000..8f172b79ca --- /dev/null +++ b/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/beans/DeploymentInfoBuilderTest.java @@ -0,0 +1,168 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : SO + * ================================================================================ + * Copyright (C) 2018 Nokia. + * ============================================================================= + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.so.cloudify.beans; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.ImmutableMap; +import org.junit.Test; +import org.onap.so.cloudify.v3.model.Execution; + +public class DeploymentInfoBuilderTest { + + private static final String ERROR_MESSAGE = "something went wrong"; + private static final String INSTALL_WORKFLOW_ID = "install"; + private static final String UNINSTALL_WORKFLOW_ID = "uninstall"; + + @Test + public void shouldConstructDeploymentInfo_withBasicValues() { + DeploymentInfo deploymentInfo = new DeploymentInfoBuilder() + .withId("id") + .withStatus(DeploymentStatus.CREATED) + .withDeploymentOutputs(ImmutableMap.of()) + .withDeploymentInputs(ImmutableMap.of()) + .withActionStatus("started") + .withLastAction(INSTALL_WORKFLOW_ID) + .withErrorMessage(ERROR_MESSAGE) + .build(); + + assertThat(deploymentInfo.getId()).isEqualTo("id"); + assertThat(deploymentInfo.getStatus()).isEqualTo(DeploymentStatus.CREATED); + assertThat(deploymentInfo.getOutputs()).isEqualTo(ImmutableMap.of()); + assertThat(deploymentInfo.getInputs()).isEqualTo(ImmutableMap.of()); + assertThat(deploymentInfo.getActionStatus()).isEqualTo("started"); + assertThat(deploymentInfo.getLastAction()).isEqualTo(INSTALL_WORKFLOW_ID); + assertThat(deploymentInfo.getErrorMessage()).isEqualTo(ERROR_MESSAGE); + } + + @Test + public void shouldConstructDeploymentInfo_withCreateDeploymentStatus_fromNullExecution() { + DeploymentInfo deploymentInfo = new DeploymentInfoBuilder() + .fromExecution(null) + .build(); + + assertThat(deploymentInfo.getStatus()).isEqualTo(DeploymentStatus.CREATED); + } + + @Test + public void shouldConstructDeploymentInfo_withInstalledDeploymentStatus_fromTerminatedExecution() { + String workflowIdLastAction = INSTALL_WORKFLOW_ID; + String status = "terminated"; + DeploymentStatus expectedDeploymentStatus = DeploymentStatus.INSTALLED; + verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus); + } + + @Test + public void shouldConstructDeploymentInfo_withFailedDeploymentStatus_fromFailedInstallExecution() { + String workflowIdLastAction = INSTALL_WORKFLOW_ID; + String status = "failed"; + DeploymentStatus expectedDeploymentStatus = DeploymentStatus.FAILED; + verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus); + } + + @Test + public void shouldConstructDeploymentInfo_withInstallingDeploymentStatus_fromStartedExecution() { + String workflowIdLastAction = INSTALL_WORKFLOW_ID; + String status = "started"; + DeploymentStatus expectedDeploymentStatus = DeploymentStatus.INSTALLING; + verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus); + } + + @Test + public void shouldConstructDeploymentInfo_withInstallingDeploymentStatus_fromPendingExecution() { + String workflowIdLastAction = INSTALL_WORKFLOW_ID; + String status = "pending"; + DeploymentStatus expectedDeploymentStatus = DeploymentStatus.INSTALLING; + verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus); + } + + @Test + public void shouldConstructDeploymentInfo_withUnknownDeploymentStatus_fromUnmappableExecution() { + String workflowIdLastAction = INSTALL_WORKFLOW_ID; + String status = "strangeStatus"; + DeploymentStatus expectedDeploymentStatus = DeploymentStatus.UNKNOWN; + verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus); + } + + @Test + public void shouldConstructDeploymentInfo_withCreatedDeploymentStatus_fromTerminatedExecution() { + String workflowIdLastAction = UNINSTALL_WORKFLOW_ID; + String status = "terminated"; + DeploymentStatus expectedDeploymentStatus = DeploymentStatus.CREATED; + verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus); + } + + @Test + public void shouldConstructDeploymentInfo_withFailedDeploymentStatus_fromFailedUninstallExecution() { + String workflowIdLastAction = UNINSTALL_WORKFLOW_ID; + String status = "failed"; + DeploymentStatus expectedDeploymentStatus = DeploymentStatus.FAILED; + verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus); + } + + @Test + public void shouldConstructDeploymentInfo_withUninstallingDeploymentStatus_fromStartedUninstallExecution() { + String workflowIdLastAction = UNINSTALL_WORKFLOW_ID; + String status = "started"; + DeploymentStatus expectedDeploymentStatus = DeploymentStatus.UNINSTALLING; + verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus); + } + + @Test + public void shouldConstructDeploymentInfo_withUninstallingDeploymentStatus_fromPendingUninstallExecution() { + String workflowIdLastAction = UNINSTALL_WORKFLOW_ID; + String status = "pending"; + DeploymentStatus expectedDeploymentStatus = DeploymentStatus.UNINSTALLING; + verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus); + } + + @Test + public void shouldConstructDeploymentInfo_withUnknownDeploymentStatus_fromUnmappableUninstallExecution() { + String workflowIdLastAction = UNINSTALL_WORKFLOW_ID; + String status = "strangeStatus"; + DeploymentStatus expectedDeploymentStatus = DeploymentStatus.UNKNOWN; + verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus); + } + + @Test + public void shouldConstructDeploymentInfo_withUnknownDeploymentStatus_forUnknownExecutionWorkflowId() { + String workflowIdLastAction = "strangeWorkflowIdLastAction"; + String status = "strangeStatus"; + DeploymentStatus expectedDeploymentStatus = DeploymentStatus.UNKNOWN; + verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus); + } + + private void verifyDeploymentInfoConstruction(String workflowIdLastAction, String actionStatus, + DeploymentStatus expectedDeploymentStatus) { + + Execution execution = new Execution(); + execution.setWorkflowId(workflowIdLastAction); + execution.setStatus(actionStatus); + execution.setError(ERROR_MESSAGE); + DeploymentInfo deploymentInfo = new DeploymentInfoBuilder() + .fromExecution(execution) + .build(); + + assertThat(deploymentInfo.getLastAction()).isEqualTo(workflowIdLastAction); + assertThat(deploymentInfo.getActionStatus()).isEqualTo(actionStatus); + assertThat(deploymentInfo.getErrorMessage()).isEqualTo(ERROR_MESSAGE); + assertThat(deploymentInfo.getStatus()).isEqualTo(expectedDeploymentStatus); + } +} \ No newline at end of file diff --git a/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/beans/DeploymentInfoTest.java b/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/beans/DeploymentInfoTest.java deleted file mode 100644 index e200f9aa96..0000000000 --- a/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/beans/DeploymentInfoTest.java +++ /dev/null @@ -1,76 +0,0 @@ -/* -* ============LICENSE_START======================================================= - * ONAP : SO - * ================================================================================ - * Copyright (C) 2018 TechMahindra - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= -*/ - -package org.onap.so.cloudify.beans; - -import static org.mockito.Mockito.mock; -import java.util.HashMap; -import java.util.Map; -import org.junit.Test; -import org.mockito.Mock; -import org.onap.so.cloudify.v3.model.Deployment; -import org.onap.so.cloudify.v3.model.DeploymentOutputs; -import org.onap.so.cloudify.v3.model.Execution; -import org.powermock.api.mockito.PowerMockito; - -public class DeploymentInfoTest { - - @Mock - DeploymentStatus status; - - @Mock - DeploymentOutputs out; - - @Mock - Execution execution; - - @Mock - Deployment deployment; - - @Test - public void test() { - Deployment deployment=mock(Deployment.class); - Map dep=new HashMap(); - Map outputs = new HashMap(); - Map inputs = new HashMap(); - inputs.put("id",dep); - status=DeploymentStatus.CREATED; - outputs.put("id", out); - dep.put("id", outputs); - DeploymentInfo dinfo=new DeploymentInfo(deployment); - DeploymentInfo dinfi=new DeploymentInfo("id"); - DeploymentInfo din=new DeploymentInfo("id",outputs); - DeploymentInfo dfo=new DeploymentInfo("id", status); - DeploymentInfo dfoi=new DeploymentInfo(deployment, out, execution); - dinfo=PowerMockito.spy(new DeploymentInfo()); - dinfo.setId("id"); - dinfi.setInputs(inputs); - din.setStatus(status); - din.setOutputs(outputs); - assert(din.toString()!=null); - assert(din.getOutputs().equals(outputs)); - assert(din.getId().equals("id")); - assert(din.getStatus().equals(status)); - din.getLastAction(); - din.getErrorMessage(); - din.getActionStatus(); - } - -} diff --git a/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/utils/MsoCloudifyUtilsTest2.java b/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/utils/MsoCloudifyUtilsTest2.java index 96202c5122..c7aecd90be 100644 --- a/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/utils/MsoCloudifyUtilsTest2.java +++ b/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/utils/MsoCloudifyUtilsTest2.java @@ -2,14 +2,16 @@ * ============LICENSE_START======================================================= * ONAP - SO * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2018 Nokia. * ================================================================================ * 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. @@ -17,7 +19,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.onap.so.cloudify.utils; import static com.shazam.shazamcrest.MatcherAssert.assertThat; @@ -42,6 +43,7 @@ import org.onap.so.adapters.vdu.VduModelInfo; import org.onap.so.adapters.vdu.VduStateType; import org.onap.so.adapters.vdu.VduStatus; import org.onap.so.cloud.CloudConfig; +import org.onap.so.cloudify.beans.DeploymentInfoBuilder; import org.onap.so.db.catalog.beans.CloudIdentity; import org.onap.so.db.catalog.beans.CloudSite; import org.onap.so.cloudify.beans.DeploymentInfo; @@ -82,9 +84,10 @@ public class MsoCloudifyUtilsTest2 { List artifacts = new ArrayList<>(); artifacts.add(artifact); vduModel.setArtifacts(artifacts); - DeploymentInfo deployment = new DeploymentInfo(); - deployment.setId("id"); - deployment.setStatus(DeploymentStatus.INSTALLED); + DeploymentInfo deployment = new DeploymentInfoBuilder() + .withId("id") + .withStatus(DeploymentStatus.INSTALLED) + .build(); Map blueprintFiles = new HashMap<>(); blueprintFiles.put(artifact.getName(), artifact.getContent()); String instanceName = "instanceName"; @@ -118,9 +121,10 @@ public class MsoCloudifyUtilsTest2 { CloudInfo cloudInfo = new CloudInfo(); cloudInfo.setCloudSiteId("cloudSiteId"); cloudInfo.setTenantId("tenantId"); - DeploymentInfo deployment = new DeploymentInfo(); - deployment.setId("id"); - deployment.setStatus(DeploymentStatus.INSTALLED); + DeploymentInfo deployment = new DeploymentInfoBuilder() + .withId("id") + .withStatus(DeploymentStatus.INSTALLED) + .build(); String instanceId = "instanceId"; MsoCloudifyUtils cloudify = Mockito.spy(MsoCloudifyUtils.class); @@ -148,14 +152,12 @@ public class MsoCloudifyUtilsTest2 { cloudInfo.setTenantId("tenantId"); String instanceId = "instanceId"; int timeoutMinutes = 1; - DeploymentInfo deployment = Mockito.mock(DeploymentInfo.class); - deployment.setId("id"); - deployment.setStatus(DeploymentStatus.CREATED); - when(deployment.getId()).thenReturn("id"); - when(deployment.getStatus()).thenReturn(DeploymentStatus.CREATED); - when(deployment.getLastAction()).thenReturn("deleting"); + DeploymentInfo deploymentInfo = new DeploymentInfoBuilder() + .withId("id") + .withStatus(DeploymentStatus.CREATED) + .withLastAction("deleting").build(); MsoCloudifyUtils cloudify = Mockito.spy(MsoCloudifyUtils.class); - doReturn(deployment).when(cloudify).uninstallAndDeleteDeployment(cloudInfo.getCloudSiteId(), + doReturn(deploymentInfo).when(cloudify).uninstallAndDeleteDeployment(cloudInfo.getCloudSiteId(), cloudInfo.getTenantId(), instanceId, timeoutMinutes); VduInstance actual = cloudify.deleteVdu(cloudInfo, instanceId, timeoutMinutes); @@ -173,16 +175,14 @@ public class MsoCloudifyUtilsTest2 { status.setLastAction(new PluginAction("deleting", null, null)); expected.setStatus(status); - DeploymentInfo deployment = Mockito.mock(DeploymentInfo.class); - deployment.setId("id"); - deployment.setStatus(DeploymentStatus.CREATED); - when(deployment.getId()).thenReturn("id"); - when(deployment.getStatus()).thenReturn(DeploymentStatus.CREATED); - when(deployment.getLastAction()).thenReturn("deleting"); + DeploymentInfo deploymentInfo = new DeploymentInfoBuilder() + .withId("id") + .withStatus(DeploymentStatus.CREATED) + .withLastAction("deleting").build(); MsoCloudifyUtils cloudify = new MsoCloudifyUtils(); - VduInstance actual = cloudify.deploymentInfoToVduInstance(deployment); + VduInstance actual = cloudify.deploymentInfoToVduInstance(deploymentInfo); assertThat(actual, sameBeanAs(expected)); } @@ -193,16 +193,14 @@ public class MsoCloudifyUtilsTest2 { expected.setState(VduStateType.DELETING); expected.setLastAction(new PluginAction("deleting", null, null)); - DeploymentInfo deployment = Mockito.mock(DeploymentInfo.class); - deployment.setId("id"); - deployment.setStatus(DeploymentStatus.CREATED); - when(deployment.getId()).thenReturn("id"); - when(deployment.getStatus()).thenReturn(DeploymentStatus.CREATED); - when(deployment.getLastAction()).thenReturn("deleting"); + DeploymentInfo deploymentInfo = new DeploymentInfoBuilder() + .withId("id") + .withStatus(DeploymentStatus.CREATED) + .withLastAction("deleting").build(); MsoCloudifyUtils cloudify = new MsoCloudifyUtils(); - VduStatus actual = cloudify.deploymentStatusToVduStatus(deployment); + VduStatus actual = cloudify.deploymentStatusToVduStatus(deploymentInfo); assertThat(actual, sameBeanAs(expected)); } -- cgit 1.2.3-korg