Picking workflow names dynamically for schema update procedure 11/111411/3
authorrameshiyer27 <ramesh.murugan.iyer@est.tech>
Wed, 19 Aug 2020 09:22:20 +0000 (10:22 +0100)
committerrameshiyer27 <ramesh.murugan.iyer@est.tech>
Thu, 20 Aug 2020 16:23:34 +0000 (17:23 +0100)
Issue-ID: SO-3147
Signed-off-by: zrrmmua <ramesh.murugan.iyer@est.tech>
Change-Id: I4c9de93ea7b51657c2bc6ee24b4bb52e2068cf5a

bpmn/so-bpmn-infrastructure-flows/src/test/java/org/onap/so/bpmn/infrastructure/process/ServiceLevelUpgradeTest.java
bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparable.java [moved from bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/AbstractServiceLevelPreparable.java with 64% similarity]
bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelConstants.java
bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelPreparation.java
bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/service/level/impl/ServiceLevelUpgrade.java
bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/service/level/ServiceLevelPreparationTest.java
mso-catalog-db/src/main/java/org/onap/so/db/catalog/client/CatalogDbClient.java
mso-catalog-db/src/main/java/org/onap/so/db/catalog/data/repository/WorkflowRepository.java
mso-catalog-db/src/test/java/org/onap/so/db/catalog/client/CatalogDbClientTest.java

index 805597c..00dda29 100644 (file)
 
 package org.onap.so.bpmn.infrastructure.process;
 
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
+import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
+import static com.github.tomakehurst.wiremock.client.WireMock.post;
+import static com.github.tomakehurst.wiremock.client.WireMock.put;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static org.assertj.core.api.Assertions.fail;
+import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareAssertions.assertThat;
 import com.google.protobuf.Struct;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
 import org.camunda.bpm.engine.runtime.ProcessInstance;
 import org.junit.Before;
 import org.junit.Test;
@@ -35,14 +47,6 @@ import org.onap.so.bpmn.mock.FileUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
-import static com.github.tomakehurst.wiremock.client.WireMock.*;
-import static org.assertj.core.api.Assertions.fail;
-import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareAssertions.assertThat;
 
 /**
  * Basic Integration test for ServiceLevelUpgrade.bpmn workflow.
 
 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;
-import java.util.ArrayList;
-import java.util.List;
 
 /**
- * Abstract class for Service level upgrade Execution, it should be extended for service level upgrade tasks.
+ * Parent class for Service level upgrade Execution, it should be extended for service level upgrade tasks.
  */
