From 329da0d034a4645685ac0fe7f8e61d24e435d5dc Mon Sep 17 00:00:00 2001 From: rameshiyer27 Date: Wed, 19 Aug 2020 10:22:20 +0100 Subject: Picking workflow names dynamically for schema update procedure Issue-ID: SO-3147 Signed-off-by: zrrmmua Change-Id: I4c9de93ea7b51657c2bc6ee24b4bb52e2068cf5a --- .../level/AbstractServiceLevelPreparable.java | 72 --------------- .../service/level/ServiceLevelPreparable.java | 98 ++++++++++++++++++++ .../service/level/impl/ServiceLevelConstants.java | 48 ++++++++-- .../level/impl/ServiceLevelPreparation.java | 55 ++++++------ .../service/level/impl/ServiceLevelUpgrade.java | 69 ++++++++------ .../service/level/ServiceLevelPreparationTest.java | 100 +++++++++++++-------- 6 files changed, 268 insertions(+), 174 deletions(-) delete mode 100644 bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/AbstractServiceLevelPreparable.java create mode 100644 bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparable.java (limited to 'bpmn/so-bpmn-tasks') diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/AbstractServiceLevelPreparable.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/AbstractServiceLevelPreparable.java deleted file mode 100644 index 0671354bd6..0000000000 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/AbstractServiceLevelPreparable.java +++ /dev/null @@ -1,72 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nordix Foundation. - * ================================================================================ - * 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.so.bpmn.infrastructure.service.level; - -import org.camunda.bpm.engine.delegate.DelegateExecution; -import org.onap.so.bpmn.infrastructure.service.level.impl.ServiceLevelConstants; -import org.onap.so.client.exception.ExceptionBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import java.util.ArrayList; -import java.util.List; - -/** - * Abstract class for Service level upgrade Execution, it should be extended for service level upgrade tasks. - */ -public abstract class AbstractServiceLevelPreparable { - - protected static final Logger LOG = LoggerFactory.getLogger(AbstractServiceLevelPreparable.class); - - @Autowired - protected ExceptionBuilder exceptionBuilder; - - /** - * This method fetches workflow names to be invoked based on the controller scope . - * - * @param scope Controller scope - * @return String value of Workflow name - */ - protected abstract String fetchWorkflowUsingScope(DelegateExecution execution, final String scope); - - /** - * This method validates the execution parameters to be passed for health check workflow. - * - * @param execution Delegate execution obj - * @param scope Controller scope * Throws workflow exception if validation fails - */ - protected void validateParamsWithScope(DelegateExecution execution, final String scope, List params) { - List invalidVariables = new ArrayList<>(); - for (String param : params) { - if (!execution.hasVariable(param) || execution.getVariable(param) == null - || String.valueOf(execution.getVariable(param)).isEmpty()) { - invalidVariables.add(param); - } - } - if (invalidVariables.size() > 0) { - LOG.error("Validation error for the {} health check attributes: {}", scope, invalidVariables); - exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, - "Validation of health check workflow parameters failed for the scope: " + scope); - } - - } - -} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparable.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparable.java new file mode 100644 index 0000000000..e26195158d --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparable.java @@ -0,0 +1,98 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * 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.so.bpmn.infrastructure.service.level; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import org.camunda.bpm.engine.delegate.DelegateExecution; +import org.onap.so.bpmn.infrastructure.service.level.impl.ServiceLevelConstants; +import org.onap.so.client.exception.ExceptionBuilder; +import org.onap.so.db.catalog.beans.Workflow; +import org.onap.so.db.catalog.client.CatalogDbClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * Parent class for Service level upgrade Execution, it should be extended for service level upgrade tasks. + */ +public class ServiceLevelPreparable { + + protected static final Logger LOG = LoggerFactory.getLogger(ServiceLevelPreparable.class); + + @Autowired + protected ExceptionBuilder exceptionBuilder; + + @Autowired + protected CatalogDbClient catalogDbClient; + + /** + * Fetches workflow names based on the controller scope and operation name. + * + * @param scope Controller scope + * @param operationName healthcheck/softwareUpgrade + * @return String value of Workflow name + */ + protected String fetchWorkflowUsingScope(final String scope, String operationName) { + Optional wflName = Optional.empty(); + try { + List workflows = catalogDbClient.findWorkflowByOperationName(operationName); + if (!workflows.isEmpty()) { + wflName = Optional.ofNullable( + workflows.stream().filter(workflow -> workflow.getResourceTarget().equalsIgnoreCase(scope)) + .findFirst().get().getName()); + } + } catch (Exception e) { + // do nothing and assign the default workflow in finally + LOG.error("Error occurred while fetching workflow name from CatalogDb {}", e); + } finally { + if (wflName.isEmpty()) { + wflName = Optional.of(ServiceLevelConstants.WORKFLOW_OPERATIONS_MAP.get(operationName).get(scope)); + } + } + return wflName.get(); + + } + + /** + * This method validates the execution parameters to be passed for health check workflow. + * + * @param execution Delegate execution obj + * @param scope Controller scope * Throws workflow exception if validation fails + */ + protected void validateParamsWithScope(DelegateExecution execution, final String scope, List params) { + List invalidVariables = new ArrayList<>(); + for (String param : params) { + if (!execution.hasVariable(param) || execution.getVariable(param) == null + || String.valueOf(execution.getVariable(param)).isEmpty()) { + invalidVariables.add(param); + } + } + if (invalidVariables.size() > 0) { + LOG.error("Validation error for the {} health check attributes: {}", scope, invalidVariables); + exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, + "Validation of health check workflow parameters failed for the scope: " + scope); + } + + } + +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelConstants.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelConstants.java index 34ebb308c3..d94641846d 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelConstants.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelConstants.java @@ -1,5 +1,29 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * 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.so.bpmn.infrastructure.service.level.impl; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + public class ServiceLevelConstants { public static final String BPMN_REQUEST = "bpmnRequest"; public static final String RESOURCE_TYPE = "resourceType"; @@ -8,13 +32,27 @@ public class ServiceLevelConstants { public static final String PNF = "pnf"; public static final String VNF = "vnf"; public static final String EMPTY_STRING = ""; - public static final String WORKFLOW_TO_INVOKE = "healthCheckWorkflow"; + public static final String HEALTH_CHECK_WORKFLOW_TO_INVOKE = "healthCheckWorkflow"; public static final String SOFTWARE_WORKFLOW_TO_INVOKE = "softwareUpgradeWorkflow"; - public static final String GENERIC_PNF_HEALTH_CHECK_WORKFLOW = "GenericPnfHealthCheck"; - public static final String PNF_SOFTWARE_UPGRADE_WORKFLOW = "PNFSoftwareUpgrade"; + public static final String HEALTH_CHECK_OPERATION = "ResourceHealthCheck"; + public static final String SW_UP_OPERATION = "ResourceSoftwareUpgrade"; public static final String CONTROLLER_STATUS = "ControllerStatus"; public static final int ERROR_CODE = 601; - // TODO This value needs to be updated once vnf health check workflow is available - protected static final String GENERIC_VNF_HEALTH_CHECK_WORKFLOW = "GenericVNFHealthCheck"; + + // TODO GenericVNFHealthCheck and GenericVnfSoftwareUpgrade workflow names should be updated once the workflow is + // implemented. + public static final Map DEFAULT_HEALTH_CHECK_WORKFLOWS = + Map.of(PNF, "GenericPnfHealthCheck", VNF, "GenericVNFHealthCheck"); + + public static final Map DEFAULT_SOFTWARE_UP_WORKFLOWS = + Map.of(PNF, "PNFSoftwareUpgrade", VNF, "GenericVnfSoftwareUpgrade"); + + // Maps operation name with workflows + public static final Map> WORKFLOW_OPERATIONS_MAP = Map.of(HEALTH_CHECK_OPERATION, + DEFAULT_HEALTH_CHECK_WORKFLOWS, SW_UP_OPERATION, DEFAULT_SOFTWARE_UP_WORKFLOWS); + + public static final List VALID_CONTROLLER_SCOPE = Arrays.asList(PNF, VNF); + + } diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelPreparation.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelPreparation.java index 5b772760f0..59884ecbc2 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelPreparation.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelPreparation.java @@ -20,59 +20,54 @@ package org.onap.so.bpmn.infrastructure.service.level.impl; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; -import org.onap.so.bpmn.infrastructure.service.level.AbstractServiceLevelPreparable; +import org.onap.so.bpmn.infrastructure.service.level.ServiceLevelPreparable; import org.springframework.stereotype.Component; -import java.util.Arrays; -import java.util.List; + /** * Fetches health check workflow based on the controller_scope. Invoke the corresponding health check workflow after * validation. */ @Component("ServiceLevelPreparation") -public class ServiceLevelPreparation extends AbstractServiceLevelPreparable implements JavaDelegate { +public class ServiceLevelPreparation extends ServiceLevelPreparable implements JavaDelegate { - private static final List PNF_HC_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID, + private static final List PNF_HEALTH_CHECK_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID, ServiceLevelConstants.RESOURCE_TYPE, ServiceLevelConstants.BPMN_REQUEST, ServiceLevelConstants.PNF_NAME); + // TODO Update the list with vnf health check parameters if any validation needed + private static final List VNF_HEALTH_CHECK_PARAMS = Collections.emptyList(); + + private static final Map> HEALTH_CHECK_PARAMS_MAP = Map.of(ServiceLevelConstants.PNF, + PNF_HEALTH_CHECK_PARAMS, ServiceLevelConstants.VNF, VNF_HEALTH_CHECK_PARAMS); + @Override public void execute(DelegateExecution execution) throws Exception { if (execution.hasVariable(ServiceLevelConstants.RESOURCE_TYPE) && execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE) != null) { final String controllerScope = (String) execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE); LOG.debug("Scope retrieved from delegate execution: " + controllerScope); - final String wflName = fetchWorkflowUsingScope(execution, controllerScope); - LOG.debug("Health check workflow fetched for the scope: {} is: {}", controllerScope, wflName); - - if (ServiceLevelConstants.PNF.equalsIgnoreCase(controllerScope)) { - validateParamsWithScope(execution, controllerScope, PNF_HC_PARAMS); + if (ServiceLevelConstants.VALID_CONTROLLER_SCOPE.contains(controllerScope)) { + final String wflName = + fetchWorkflowUsingScope(controllerScope, ServiceLevelConstants.HEALTH_CHECK_OPERATION); + LOG.debug("Health check workflow fetched for the scope: {} is: {}", controllerScope, wflName); + validateParamsWithScope(execution, controllerScope, HEALTH_CHECK_PARAMS_MAP.get(controllerScope)); LOG.info("Parameters validated successfully for {}", wflName); + execution.setVariable(ServiceLevelConstants.HEALTH_CHECK_WORKFLOW_TO_INVOKE, wflName); + execution.setVariable(ServiceLevelConstants.CONTROLLER_STATUS, ServiceLevelConstants.EMPTY_STRING); + } else { + exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, + "Invalid Controller scope to prepare resource level health check"); } - execution.setVariable(ServiceLevelConstants.WORKFLOW_TO_INVOKE, wflName); - execution.setVariable(ServiceLevelConstants.CONTROLLER_STATUS, ServiceLevelConstants.EMPTY_STRING); } else { exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, - "Controller scope not found to invoke resource level health check"); - } - } - - @Override - public String fetchWorkflowUsingScope(DelegateExecution execution, final String scope) { - String wflName = null; - switch (scope.toLowerCase()) { - case ServiceLevelConstants.PNF: - wflName = ServiceLevelConstants.GENERIC_PNF_HEALTH_CHECK_WORKFLOW; - break; - case ServiceLevelConstants.VNF: - wflName = ServiceLevelConstants.GENERIC_VNF_HEALTH_CHECK_WORKFLOW; - break; - default: - exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, - "No valid health check work flow retrieved for the scope: " + scope); + "Resource type not found in the execution to invoke resource level health check"); } - return wflName; } } diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelUpgrade.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelUpgrade.java index dfc77d6f0c..9d7d8efb65 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelUpgrade.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelUpgrade.java @@ -1,34 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * 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.so.bpmn.infrastructure.service.level.impl; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; -import org.onap.so.bpmn.infrastructure.service.level.AbstractServiceLevelPreparable; +import org.onap.so.bpmn.infrastructure.service.level.ServiceLevelPreparable; import org.springframework.stereotype.Component; -import java.util.Arrays; -import java.util.List; @Component -public class ServiceLevelUpgrade extends AbstractServiceLevelPreparable implements JavaDelegate { +public class ServiceLevelUpgrade extends ServiceLevelPreparable implements JavaDelegate { - private static final List PNF_SWU_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID, + private static final List PNF_SOFTWARE_UP_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID, ServiceLevelConstants.RESOURCE_TYPE, ServiceLevelConstants.BPMN_REQUEST, ServiceLevelConstants.PNF_NAME); - @Override - protected String fetchWorkflowUsingScope(DelegateExecution execution, String scope) { - String wflName = null; - switch (scope.toLowerCase()) { - case ServiceLevelConstants.PNF: - wflName = ServiceLevelConstants.PNF_SOFTWARE_UPGRADE_WORKFLOW; - break; - case ServiceLevelConstants.VNF: - wflName = ServiceLevelConstants.GENERIC_VNF_HEALTH_CHECK_WORKFLOW; - break; - default: - exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, - "No valid health check work flow retrieved for the scope: " + scope); - } - return wflName; - } + // TODO Update the list with vnf software upgrade parameters if any validation needed + private static final List VNF_SOFTWARE_UP_PARAMS = Collections.emptyList(); + + private static final Map> SOFTWARE_UP_PARAMS_MAP = Map.of(ServiceLevelConstants.PNF, + PNF_SOFTWARE_UP_PARAMS, ServiceLevelConstants.VNF, VNF_SOFTWARE_UP_PARAMS); + @Override public void execute(DelegateExecution execution) throws Exception { @@ -36,19 +48,20 @@ public class ServiceLevelUpgrade extends AbstractServiceLevelPreparable implemen && execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE) != null) { final String controllerScope = (String) execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE); LOG.debug("Scope retrieved from delegate execution: " + controllerScope); - final String wflName = fetchWorkflowUsingScope(execution, controllerScope); - LOG.debug("Health check workflow fetched for the scope: {} is: {}", controllerScope, wflName); - - if ("pnf".equalsIgnoreCase(controllerScope)) { - validateParamsWithScope(execution, controllerScope, PNF_SWU_PARAMS); + if (ServiceLevelConstants.VALID_CONTROLLER_SCOPE.contains(controllerScope)) { + final String wflName = fetchWorkflowUsingScope(controllerScope, ServiceLevelConstants.SW_UP_OPERATION); + LOG.debug("Software Upgrade workflow fetched for the scope: {} is: {}", controllerScope, wflName); + validateParamsWithScope(execution, controllerScope, SOFTWARE_UP_PARAMS_MAP.get(controllerScope)); LOG.info("Parameters validated successfully for {}", wflName); execution.setVariable(ServiceLevelConstants.SOFTWARE_WORKFLOW_TO_INVOKE, wflName); execution.setVariable(ServiceLevelConstants.CONTROLLER_STATUS, ""); + } else { + exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, + "Invalid Controller scope for resource level software upgrade"); } - } else { exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE, - "Controller scope not found to invoke resource level health check"); + "Resource type not found in the execution to invoke resource level software upgrade"); } } } diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparationTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparationTest.java index 340b2776a4..0b1c5b0f5e 100644 --- a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparationTest.java +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparationTest.java @@ -20,6 +20,13 @@ package org.onap.so.bpmn.infrastructure.service.level; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; import org.camunda.bpm.engine.delegate.BpmnError; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake; @@ -27,23 +34,16 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Spy; +import org.onap.so.bpmn.BaseTaskTest; import org.onap.so.bpmn.infrastructure.service.level.impl.ServiceLevelConstants; import org.onap.so.bpmn.infrastructure.service.level.impl.ServiceLevelPreparation; import org.onap.so.client.exception.ExceptionBuilder; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import static org.assertj.core.api.Assertions.assertThat; +import org.onap.so.db.catalog.beans.Workflow; -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = {ServiceLevelPreparation.class, ExceptionBuilder.class}) -public class ServiceLevelPreparationTest { +public class ServiceLevelPreparationTest extends BaseTaskTest { private static final String TEST_PNF_SCOPE = "pnf"; private static final String TEST_PROCESS_KEY = "testProcessKey"; @@ -52,42 +52,29 @@ public class ServiceLevelPreparationTest { private static final String RESOURCE_TYPE = "resourceType"; private static final String SERVICE_INSTANCE_ID = "serviceInstanceId"; private static final String PNF_NAME = "pnfName"; - private static final List PNF_HEALTH_CHECK_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID, - ServiceLevelConstants.RESOURCE_TYPE, ServiceLevelConstants.BPMN_REQUEST, ServiceLevelConstants.PNF_NAME); + private static final String HEALTH_CHECK_OPERATION = "ResourceHealthCheck"; + private static final String PNF_HEALTH_CHECK_WORKFLOW = "PNFHealthCheck"; + private static final Map> HEALTH_CHECK_PARAMS_MAP = Map.of(TEST_PNF_SCOPE, + Arrays.asList(SERVICE_INSTANCE_ID, RESOURCE_TYPE, BPMN_REQUEST, PNF_NAME), "vnf", Collections.emptyList()); + + private List workflowList = new ArrayList<>(); - private Map pnfHealthCheckTestParams = new HashMap<>(); + @Rule + public ExpectedException thrown = ExpectedException.none(); - @Autowired + @InjectMocks private ServiceLevelPreparation serviceLevelPrepare; - @Autowired + @InjectMocks + @Spy private ExceptionBuilder exceptionBuilder; - @Rule - public ExpectedException thrown = ExpectedException.none(); - private DelegateExecution execution = new DelegateExecutionFake(); private DelegateExecution invalidExecution = new DelegateExecutionFake(); @Before public void setUpPnfUpgradeTest() { - pnfHealthCheckTestParams.put("TEST_SERVICE_MODEL_INFO", "d4c6855e-3be2-5dtu-9390-c999a38829bc"); - pnfHealthCheckTestParams.put("TEST_SERVICE_INSTANCE_NAME", "test_service_id"); - pnfHealthCheckTestParams.put("TEST_PNF_CORRELATION_ID", "pnfCorrelationId"); - pnfHealthCheckTestParams.put("TEST_MODEL_UUID", "6bc0b04d-1873-4721-b53d-6615225b2a28"); - pnfHealthCheckTestParams.put("TEST_PNF_UUID", "c93g70d9-8de3-57f1-7de1-f5690ac2b005"); - pnfHealthCheckTestParams.put("TEST_PRC_BLUEPRINT_NAME", "serviceUpgrade"); - pnfHealthCheckTestParams.put("TEST_PRC_BLUEPRINT_VERSION", "1.0.2"); - pnfHealthCheckTestParams.put("TEST_PRC_CUSTOMIZATION_UUID", "PRC_customizationUuid"); - pnfHealthCheckTestParams.put("TEST_RESOURCE_CUSTOMIZATION_UUID_PARAM", "9acb3a83-8a52-412c-9a45-901764938144"); - pnfHealthCheckTestParams.put("TEST_PRC_INSTANCE_NAME", "Demo_pnf"); - pnfHealthCheckTestParams.put("TEST_PRC_CONTROLLER_ACTOR", "cds"); - pnfHealthCheckTestParams.put("TEST_REQUEST_PAYLOAD", "test_payload"); - - for (String param : PNF_HEALTH_CHECK_PARAMS) { - execution.setVariable(param, pnfHealthCheckTestParams.get("TEST_" + param)); - } execution.setVariable(RESOURCE_TYPE, TEST_PNF_SCOPE); execution.setVariable(TEST_PROCESS_KEY, PROCESS_KEY_VALUE); execution.setVariable(BPMN_REQUEST, "bpmnRequestValue"); @@ -95,14 +82,22 @@ public class ServiceLevelPreparationTest { execution.setVariable(PNF_NAME, "PnfDemo"); invalidExecution.setVariables(execution.getVariables()); + + Workflow pnfWorkflow = new Workflow(); + pnfWorkflow.setName(PNF_HEALTH_CHECK_WORKFLOW); + pnfWorkflow.setOperationName(HEALTH_CHECK_OPERATION); + pnfWorkflow.setResourceTarget(TEST_PNF_SCOPE); + workflowList.add(pnfWorkflow); + + when(catalogDbClient.findWorkflowByOperationName(HEALTH_CHECK_OPERATION)).thenReturn(workflowList); } @Test public void executePnfUpgradeSuccessTest() throws Exception { serviceLevelPrepare.execute(execution); // Expect the pnf health check workflow to be set in to execution if validation is successful - assertThat(String.valueOf(execution.getVariable(ServiceLevelConstants.WORKFLOW_TO_INVOKE))) - .isEqualTo("GenericPnfHealthCheck"); + assertThat(String.valueOf(execution.getVariable(ServiceLevelConstants.HEALTH_CHECK_WORKFLOW_TO_INVOKE))) + .isEqualTo("PNFHealthCheck"); } @Test @@ -110,7 +105,8 @@ public class ServiceLevelPreparationTest { invalidExecution.removeVariable(BPMN_REQUEST); // BPMN exception is thrown in case of validation failure or invalid execution thrown.expect(BpmnError.class); - serviceLevelPrepare.validateParamsWithScope(invalidExecution, TEST_PNF_SCOPE, PNF_HEALTH_CHECK_PARAMS); + serviceLevelPrepare.validateParamsWithScope(invalidExecution, TEST_PNF_SCOPE, + HEALTH_CHECK_PARAMS_MAP.get(TEST_PNF_SCOPE)); } @Test @@ -129,6 +125,32 @@ public class ServiceLevelPreparationTest { } + @Test + public void validateDefaultWorkflowIsSetWithoutDBData() throws Exception { + // Mock empty workflow list in db response + when(catalogDbClient.findWorkflowByOperationName(HEALTH_CHECK_OPERATION)).thenReturn(new ArrayList()); + serviceLevelPrepare.execute(execution); + // Expect default workflow gets assigned when workflow name not found in db. + assertThat(String.valueOf(execution.getVariable(ServiceLevelConstants.HEALTH_CHECK_WORKFLOW_TO_INVOKE))) + .isEqualTo(ServiceLevelConstants.DEFAULT_HEALTH_CHECK_WORKFLOWS.get(TEST_PNF_SCOPE)); + } + + @Test + public void validateWorkflowSetFromDb() throws Exception { + Workflow vnfWorkflow = new Workflow(); + vnfWorkflow.setName("VNFHealthCheck"); + vnfWorkflow.setOperationName(HEALTH_CHECK_OPERATION); + vnfWorkflow.setResourceTarget("vnf"); + workflowList.add(vnfWorkflow); + // Mock db response with multiple worklfows mapped with same operation name + when(catalogDbClient.findWorkflowByOperationName(HEALTH_CHECK_OPERATION)).thenReturn(workflowList); + serviceLevelPrepare.execute(execution); + + // Expect right workflow gets assigned from db based on the controller scope. + assertThat(String.valueOf(execution.getVariable(ServiceLevelConstants.HEALTH_CHECK_WORKFLOW_TO_INVOKE))) + .isEqualTo(PNF_HEALTH_CHECK_WORKFLOW); + } + } -- cgit 1.2.3-korg