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
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.adapter.cnfm.tasks;
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;
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;
44 * @author Raviteja Karumuri (raviteja.karumuri@est.tech)
46 @RunWith(MockitoJUnitRunner.class)
47 public class MonitorCnfmJobTaskTest {
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;
55 private CnfmHttpServiceProvider mockedCnfmHttpServiceProvider;
57 private ExceptionBuilder exceptionUtil;
61 monitorCnfmCreateJobTask = new MonitorCnfmJobTask(mockedCnfmHttpServiceProvider, exceptionUtil);
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));
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));
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);
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));
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);
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);
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);
124 public void test_hasOperationFinished_status_null() {
125 stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, null);
126 boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution);
127 assertFalse(returnedValue);
131 public void test_timeOutLogFailure() {
132 monitorCnfmCreateJobTask.timeOutLogFailure(stubbedExecution);
133 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1205),
134 any(Exception.class));
137 private Optional<AsLcmOpOcc> getAsLcmOpOcc() {
138 final AsLcmOpOcc asLcmOpOcc = new AsLcmOpOcc();
139 asLcmOpOcc.setOperationState(AsLcmOpOcc.OperationStateEnum.COMPLETED);
140 return Optional.of(asLcmOpOcc);