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