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