-public abstract class AbstractServiceLevelPreparable {
+public class ServiceLevelPreparable {
 
-    protected static final Logger LOG = LoggerFactory.getLogger(AbstractServiceLevelPreparable.class);
+    protected static final Logger LOG = LoggerFactory.getLogger(ServiceLevelPreparable.class);
 
     @Autowired
     protected ExceptionBuilder exceptionBuilder;
 
+    @Autowired
+    protected CatalogDbClient catalogDbClient;
+
     /**
-     * This method fetches workflow names to be invoked based on the controller scope .
+     * 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 abstract String fetchWorkflowUsingScope(DelegateExecution execution, final String scope);
+    protected String fetchWorkflowUsingScope(final String scope, String operationName) {
+        Optional<String> wflName = Optional.empty();
+        try {
+            List<Workflow> 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.
index 34ebb30..d946418 100644 (file)
@@ -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<String, String> DEFAULT_HEALTH_CHECK_WORKFLOWS =
+            Map.of(PNF, "GenericPnfHealthCheck", VNF, "GenericVNFHealthCheck");
+
+    public static final Map<String, String> DEFAULT_SOFTWARE_UP_WORKFLOWS =
+            Map.of(PNF, "PNFSoftwareUpgrade", VNF, "GenericVnfSoftwareUpgrade");
+
+    // Maps operation name with workflows
+    public static final Map<String, Map<String, String>> WORKFLOW_OPERATIONS_MAP = Map.of(HEALTH_CHECK_OPERATION,
+            DEFAULT_HEALTH_CHECK_WORKFLOWS, SW_UP_OPERATION, DEFAULT_SOFTWARE_UP_WORKFLOWS);
+
+    public static final List<String> VALID_CONTROLLER_SCOPE = Arrays.asList(PNF, VNF);
+
+
 
 }
index 5b77276..59884ec 100644 (file)
 
 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<String> PNF_HC_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID,
+    private static final List<String> 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<String> VNF_HEALTH_CHECK_PARAMS = Collections.emptyList();
+
+    private static final Map<String, List<String>> 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;
     }
 
 }
index dfc77d6..9d7d8ef 100644 (file)
@@ -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<String> PNF_SWU_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID,
+    private static final List<String> 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<String> VNF_SOFTWARE_UP_PARAMS = Collections.emptyList();
+
+    private static final Map<String, List<String>> 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");
         }
     }
 }
index 340b277..0b1c5b0 100644 (file)
 
 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<String> 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<String, List<String>> 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<Workflow> workflowList = new ArrayList<>();
 
-    private Map<String, String> 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<Workflow>());
+        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);
+    }
+
 }
 
 
index 8d342b6..ef90dd6 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.onap.so.db.catalog.client;
 
+import com.google.common.base.Strings;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -83,7 +84,6 @@ import org.springframework.stereotype.Component;
 import org.springframework.web.client.HttpClientErrorException;
 import org.springframework.web.client.RestTemplate;
 import org.springframework.web.util.UriComponentsBuilder;
-import com.google.common.base.Strings;
 import uk.co.blackpepper.bowman.Client;
 import uk.co.blackpepper.bowman.ClientFactory;
 import uk.co.blackpepper.bowman.Configuration;
@@ -162,6 +162,7 @@ public class CatalogDbClient {
     protected static final String SOURCE = "source";
     protected static final String RESOURCE_TARGET = "resourceTarget";
     protected static final String FLAG = "flag";
+    protected static final String OPERATION_NAME = "operationName";
 
     private static final String TARGET_ENTITY = "SO:CatalogDB";
     private static final String ASTERISK = "*";
@@ -215,6 +216,7 @@ public class CatalogDbClient {
             "/findBBNameSelectionReferenceByControllerActorAndScopeAndAction";
     private String findWorkflowByResourceTarget = "/findByResourceTarget";
     private String findProcessingFlagsByFlag = "/findByFlag";
+    private String findWorkflowByOperationName = "/findByOperationName";
 
     private String serviceURI;
     private String vfModuleURI;
@@ -365,6 +367,7 @@ public class CatalogDbClient {
                 + findBBNameSelectionReferenceByControllerActorAndScopeAndAction;
 
         findProcessingFlagsByFlag = endpoint + PROCESSING_FLAGS + SEARCH + findProcessingFlagsByFlag;
+        findWorkflowByOperationName = endpoint + WORKFLOW + SEARCH + findWorkflowByOperationName;
 
         serviceURI = endpoint + SERVICE + URI_SEPARATOR;
         vfModuleURI = endpoint + VFMODULE + URI_SEPARATOR;
@@ -1106,6 +1109,11 @@ public class CatalogDbClient {
                 .queryParam(RESOURCE_TARGET, resourceTarget).build().toString()));
     }
 
+    public List<Workflow> findWorkflowByOperationName(String operationName) {
+        return this.getMultipleResources(workflowClient, getUri(UriBuilder.fromUri(findWorkflowByOperationName)
+                .queryParam(OPERATION_NAME, operationName).build().toString()));
+    }
+
     public ProcessingFlags findProcessingFlagsByFlag(String flag) {
         return this.getSingleResource(processingFlagsClient,
                 getUri(UriBuilder.fromUri(findProcessingFlagsByFlag).queryParam(FLAG, flag).build().toString()));
index 93ec54a..74843e5 100644 (file)
@@ -35,6 +35,8 @@ public interface WorkflowRepository extends JpaRepository<Workflow, Integer> {
 
     List<Workflow> findByResourceTarget(String resourceTarget);
 
+    List<Workflow> findByOperationName(String operationName);
+
     /**
      * Used to fetch the @{link Workflow} by the Model UUID.
      *
index 79e3cbc..6a5daa6 100644 (file)
@@ -204,4 +204,15 @@ public class CatalogDbClientTest {
                 .queryParam(CatalogDbClient.RESOURCE_TARGET, pnf_resource).build()));
     }
 
+    @Test
+    public final void testFindWorkFlowByOperationName() {
+        final String operationName = "PNFSoftwareUpgrade";
+        doReturn(new ArrayList()).when(catalogDbClient).getMultipleResources(any(), any());
+        catalogDbClient.findWorkflowByOperationName(operationName);
+
+        // verify
+        verify(catalogDbClient).getMultipleResources(any(Client.class), eq(UriBuilder.fromUri("/findByOperationName")
+                .queryParam(CatalogDbClient.OPERATION_NAME, operationName).build()));
+    }
+
 }