From 181c40dc08d42c019bbaeb85fbcfc72a9dc685a5 Mon Sep 17 00:00:00 2001 From: Lukasz Muszkieta Date: Thu, 8 Nov 2018 15:46:26 +0100 Subject: Add junit tests for CreateAAIVfModule Change-Id: If71c2472aefc7edd1387837bdbbb24a186c1316c Issue-ID: SO-784 Signed-off-by: Lukasz Muszkieta --- .../bpmn/common/scripts/CreateAAIVfModule.groovy | 122 ++----------------- .../common/scripts/CreateAAIVfModuleTest.groovy | 132 ++++++++++++++++++++- 2 files changed, 139 insertions(+), 115 deletions(-) diff --git a/bpmn/MSOCommonBPMN/src/main/groovy/org/onap/so/bpmn/common/scripts/CreateAAIVfModule.groovy b/bpmn/MSOCommonBPMN/src/main/groovy/org/onap/so/bpmn/common/scripts/CreateAAIVfModule.groovy index 48268464d8..370600755b 100644 --- a/bpmn/MSOCommonBPMN/src/main/groovy/org/onap/so/bpmn/common/scripts/CreateAAIVfModule.groovy +++ b/bpmn/MSOCommonBPMN/src/main/groovy/org/onap/so/bpmn/common/scripts/CreateAAIVfModule.groovy @@ -19,6 +19,8 @@ */ package org.onap.so.bpmn.common.scripts + +import org.apache.commons.lang.StringUtils import org.camunda.bpm.engine.delegate.DelegateExecution import org.onap.aai.domain.yang.GenericVnf import org.onap.so.bpmn.core.RollbackData @@ -94,55 +96,25 @@ public class CreateAAIVfModule extends AbstractServiceTaskProcessor{ execution.setVariable("CAAIVfMod_vnfName", vnfName) String vnfType = execution.getVariable("vnfType") - if (vnfType != null && !vnfType.isEmpty()) { - execution.setVariable("CAAIVfMod_vnfType", vnfType) - } else { - execution.setVariable("CAAIVfMod_vnfType","") - } + execution.setVariable("CAAIVfMod_vnfType", StringUtils.defaultString(vnfType)) execution.setVariable("CAAIVfMod_serviceId", execution.getVariable("serviceId")) String personaModelId = execution.getVariable("personaModelId") + execution.setVariable("CAAIVfMod_personaId",StringUtils.defaultString(personaModelId)) - if (personaModelId != null && !personaModelId.isEmpty()) { - execution.setVariable("CAAIVfMod_personaId",personaModelId) - } else { - execution.setVariable("CAAIVfMod_personaId","") - } - String personaModelVersion = execution.getVariable("personaModelVersion") + execution.setVariable("CAAIVfMod_personaVer", StringUtils.defaultString(personaModelVersion)) - if (personaModelVersion != null && !personaModelVersion.isEmpty()) { - execution.setVariable("CAAIVfMod_personaVer", personaModelVersion) - } else { - execution.setVariable("CAAIVfMod_personaVer","") - } - - String modelCustomizationId = execution.getVariable("modelCustomizationId") + execution.setVariable("CAAIVfMod_modelCustomizationId",StringUtils.defaultString(modelCustomizationId)) - if (modelCustomizationId != null && !modelCustomizationId.isEmpty()) { - execution.setVariable("CAAIVfMod_modelCustomizationId",modelCustomizationId) - } else { - execution.setVariable("CAAIVfMod_modelCustomizationId","") - } - String vnfPersonaModelId = execution.getVariable("vnfPersonaModelId") - - if (vnfPersonaModelId != null && !vnfPersonaModelId.isEmpty()) { - execution.setVariable("CAAIVfMod_vnfPersonaId",vnfPersonaModelId) - } else { - execution.setVariable("CAAIVfMod_vnfPersonaId","") - } - + execution.setVariable("CAAIVfMod_vnfPersonaId", StringUtils.defaultString(vnfPersonaModelId)) + String vnfPersonaModelVersion = execution.getVariable("vnfPersonaModelVersion") + execution.setVariable("CAAIVfMod_vnfPersonaVer", StringUtils.defaultString(vnfPersonaModelVersion)) - if (vnfPersonaModelVersion != null && !vnfPersonaModelVersion.isEmpty()) { - execution.setVariable("CAAIVfMod_vnfPersonaVer",vnfPersonaModelVersion) - } else { - execution.setVariable("CAAIVfMod_vnfPersonaVer","") - } - //isBaseVfModule Boolean isBaseVfModule = false String isBaseVfModuleString = execution.getVariable("isBaseVfModule") @@ -150,7 +122,7 @@ public class CreateAAIVfModule extends AbstractServiceTaskProcessor{ isBaseVfModule = true } execution.setVariable("CAAIVfMod_isBaseVfModule", isBaseVfModule) - + String isVidRequest = execution.getVariable("isVidRequest") if (isVidRequest != null && "true".equals(isVidRequest)) { msoLogger.debug("VID Request received") @@ -166,7 +138,7 @@ public class CreateAAIVfModule extends AbstractServiceTaskProcessor{ execution.setVariable("CAAIVfMod_aaiNamespace",aaiNamespace) } - + // send a GET request to AA&I to retrieve the Generic VNF/VF Module information based on a Vnf Name // expect a 200 response with the information in the response body or a 404 if the Generic VNF does not exist public void queryAAIForGenericVnf(DelegateExecution execution) { @@ -424,8 +396,9 @@ public class CreateAAIVfModule extends AbstractServiceTaskProcessor{ } } } + if (qryModuleList != null && !qryModuleList.getVfModule().isEmpty() && !execution.getVariable("CAAIVfMod_baseModuleConflict")) { - def qryModules = qryModuleList.getVfModule() + def qryModules = qryModuleList.getVfModule() for (org.onap.aai.domain.yang.VfModule qryModule : qryModules) { if (qryModule.isBaseVfModule) { // a base module already exists in this VNF - failure @@ -501,73 +474,4 @@ public class CreateAAIVfModule extends AbstractServiceTaskProcessor{ exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, errorResponse) msoLogger.debug("Workflow exception occurred in CreateAAIVfModule: " + errorResponse) } - - /** - * Performs a rollback. - * TBD: This method requires additional testing once integrated with the - * main CreateVfModule flow. - * @param execution the execution - */ - public void rollback(DelegateExecution execution) { - def method = getClass().getSimpleName() + ".rollback(" + - "execution=" + execution.getId() + - ")" - msoLogger.debug("Entered " + method) - - try { - RollbackData rollbackData = (RollbackData) execution.getVariable("RollbackData") - msoLogger.debug("RollbackData:" + rollbackData) - - AaiUtil aaiUriUtil = new AaiUtil(this) - - if (rollbackData != null) { - if (rollbackData.hasType("VFMODULE")) { - // use the DeleteAAIVfModule groovy methods for the rollback - def vnfId = rollbackData.get("VFMODULE", "vnfId") - def vfModuleId = rollbackData.get("VFMODULE", "vfModuleId") - def isBaseModule = rollbackData.get("VFMODULE", "isBaseModule") - execution.setVariable("DAAIVfMod_vnfId", vnfId) - execution.setVariable("DAAIVfMod_vfModuleId", vfModuleId) - - DeleteAAIVfModule dvm = new DeleteAAIVfModule() - // query A&AI to get the needed information for the delete(s) - dvm.queryAAIForGenericVnf(execution) - dvm.parseForVfModule(execution) - - // roll back the base or add-on module - dvm.deleteVfModule(execution) - def responseCode = execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") - def response = execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") - - if (isOneOf(responseCode, 200, 204)) { - msoLogger.debug("Received " + responseCode + " to VF Module rollback request") - } else { - msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Received " + responseCode + " to VF Module rollback request", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, rollbackData + System.lineSeparator() + "Response: " + response); - } - - // a new Generic VNF was created that needs to be rolled back - if (isBaseModule.equals("true")) { - dvm.queryAAIForGenericVnf(execution) - dvm.parseForResourceVersion(execution) - dvm.deleteGenericVnf(execution) - responseCode = execution.getVariable("DAAIVfMod_deleteGenericVnfResponseCode") - response = execution.getVariable("DAAIVfMod_deleteGenericVnfResponse") - - if (isOneOf(responseCode, 200, 204)) { - msoLogger.debug("Received " + responseCode + " to Generic VNF rollback request") - execution.setVariable("RollbackResult", "SUCCESS") - } else { - msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Received " + responseCode + " to Generic VNF rollback request", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, rollbackData + System.lineSeparator() + "Response: " + response); - } - } else { - execution.setVariable("RollbackResult", "SUCCESS") - } - } - } - - msoLogger.debug("Exited " + method) - } catch (Exception e) { - msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Caught exception in " + method, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e); - } - } } diff --git a/bpmn/MSOCommonBPMN/src/test/groovy/org/onap/so/bpmn/common/scripts/CreateAAIVfModuleTest.groovy b/bpmn/MSOCommonBPMN/src/test/groovy/org/onap/so/bpmn/common/scripts/CreateAAIVfModuleTest.groovy index 886e92bd45..1fa10a56c5 100644 --- a/bpmn/MSOCommonBPMN/src/test/groovy/org/onap/so/bpmn/common/scripts/CreateAAIVfModuleTest.groovy +++ b/bpmn/MSOCommonBPMN/src/test/groovy/org/onap/so/bpmn/common/scripts/CreateAAIVfModuleTest.groovy @@ -10,9 +10,9 @@ * 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. @@ -23,6 +23,8 @@ package org.onap.so.bpmn.common.scripts +import org.camunda.bpm.engine.delegate.DelegateExecution + import static org.assertj.core.api.Assertions.assertThat import static org.mockito.ArgumentMatchers.any import static org.mockito.ArgumentMatchers.anyObject @@ -57,12 +59,12 @@ class CreateAAIVfModuleTest extends MsoGroovyTest{ private static final String VF_MODULE_MODEL_NAME = "modModelNameTest" private static final String DEFAULT_AAI_VERSION = "9" private static final String DEFAULT_AAI_NAMESPACE = "defaultTestNamespace" - + @Spy CreateAAIVfModule createAAIVfModule ; private DelegateExecutionFake executionFake; - + @Before public void init() throws IOException { super.init("CreateAAIVfModule") @@ -236,7 +238,7 @@ class CreateAAIVfModuleTest extends MsoGroovyTest{ @Test void parseForAddOnModule_moduleNameFound() { - + GenericVnf vnf = new GenericVnf(); VfModule module = new VfModule(); VfModules modules = new VfModules(); @@ -302,7 +304,7 @@ class CreateAAIVfModuleTest extends MsoGroovyTest{ modules.getVfModule().add(module) module.setVfModuleName(VF_MODULE_NAME) module.setIsBaseVfModule(true) - + executionFake.setVariable("CAAIVfMod_queryGenericVnfResponse", vnf) executionFake.setVariable("CAAIVfMod_baseModuleConflict", false) @@ -331,6 +333,103 @@ class CreateAAIVfModuleTest extends MsoGroovyTest{ .isEqualTo("VF Module " + VF_MODULE_NAME + " does not exist for Generic VNF " + VNF_NAME) } + @Test + void handleFailure_errorCode5000() { + executionFake.setVariable("CAAIVfMod_createGenericVnfResponseCode", "123") + executionFake.setVariable("CAAIVfMod_createGenericVnfResponse", "responseTest") + + ExceptionUtilForTesting exceptionUtilForTesting = new ExceptionUtilForTesting() + createAAIVfModule.setExceptionUtil(exceptionUtilForTesting) + + createAAIVfModule.handleCreateVfModuleFailure(executionFake) + + assertThat(exceptionUtilForTesting.getErrorCode()).isEqualTo(5000) + assertThat(exceptionUtilForTesting.getErrorMessage()).isEqualTo("responseTest") + } + + @Test + void handleFailure_errorCode1002() { + executionFake.setVariable("CAAIVfMod_queryGenericVnfResponse", "responseTest") + executionFake.setVariable("CAAIVfMod_newGenericVnf", true) + + ExceptionUtilForTesting exceptionUtilForTesting = new ExceptionUtilForTesting() + createAAIVfModule.setExceptionUtil(exceptionUtilForTesting) + + createAAIVfModule.handleCreateVfModuleFailure(executionFake) + + assertThat(exceptionUtilForTesting.getErrorCode()).isEqualTo(1002) + assertThat(exceptionUtilForTesting.getErrorMessage()).isEqualTo("responseTest") + } + + @Test + void handleFailure_errorCode1002_queryGenericVnfResponse() { + executionFake.setVariable("CAAIVfMod_queryGenericVnfResponse", "responseTest") + executionFake.setVariable("CAAIVfMod_queryGenericVnfResponseCode", 404) + executionFake.setVariable("CAAIVfMod_newGenericVnf", false) + + ExceptionUtilForTesting exceptionUtilForTesting = new ExceptionUtilForTesting() + createAAIVfModule.setExceptionUtil(exceptionUtilForTesting) + + createAAIVfModule.handleCreateVfModuleFailure(executionFake) + + assertThat(exceptionUtilForTesting.getErrorCode()).isEqualTo(1002) + assertThat(exceptionUtilForTesting.getErrorMessage()).isEqualTo("responseTest") + } + + @Test + void handleFailure_errorCode5000_createVfModuleResponseCode() { + executionFake.setVariable("CAAIVfMod_createVfModuleResponseCode", "123") + executionFake.setVariable("CAAIVfMod_createVfModuleResponse", "responseTest") + + ExceptionUtilForTesting exceptionUtilForTesting = new ExceptionUtilForTesting() + createAAIVfModule.setExceptionUtil(exceptionUtilForTesting) + + createAAIVfModule.handleCreateVfModuleFailure(executionFake) + + assertThat(exceptionUtilForTesting.getErrorCode()).isEqualTo(5000) + assertThat(exceptionUtilForTesting.getErrorMessage()).isEqualTo("responseTest") + } + + @Test + void handleFailure_errorCode1002_moduleExists() { + executionFake.setVariable("CAAIVfMod_moduleExists", true) + executionFake.setVariable("CAAIVfMod_parseModuleResponse", "responseTest") + + ExceptionUtilForTesting exceptionUtilForTesting = new ExceptionUtilForTesting() + createAAIVfModule.setExceptionUtil(exceptionUtilForTesting) + + createAAIVfModule.handleCreateVfModuleFailure(executionFake) + + assertThat(exceptionUtilForTesting.getErrorCode()).isEqualTo(1002) + assertThat(exceptionUtilForTesting.getErrorMessage()).isEqualTo("responseTest") + } + + @Test + void handleFailure_errorCode1002_baseModuleConflict() { + executionFake.setVariable("CAAIVfMod_baseModuleConflict", true) + executionFake.setVariable("CAAIVfMod_parseModuleResponse", "responseTest") + + ExceptionUtilForTesting exceptionUtilForTesting = new ExceptionUtilForTesting() + createAAIVfModule.setExceptionUtil(exceptionUtilForTesting) + + createAAIVfModule.handleCreateVfModuleFailure(executionFake) + + assertThat(exceptionUtilForTesting.getErrorCode()).isEqualTo(1002) + assertThat(exceptionUtilForTesting.getErrorMessage()).isEqualTo("responseTest") + } + + @Test + void handleFailure_errorCode2000() { + ExceptionUtilForTesting exceptionUtilForTesting = new ExceptionUtilForTesting() + createAAIVfModule.setExceptionUtil(exceptionUtilForTesting) + + createAAIVfModule.handleCreateVfModuleFailure(executionFake) + + assertThat(exceptionUtilForTesting.getErrorCode()).isEqualTo(2000) + assertThat(exceptionUtilForTesting.getErrorMessage()). + isEqualTo("Unknown error occurred during CreateAAIVfModule flow") + } + private void prepareUrnPropertiesReader() { Environment mockEnvironment = mock(Environment.class) when(mockEnvironment.getProperty("mso.workflow.global.default.aai.version")).thenReturn(DEFAULT_AAI_VERSION) @@ -338,4 +437,25 @@ class CreateAAIVfModuleTest extends MsoGroovyTest{ UrnPropertiesReader urnPropertiesReader = new UrnPropertiesReader() urnPropertiesReader.setEnvironment(mockEnvironment) } + + class ExceptionUtilForTesting extends ExceptionUtil { + private int errorCode + private String errorMessage + + int getErrorCode() { + return errorCode + } + + String getErrorMessage() { + return errorMessage + } + + @Override + void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, + String errorMessage) { + this.errorCode = errorCode + this.errorMessage = errorMessage + } + } + } -- cgit 1.2.3-korg