fb15ffa2b36da30e3ec1cae0b43369a83888d4a0
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / service / level / ServiceLevelTest.java
1 /*-
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
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.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 java.util.ArrayList;
29 import java.util.List;
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.onap.so.bpmn.infrastructure.service.level.impl.ServiceLevelConstants;
39 import org.onap.so.client.exception.ExceptionBuilder;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class ServiceLevelTest {
43
44     private static final String EXECUTION_KEY_PNF_NAME_LIST = "pnfNameList";
45     private static final String EXECUTION_KEY_PNF_COUNTER = "pnfCounter";
46     private static final String PARAM_NAME = "param1";
47     private static final String SCOPE = "scope1";
48     private static final String PNF_NAME = "pnfName1";
49
50     @Mock
51     private ExceptionBuilder exceptionBuilderMock;
52     @InjectMocks
53     private ServiceLevel testedObject;
54
55     private DelegateExecution execution;
56
57     @Before
58     public void init() {
59         execution = new DelegateExecutionFake();
60     }
61
62     @Test
63     public void pnfCounterExecution_success() {
64         // given
65         execution.setVariable(EXECUTION_KEY_PNF_NAME_LIST, createPnfNameList());
66         execution.setVariable(EXECUTION_KEY_PNF_COUNTER, 0);
67         // when
68         testedObject.pnfCounterExecution(execution);
69         // then
70         assertThat(execution.getVariable(ServiceLevelConstants.PNF_NAME)).isEqualTo(PNF_NAME);
71         assertThat(execution.getVariable(EXECUTION_KEY_PNF_COUNTER)).isEqualTo(1);
72     }
73
74     @Test
75     public void validateParams_success_paramExistsInExecution() {
76         // given
77         execution.setVariable(PARAM_NAME, "anyValue");
78         // when
79         testedObject.validateParamsWithScope(execution, "anyScope", createParamList());
80         // then
81         verify(exceptionBuilderMock, times(0)).buildAndThrowWorkflowException(any(DelegateExecution.class),
82                 eq(ServiceLevelConstants.ERROR_CODE), any(String.class));
83     }
84
85     @Test
86     public void validateParams_exceptionParamDoesNotExistInExecution() {
87         // when
88         testedObject.validateParamsWithScope(execution, SCOPE, createParamList());
89         // then
90         verify(exceptionBuilderMock).buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
91                 "Validation of health check workflow parameters failed for the scope: " + SCOPE);
92     }
93
94     private List<String> createParamList() {
95         List<String> params = new ArrayList<>();
96         params.add(PARAM_NAME);
97         return params;
98     }
99
100     private List<String> createPnfNameList() {
101         List<String> pnfNameList = new ArrayList<>();
102         pnfNameList.add(PNF_NAME);
103         return pnfNameList;
104     }
105 }