[SO] SO changes to support Delete AS
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / adapter / cnfm / tasks / CnfInstantiateTaskTest.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 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
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.doThrow;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.INPUT_PARAMETER;
33 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.CLOUD_OWNER_PARAM_KEY;
34 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.CLOUD_REGION_PARAM_KEY;
35 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.TENANT_ID_PARAM_KEY;
36 import java.util.Collections;
37 import java.util.Optional;
38 import java.util.UUID;
39 import org.camunda.bpm.engine.delegate.BpmnError;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.junit.MockitoJUnitRunner;
44 import org.onap.so.bpmn.common.BuildingBlockExecution;
45 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParameter;
46 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
47 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
48 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
49 import org.onap.so.client.exception.ExceptionBuilder;
50 import org.onap.so.cnfm.lcm.model.AsInstance;
51 import org.onap.so.cnfm.lcm.model.CreateAsRequest;
52 import org.onap.so.cnfm.lcm.model.InstantiateAsRequest;
53
54
55 /**
56  * @author raviteja.kaumuri@est.tech
57  */
58 @RunWith(MockitoJUnitRunner.class)
59 public class CnfInstantiateTaskTest {
60
61     @Mock
62     protected ExceptionBuilder exceptionUtil;
63     private static final String CREATE_AS_REQUEST_OBJECT = "CreateAsRequestObject";
64     private static final String INSTANTIATE_AS_REQUEST_OBJECT = "InstantiateAsRequest";
65     private static final String MODEL_INSTANCE_NAME = "instanceTest";
66     private static final String AS_INSTANCE_ID = "asInstanceid";
67     private static final String CLOUD_OWNER = "CloudOwner";
68     private static final String LCP_CLOUD_REGION_ID = "RegionOne";
69     private static final String CNF_ID = UUID.randomUUID().toString();
70     private static final String CNF_NAME = "CNF_NAME";
71     private static final String JOB_ID = UUID.randomUUID().toString();
72
73     @Mock
74     private CnfmHttpServiceProvider mockedCnfmHttpServiceProvider;
75
76     private final BuildingBlockExecution stubbedExecution = new StubbedBuildingBlockExecution();
77
78     @Test
79     public void testCreateCreateASRequest_withValidValues_storesRequestInExecution() throws Exception {
80
81         final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
82         stubbedExecution.setVariable(INPUT_PARAMETER,
83                 new InputParameter(Collections.emptyMap(), Collections.emptyList()));
84
85         objUnderTest.createCreateAsRequest(stubbedExecution);
86
87         final CreateAsRequest actual = stubbedExecution.getVariable(CREATE_AS_REQUEST_OBJECT);
88         assertNotNull(actual);
89         assertEquals(MODEL_INSTANCE_NAME, actual.getAsInstanceName());
90
91         assertEquals(CLOUD_OWNER, actual.getAdditionalParams().get(CLOUD_OWNER_PARAM_KEY).toString());
92         assertEquals(LCP_CLOUD_REGION_ID, actual.getAdditionalParams().get(CLOUD_REGION_PARAM_KEY).toString());
93         assertEquals(StubbedBuildingBlockExecution.getTenantId(),
94                 actual.getAdditionalParams().get(TENANT_ID_PARAM_KEY).toString());
95     }
96
97     @Test
98     public void testCreateCreateASRequest_ForBBThrowsException_exceptionBuilderCalled() throws Exception {
99
100         final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
101
102         final BuildingBlockExecution stubbedExecutionNoReqDetails = new StubbedBuildingBlockExecution();
103         final ExecuteBuildingBlock executeBuildingBlock = stubbedExecutionNoReqDetails.getVariable("buildingBlock");
104         executeBuildingBlock.setRequestDetails(null);
105
106         doThrow(BpmnError.class).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class),
107                 eq(2000), anyString(), any());
108         objUnderTest.createCreateAsRequest(stubbedExecutionNoReqDetails);
109         final CreateAsRequest actual = stubbedExecutionNoReqDetails.getVariable(CREATE_AS_REQUEST_OBJECT);
110
111         assertNull(actual);
112         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(2000), anyString(),
113                 any());
114
115     }
116
117     @Test
118     public void invokeCnfmWithCreateAsRequest_validValues_storesResponseInExecution() throws Exception {
119
120         final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
121         stubbedExecution.setVariable(CREATE_AS_REQUEST_OBJECT, new CreateAsRequest());
122
123         when(mockedCnfmHttpServiceProvider.invokeCreateAsRequest(any(CreateAsRequest.class)))
124                 .thenReturn(getAsInstance());
125
126         objUnderTest.invokeCnfmWithCreateAsRequest(stubbedExecution);
127
128         assertEquals(JOB_ID, stubbedExecution.getVariable(AS_INSTANCE_ID));
129     }
130
131     @Test
132     public void invokeCnfmWithCreateAsRequest_ForBBThrowsException_exceptionBuilderCalled() throws Exception {
133
134         final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
135         stubbedExecution.setVariable(CREATE_AS_REQUEST_OBJECT, new CreateAsRequest());
136
137         when(mockedCnfmHttpServiceProvider.invokeCreateAsRequest(any(CreateAsRequest.class)))
138                 .thenReturn(Optional.empty());
139         doThrow(BpmnError.class).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class),
140                 eq(2003), anyString(), any());
141
142         objUnderTest.invokeCnfmWithCreateAsRequest(stubbedExecution);
143
144         assertNull(stubbedExecution.getVariable(AS_INSTANCE_ID));
145         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(2003), anyString(),
146                 any());
147
148     }
149
150     @Test
151     public void testcreateAsInstanceRequest_withValidValues_storesRequestInExecution() throws Exception {
152
153         final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
154
155         objUnderTest.createAsInstanceRequest(stubbedExecution);
156
157         final InstantiateAsRequest actual = stubbedExecution.getVariable(INSTANTIATE_AS_REQUEST_OBJECT);
158         assertNotNull(actual);
159         assertNotNull(actual.getDeploymentItems());
160     }
161
162     private Optional<AsInstance> getAsInstance() {
163         final AsInstance response = new AsInstance();
164         response.setAsInstanceid(JOB_ID);
165         return Optional.of(response);
166     }
167
168     private GenericVnf getGenericVnf() {
169         final GenericVnf genericVnf = new GenericVnf();
170         genericVnf.setVnfId(CNF_ID);
171         genericVnf.setModelInfoGenericVnf(getModelInfoGenericVnf());
172         genericVnf.setVnfName(CNF_NAME);
173         return genericVnf;
174     }
175
176     private ModelInfoGenericVnf getModelInfoGenericVnf() {
177         final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
178         modelInfoGenericVnf.setModelInstanceName(MODEL_INSTANCE_NAME);
179         return modelInfoGenericVnf;
180     }
181
182     private CnfInstantiateTask getCnfInstantiateTask() {
183         return new CnfInstantiateTask(mockedCnfmHttpServiceProvider, exceptionUtil);
184     }
185 }