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.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;
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;
45 * @author Raviteja Karumuri (raviteja.karumuri@est.tech)
47 @RunWith(MockitoJUnitRunner.class)
48 public class MonitorCnfmCreateJobTaskTest {
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;
56 private CnfmHttpServiceProvider mockedCnfmHttpServiceProvider;
58 private ExceptionBuilder exceptionUtil;
62 monitorCnfmCreateJobTask = new MonitorCnfmCreateJobTask(mockedCnfmHttpServiceProvider, exceptionUtil);
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));
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(),
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);
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(),
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(),
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);
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);
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);
133 public void test_hasOperationFinished_status_null() {
134 stubbedExecution.setVariable(OPERATION_STATUS_PARAM_NAME, null);
135 boolean returnedValue = monitorCnfmCreateJobTask.hasOperationFinished(stubbedExecution);
136 assertFalse(returnedValue);
140 public void test_timeOutLogFailure() {
141 monitorCnfmCreateJobTask.timeOutLogFailue(stubbedExecution);
142 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1205), anyString(),
146 private Optional<AsLcmOpOcc> getAsLcmOpOcc() {
147 final AsLcmOpOcc asLcmOpOcc = new AsLcmOpOcc();
148 asLcmOpOcc.setOperationState(AsLcmOpOcc.OperationStateEnum.COMPLETED);
149 return Optional.of(asLcmOpOcc);