5c76018cedc7a409a1cc39a8c92d656fa5804f92
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / EtsiVnfDeleteTaskTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.vnfm.tasks;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 import java.util.UUID;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.onap.so.bpmn.BaseTaskTest;
33 import org.onap.so.bpmn.common.BuildingBlockExecution;
34 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
35 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
36 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
37 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
38 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
39 import org.onap.vnfmadapter.v1.model.DeleteVnfResponse;
40 import com.google.common.base.Optional;
41
42 /**
43  *
44  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
45  *
46  */
47 public class EtsiVnfDeleteTaskTest extends BaseTaskTest {
48
49   private static final String MODEL_INSTANCE_NAME = "MODEL_INSTANCE_NAME";
50
51   private static final String VNF_ID = UUID.randomUUID().toString();
52
53   private static final String VNF_NAME = "VNF_NAME";
54
55   private static final String JOB_ID = UUID.randomUUID().toString();
56
57   @Mock
58   private VnfmAdapterServiceProvider mockedVnfmAdapterServiceProvider;
59
60   @Mock
61   private GeneralBuildingBlock buildingBlock;
62
63   @Mock
64   private RequestContext requestContext;
65
66   private final BuildingBlockExecution stubbedxecution = new StubbedBuildingBlockExecution();
67
68   @Test
69   public void testInvokeVnfmAdapter() throws Exception {
70     final EtsiVnfDeleteTask objUnderTest = getEtsiVnfDeleteTask();
71     when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
72     when(mockedVnfmAdapterServiceProvider.invokeDeleteRequest(eq(VNF_ID))).thenReturn(getDeleteVnfResponse());
73     objUnderTest.invokeVnfmAdapter(stubbedxecution);
74     assertNotNull(stubbedxecution.getVariable(Constants.DELETE_VNF_RESPONSE_PARAM_NAME));
75   }
76
77   @Test
78   public void testInvokeVnfmAdapterException() throws Exception {
79     final EtsiVnfDeleteTask objUnderTest = getEtsiVnfDeleteTask();
80     when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
81     when(mockedVnfmAdapterServiceProvider.invokeDeleteRequest(eq(VNF_ID))).thenReturn(Optional.absent());
82     objUnderTest.invokeVnfmAdapter(stubbedxecution);
83     assertNull(stubbedxecution.getVariable(Constants.DELETE_VNF_RESPONSE_PARAM_NAME));
84     verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1212),
85         any(Exception.class));
86   }
87
88   private Optional<DeleteVnfResponse> getDeleteVnfResponse() {
89     final DeleteVnfResponse response = new DeleteVnfResponse();
90     response.setJobId(JOB_ID);
91     return Optional.of(response);
92   }
93
94   private GenericVnf getGenericVnf() {
95     final GenericVnf genericVnf = new GenericVnf();
96     genericVnf.setVnfId(VNF_ID);
97     genericVnf.setModelInfoGenericVnf(getModelInfoGenericVnf());
98     genericVnf.setVnfName(VNF_NAME);
99     return genericVnf;
100   }
101
102   private ModelInfoGenericVnf getModelInfoGenericVnf() {
103     final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
104     modelInfoGenericVnf.setModelInstanceName(MODEL_INSTANCE_NAME);
105     return modelInfoGenericVnf;
106   }
107
108   private EtsiVnfDeleteTask getEtsiVnfDeleteTask() {
109     return new EtsiVnfDeleteTask(exceptionUtil, extractPojosForBB, mockedVnfmAdapterServiceProvider);
110   }
111 }