2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nokia
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.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import org.camunda.bpm.engine.delegate.DelegateExecution;
33 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.so.bpmn.infrastructure.service.level.impl.ServiceLevelConstants;
41 import org.onap.so.client.exception.ExceptionBuilder;
42 import org.onap.so.db.catalog.beans.Workflow;
43 import org.onap.so.db.catalog.client.CatalogDbClient;
45 @RunWith(MockitoJUnitRunner.class)
46 public class ServiceLevelTest {
48 private static final String EXECUTION_KEY_PNF_NAME_LIST = "pnfNameList";
49 private static final String EXECUTION_KEY_PNF_COUNTER = "pnfCounter";
50 private static final String PARAM_NAME = "param1";
51 private static final String PNF_NAME = "pnfName1";
52 private static final String OPERATION_NAME = ServiceLevelConstants.HEALTH_CHECK_OPERATION;
53 private static final String SCOPE = ServiceLevelConstants.PNF;
54 private static final String WORKFLOW_NAME = "workflowTestName";
57 private ExceptionBuilder exceptionBuilderMock;
59 private CatalogDbClient catalogDbClientMock;
61 private ServiceLevel testedObject;
63 private DelegateExecution execution;
67 execution = new DelegateExecutionFake();
71 public void fetchWorkflowUsingScope_catalogDBReturnsEmpty() {
73 when(catalogDbClientMock.findWorkflowByOperationName(OPERATION_NAME)).thenReturn(Collections.emptyList());
75 String workflowResult = testedObject.fetchWorkflowUsingScope(SCOPE, OPERATION_NAME);
77 assertThat(workflowResult).isEqualTo("GenericPnfHealthCheck");
81 public void fetchWorkflowUsingScope_catalogDBReturnsNotEmpty() {
83 Workflow workflow = new Workflow();
84 workflow.setResourceTarget(SCOPE);
85 workflow.setName(WORKFLOW_NAME);
86 List<Workflow> workflowList = new ArrayList<>();
87 workflowList.add(workflow);
88 when(catalogDbClientMock.findWorkflowByOperationName(OPERATION_NAME)).thenReturn(workflowList);
90 String workflowResult = testedObject.fetchWorkflowUsingScope(SCOPE, OPERATION_NAME);
92 assertThat(workflowResult).isEqualTo(WORKFLOW_NAME);
96 public void pnfCounterExecution_success() {
98 execution.setVariable(EXECUTION_KEY_PNF_NAME_LIST, createPnfNameList());
99 execution.setVariable(EXECUTION_KEY_PNF_COUNTER, 0);
101 testedObject.pnfCounterExecution(execution);
103 assertThat(execution.getVariable(ServiceLevelConstants.PNF_NAME)).isEqualTo(PNF_NAME);
104 assertThat(execution.getVariable(EXECUTION_KEY_PNF_COUNTER)).isEqualTo(1);
108 public void validateParams_success_paramExistsInExecution() {
110 execution.setVariable(PARAM_NAME, "anyValue");
112 testedObject.validateParamsWithScope(execution, "anyScope", createParamList());
114 verify(exceptionBuilderMock, times(0)).buildAndThrowWorkflowException(any(DelegateExecution.class),
115 eq(ServiceLevelConstants.ERROR_CODE), any(String.class));
119 public void validateParams_exceptionParamDoesNotExistInExecution() {
121 testedObject.validateParamsWithScope(execution, SCOPE, createParamList());
123 verify(exceptionBuilderMock).buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
124 "Validation of health check workflow parameters failed for the scope: " + SCOPE);
127 private List<String> createParamList() {
128 List<String> params = new ArrayList<>();
129 params.add(PARAM_NAME);
133 private List<String> createPnfNameList() {
134 List<String> pnfNameList = new ArrayList<>();
135 pnfNameList.add(PNF_NAME);