add junit coverage
[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 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;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class ServiceLevelTest {
47
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";
55
56     @Mock
57     private ExceptionBuilder exceptionBuilderMock;
58     @Mock
59     private CatalogDbClient catalogDbClientMock;
60     @InjectMocks
61     private ServiceLevel testedObject;
62
63     private DelegateExecution execution;
64
65     @Before
66     public void init() {
67         execution = new DelegateExecutionFake();
68     }
69
70     @Test
71     public void fetchWorkflowUsingScope_catalogDBReturnsEmpty() {
72         // given
73         when(catalogDbClientMock.findWorkflowByOperationName(OPERATION_NAME)).thenReturn(Collections.emptyList());
74         // when
75         String workflowResult = testedObject.fetchWorkflowUsingScope(SCOPE, OPERATION_NAME);
76         // then
77         assertThat(workflowResult).isEqualTo("GenericPnfHealthCheck");
78     }
79
80     @Test
81     public void fetchWorkflowUsingScope_catalogDBReturnsNotEmpty() {
82         // given
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);
89         // when
90         String workflowResult = testedObject.fetchWorkflowUsingScope(SCOPE, OPERATION_NAME);
91         // then
92         assertThat(workflowResult).isEqualTo(WORKFLOW_NAME);
93     }
94
95     @Test
96     public void pnfCounterExecution_success() {
97         // given
98         execution.setVariable(EXECUTION_KEY_PNF_NAME_LIST, createPnfNameList());
99         execution.setVariable(EXECUTION_KEY_PNF_COUNTER, 0);
100         // when
101         testedObject.pnfCounterExecution(execution);
102         // then
103         assertThat(execution.getVariable(ServiceLevelConstants.PNF_NAME)).isEqualTo(PNF_NAME);
104         assertThat(execution.getVariable(EXECUTION_KEY_PNF_COUNTER)).isEqualTo(1);
105     }
106
107     @Test
108     public void validateParams_success_paramExistsInExecution() {
109         // given
110         execution.setVariable(PARAM_NAME, "anyValue");
111         // when
112         testedObject.validateParamsWithScope(execution, "anyScope", createParamList());
113         // then
114         verify(exceptionBuilderMock, times(0)).buildAndThrowWorkflowException(any(DelegateExecution.class),
115                 eq(ServiceLevelConstants.ERROR_CODE), any(String.class));
116     }
117
118     @Test
119     public void validateParams_exceptionParamDoesNotExistInExecution() {
120         // when
121         testedObject.validateParamsWithScope(execution, SCOPE, createParamList());
122         // then
123         verify(exceptionBuilderMock).buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
124                 "Validation of health check workflow parameters failed for the scope: " + SCOPE);
125     }
126
127     private List<String> createParamList() {
128         List<String> params = new ArrayList<>();
129         params.add(PARAM_NAME);
130         return params;
131     }
132
133     private List<String> createPnfNameList() {
134         List<String> pnfNameList = new ArrayList<>();
135         pnfNameList.add(PNF_NAME);
136         return pnfNameList;
137     }
138 }