[SO] SO changes to support Delete AS
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / adapter / cnfm / tasks / CnfDeleteTaskTest.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
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;
31 import java.net.URI;
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;
47
48 /**
49  * @author raviteja.kaumuri@est.tech
50  */
51 @RunWith(MockitoJUnitRunner.class)
52 public class CnfDeleteTaskTest {
53
54     @Mock
55     private CnfmHttpServiceProvider cnfmHttpServiceProvider;
56     @Mock
57     private ExceptionBuilder exceptionUtil;
58     @Mock
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();
64
65     @Before
66     public void setup() {
67         cnfDeleteTask = new CnfDeleteTask(cnfmHttpServiceProvider, exceptionUtil, extractPojosForBB);
68     }
69
70     @Test
71     public void test_createTerminateAsRequest_success() {
72         cnfDeleteTask.createTerminateAsRequest(stubbedExecution);
73         assertNotNull(stubbedExecution.getVariable(TERMINATE_AS_REQUEST_OBJECT));
74     }
75
76     @Test
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());
85     }
86
87     @Test
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));
94     }
95
96     @Test
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);
103     }
104
105     @Test
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));
111     }
112
113     private GenericVnf getGenericVnf() {
114         GenericVnf genericVnf = new GenericVnf();
115         genericVnf.setVnfId("12345");
116         return genericVnf;
117     }
118
119     private TerminateAsRequest getTerminateAsRequest() {
120         return new TerminateAsRequest();
121     }
122
123     private Optional<URI> getURI() {
124         return Optional.of(URI.create("test_sample"));
125     }
126 }