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;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
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 javax.swing.text.html.Option;
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.bpmn.servicedecomposition.bbobjects.GenericVnf;
42 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
43 import org.onap.so.client.exception.BBObjectNotFoundException;
44 import org.onap.so.client.exception.ExceptionBuilder;
45 import org.onap.so.cnfm.lcm.model.TerminateAsRequest;
46 import org.reactivestreams.Publisher;
49 * @author raviteja.kaumuri@est.tech
51 @RunWith(MockitoJUnitRunner.class)
52 public class CnfDeleteTaskTest {
55 private CnfmHttpServiceProvider cnfmHttpServiceProvider;
57 private ExceptionBuilder exceptionUtil;
59 private ExtractPojosForBB extractPojosForBB;
60 private CnfDeleteTask cnfDeleteTask;
61 private static final String TERMINATE_AS_REQUEST_OBJECT = "TerminateAsRequest";
62 private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
63 private final BuildingBlockExecution stubbedExecution = new StubbedBuildingBlockExecution();
67 cnfDeleteTask = new CnfDeleteTask(cnfmHttpServiceProvider, exceptionUtil, extractPojosForBB);
71 public void test_createTerminateAsRequest_success() {
72 cnfDeleteTask.createTerminateAsRequest(stubbedExecution);
73 assertNotNull(stubbedExecution.getVariable(TERMINATE_AS_REQUEST_OBJECT));
77 public void test_invokeCnfmToTerminateAsInstance_success() throws BBObjectNotFoundException {
78 stubbedExecution.setVariable(TERMINATE_AS_REQUEST_OBJECT, getTerminateAsRequest());
79 when(extractPojosForBB.extractByKey(Mockito.any(), Mockito.any())).thenReturn(getGenericVnf());
80 when(cnfmHttpServiceProvider.invokeTerminateAsRequest(Mockito.anyString(),
81 Mockito.any(TerminateAsRequest.class))).thenReturn(getURI());
82 cnfDeleteTask.invokeCnfmToTerminateAsInstance(stubbedExecution);
83 URI returnedContent = stubbedExecution.getVariable(CNFM_REQUEST_STATUS_CHECK_URL);
84 assertEquals(getURI().orElseThrow().getPath(), returnedContent.getPath());
88 public void test_invokeCnfmToTerminateAsInstance_Exception() throws BBObjectNotFoundException {
89 stubbedExecution.setVariable(TERMINATE_AS_REQUEST_OBJECT, getTerminateAsRequest());
90 when(extractPojosForBB.extractByKey(Mockito.any(), Mockito.any())).thenThrow(new RuntimeException());
91 cnfDeleteTask.invokeCnfmToTerminateAsInstance(stubbedExecution);
92 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(2002),
93 any(Exception.class));
97 public void test_invokeCnfmToDeleteAsInstance_success() throws BBObjectNotFoundException {
98 when(extractPojosForBB.extractByKey(Mockito.any(), Mockito.any())).thenReturn(getGenericVnf());
99 when(cnfmHttpServiceProvider.invokeDeleteAsRequest(Mockito.anyString())).thenReturn(Optional.of(Boolean.TRUE));
100 CnfDeleteTask mockCnfDeleteTask = Mockito.spy(cnfDeleteTask);
101 mockCnfDeleteTask.invokeCnfmToDeleteAsInstance(stubbedExecution);
102 verify(mockCnfDeleteTask, times(1)).invokeCnfmToDeleteAsInstance(stubbedExecution);
106 public void test_invokeCnfmToDeleteAsInstance_Exception() throws BBObjectNotFoundException {
107 when(extractPojosForBB.extractByKey(Mockito.any(), Mockito.any())).thenThrow(new RuntimeException());
108 cnfDeleteTask.invokeCnfmToDeleteAsInstance(stubbedExecution);
109 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(2003),
110 any(Exception.class));
113 private GenericVnf getGenericVnf() {
114 GenericVnf genericVnf = new GenericVnf();
115 genericVnf.setVnfId("12345");
119 private TerminateAsRequest getTerminateAsRequest() {
120 return new TerminateAsRequest();
123 private Optional<URI> getURI() {
124 return Optional.of(URI.create("test_sample"));