2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.service.level;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.when;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.List;
30 import org.camunda.bpm.engine.delegate.BpmnError;
31 import org.camunda.bpm.engine.delegate.DelegateExecution;
32 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.mockito.InjectMocks;
38 import org.mockito.Spy;
39 import org.onap.so.bpmn.BaseTaskTest;
40 import org.onap.so.bpmn.infrastructure.service.level.impl.ServiceLevelConstants;
41 import org.onap.so.bpmn.infrastructure.service.level.impl.ServiceLevelPreparation;
42 import org.onap.so.client.exception.ExceptionBuilder;
43 import org.onap.so.db.catalog.beans.Workflow;
46 public class ServiceLevelPreparationTest extends BaseTaskTest {
48 private static final String TEST_PNF_SCOPE = "pnf";
49 private static final String TEST_PROCESS_KEY = "testProcessKey";
50 private static final String PROCESS_KEY_VALUE = "testProcessKeyValue";
51 private static final String BPMN_REQUEST = "bpmnRequest";
52 private static final String RESOURCE_TYPE = "resourceType";
53 private static final String SERVICE_INSTANCE_ID = "serviceInstanceId";
54 private static final String PNF_NAME = "pnfName";
55 private static final String HEALTH_CHECK_OPERATION = "ResourceHealthCheck";
56 private static final String PNF_HEALTH_CHECK_WORKFLOW = "PNFHealthCheck";
57 private static final Map<String, List<String>> HEALTH_CHECK_PARAMS_MAP = Map.of(TEST_PNF_SCOPE,
58 Arrays.asList(SERVICE_INSTANCE_ID, RESOURCE_TYPE, BPMN_REQUEST, PNF_NAME), "vnf", Collections.emptyList());
60 private List<Workflow> workflowList = new ArrayList<>();
63 public ExpectedException thrown = ExpectedException.none();
66 private ServiceLevelPreparation serviceLevelPrepare;
70 private ExceptionBuilder exceptionBuilder;
72 private DelegateExecution execution = new DelegateExecutionFake();
73 private DelegateExecution invalidExecution = new DelegateExecutionFake();
77 public void setUpPnfUpgradeTest() {
78 execution.setVariable(RESOURCE_TYPE, TEST_PNF_SCOPE);
79 execution.setVariable(TEST_PROCESS_KEY, PROCESS_KEY_VALUE);
80 execution.setVariable(BPMN_REQUEST, "bpmnRequestValue");
81 execution.setVariable(SERVICE_INSTANCE_ID, "serviceInstanceIdValue");
82 execution.setVariable(PNF_NAME, "PnfDemo");
84 invalidExecution.setVariables(execution.getVariables());
86 Workflow pnfWorkflow = new Workflow();
87 pnfWorkflow.setName(PNF_HEALTH_CHECK_WORKFLOW);
88 pnfWorkflow.setOperationName(HEALTH_CHECK_OPERATION);
89 pnfWorkflow.setResourceTarget(TEST_PNF_SCOPE);
90 workflowList.add(pnfWorkflow);
92 when(catalogDbClient.findWorkflowByOperationName(HEALTH_CHECK_OPERATION)).thenReturn(workflowList);
96 public void executePnfUpgradeSuccessTest() throws Exception {
97 serviceLevelPrepare.execute(execution);
98 // Expect the pnf health check workflow to be set in to execution if validation is successful
99 assertThat(String.valueOf(execution.getVariable(ServiceLevelConstants.HEALTH_CHECK_WORKFLOW_TO_INVOKE)))
100 .isEqualTo("PNFHealthCheck");
104 public void validateFailureParamsForPnfTest() throws Exception {
105 invalidExecution.removeVariable(BPMN_REQUEST);
106 // BPMN exception is thrown in case of validation failure or invalid execution
107 thrown.expect(BpmnError.class);
108 serviceLevelPrepare.validateParamsWithScope(invalidExecution, TEST_PNF_SCOPE,
109 HEALTH_CHECK_PARAMS_MAP.get(TEST_PNF_SCOPE));
113 public void invalidScopeExecuteTest() throws Exception {
114 invalidExecution.setVariable(RESOURCE_TYPE, "InvalidResource");
115 // BPMN workflow exception is expected incase of invalid resource type other than pnf/vnf
116 thrown.expect(BpmnError.class);
117 serviceLevelPrepare.execute(invalidExecution);
121 public void invokeServiceLevelPrepareWithoutScope() throws Exception {
122 invalidExecution.removeVariable(RESOURCE_TYPE);
123 thrown.expect(BpmnError.class);
124 serviceLevelPrepare.execute(invalidExecution);
129 public void validateDefaultWorkflowIsSetWithoutDBData() throws Exception {
130 // Mock empty workflow list in db response
131 when(catalogDbClient.findWorkflowByOperationName(HEALTH_CHECK_OPERATION)).thenReturn(new ArrayList<Workflow>());
132 serviceLevelPrepare.execute(execution);
133 // Expect default workflow gets assigned when workflow name not found in db.
134 assertThat(String.valueOf(execution.getVariable(ServiceLevelConstants.HEALTH_CHECK_WORKFLOW_TO_INVOKE)))
135 .isEqualTo(ServiceLevelConstants.DEFAULT_HEALTH_CHECK_WORKFLOWS.get(TEST_PNF_SCOPE));
139 public void validateWorkflowSetFromDb() throws Exception {
140 Workflow vnfWorkflow = new Workflow();
141 vnfWorkflow.setName("VNFHealthCheck");
142 vnfWorkflow.setOperationName(HEALTH_CHECK_OPERATION);
143 vnfWorkflow.setResourceTarget("vnf");
144 workflowList.add(vnfWorkflow);
145 // Mock db response with multiple worklfows mapped with same operation name
146 when(catalogDbClient.findWorkflowByOperationName(HEALTH_CHECK_OPERATION)).thenReturn(workflowList);
147 serviceLevelPrepare.execute(execution);
149 // Expect right workflow gets assigned from db based on the controller scope.
150 assertThat(String.valueOf(execution.getVariable(ServiceLevelConstants.HEALTH_CHECK_WORKFLOW_TO_INVOKE)))
151 .isEqualTo(PNF_HEALTH_CHECK_WORKFLOW);