[SO] Pending Unit test cases for create changes in SO
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / adapter / cnfm / tasks / MonitorCnfmCreateJobTaskTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.adapter.cnfm.tasks;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32 import java.net.URI;
33 import java.util.Optional;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.so.bpmn.common.BuildingBlockExecution;
41 import org.onap.so.client.exception.ExceptionBuilder;
42 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
43
44 /**
45  * @author Raviteja Karumuri (raviteja.karumuri@est.tech)
46  */
47 @RunWith(MockitoJUnitRunner.class)
48 public class MonitorCnfmCreateJobTaskTest {
49
50     private final BuildingBlockExecution stubbedExecution = new StubbedBuildingBlockExecution();
51     public static final String CREATE_CNF_STATUS_RESPONSE_PARAM_NAME = "createCnfStatusResponse";
52     private final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
53     public static final String OPERATION_STATUS_PARAM_NAME = "operationStatus";
54     private MonitorCnfmCreateJobTask monitorCnfmCreateJobTask;
55     @Mock
56     private CnfmHttpServiceProvider mockedCnfmHttpServiceProvider;
57     @Mock
58     private ExceptionBuilder exceptionUtil;
59
60     @Before
61     public void setup() {
62         monitorCnfmCreateJobTask = new MonitorCnfmCreateJobTask(mockedCnfmHttpServiceProvider, exceptionUtil);
63     }
64
65     @Test
66     public void getCurrentOperationStatus_completed() {
67         stubbedExecution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL, URI.create("sampleURL"));
68         when(mockedCnfmHttpServiceProvider.getInstantiateOperationJobStatus(Mockito.anyString()))
69                 .thenReturn(getAsLcmOpOcc());
70         monitorCnfmCreateJobTask.getCurrentOperationStatus(stubbedExecution);
71         assertEquals(AsLcmOpOcc.OperationStateEnum.COMPLETED,
72                 stubbedExecution.getVariable(OPERATION_STATUS_PARAM_NAME));
73     }
74
75     @Test
76     public void test_getCurrentOperationStatus_Exception() {
77         stubbedExecution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL, URI.create("sampleURL"));
78         when(mockedCnfmHttpServiceProvider.getInstantiateOperationJobStatus(Mockito.anyString()))
79                 .thenThrow(new RuntimeException());
80         monitorCnfmCreateJobTask.getCurrentOperationStatus(stubbedExecution);
81         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1209), anyString(),
82                 any());
83     }
84
85     @Test
86     public void test_checkIfOperationWasSuccessful_status_completed() {
87         final MonitorCnfmCreateJobTask mockedMonitorCnfmCreateJobTask = Mockito.spy(monitorCnfmCreateJobTask);
88         mockedMonitorCnfmCreateJobTask.checkIfOperationWasSuccessful(stubbedExecution);
89         verify(mockedMonitorCnfmCreateJobTask, times(1)).checkIfOperationWasSuccessful(stubbedExecution);
90     }
91
92     @Test
93     public void test_checkIfOperationWasSuccessful_status_Failed() {
94         Optional<AsLcmOpOcc> mockedAsLcmOpOcc = getAsLcmOpOcc();
95         mockedAsLcmOpOcc.orElseThrow().setOperationState(AsLcmOpOcc.OperationStateEnum.FAILED);
96         stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.FAILED);
97         stubbedExecution.setVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME, mockedAsLcmOpOcc.orElseThrow());
98         monitorCnfmCreateJobTask.checkIfOperationWasSuccessful(stubbedExecution);
99         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1207), anyString(),
100                 any());
101     }
102
103     @Test
104     public void test_checkIfOperationWasSuccessful_status_Null() {
105         stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, null);
106         monitorCnfmCreateJobTask.checkIfOperationWasSuccessful(stubbedExecution);
107         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1206), anyString(),
108                 any());
109     }
110
111     @Test
112     public void test_hasOperationFinished_status_completed() {
113         stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.COMPLETED);
114         boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution);
115         assertTrue(returnedValue);
116     }
117
118     @Test
119     public void test_hasOperationFinished_status_failed() {
120         stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.FAILED);
121         boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution);
122         assertTrue(returnedValue);
123     }
124
125     @Test
126     public void test_hasOperationFinished_status_rollback() {
127         stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, AsLcmOpOcc.OperationStateEnum.ROLLED_BACK);
128         boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution);
129         assertTrue(returnedValue);
130     }
131
132     @Test
133     public void test_hasOperationFinished_status_null() {
134         stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, null);
135         boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution);
136         assertFalse(returnedValue);
137     }
138
139     @Test
140     public void test_timeOutLogFailure() {
141         monitorCnfmCreateJobTask.timeOutLogFailue(stubbedExecution);
142         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1205), anyString(),
143                 any());
144     }
145
146     private Optional<AsLcmOpOcc> getAsLcmOpOcc() {
147         final AsLcmOpOcc asLcmOpOcc = new AsLcmOpOcc();
148         asLcmOpOcc.setOperationState(AsLcmOpOcc.OperationStateEnum.COMPLETED);
149         return Optional.of(asLcmOpOcc);
150     }
151
152 }