0b1c5b0f5e888c81c1cdf7412ea63f9a9d6e1822
[so.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.service.level;
22
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;
29 import java.util.Map;
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;
44
45
46 public class ServiceLevelPreparationTest extends BaseTaskTest {
47
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());
59
60     private List<Workflow> workflowList = new ArrayList<>();
61
62     @Rule
63     public ExpectedException thrown = ExpectedException.none();
64
65     @InjectMocks
66     private ServiceLevelPreparation serviceLevelPrepare;
67
68     @InjectMocks
69     @Spy
70     private ExceptionBuilder exceptionBuilder;
71
72     private DelegateExecution execution = new DelegateExecutionFake();
73     private DelegateExecution invalidExecution = new DelegateExecutionFake();
74
75
76     @Before
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");
83
84         invalidExecution.setVariables(execution.getVariables());
85
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);
91
92         when(catalogDbClient.findWorkflowByOperationName(HEALTH_CHECK_OPERATION)).thenReturn(workflowList);
93     }
94
95     @Test
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");
101     }
102
103     @Test
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));
110     }
111
112     @Test
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);
118     }
119
120     @Test
121     public void invokeServiceLevelPrepareWithoutScope() throws Exception {
122         invalidExecution.removeVariable(RESOURCE_TYPE);
123         thrown.expect(BpmnError.class);
124         serviceLevelPrepare.execute(invalidExecution);
125
126     }
127
128     @Test
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));
136     }
137
138     @Test
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);
148
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);
152     }
153
154 }
155
156