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;
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;
56 * @author raviteja.kaumuri@est.tech
58 @RunWith(MockitoJUnitRunner.class)
59 public class CnfInstantiateTaskTest {
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();
74 private CnfmHttpServiceProvider mockedCnfmHttpServiceProvider;
76 private final BuildingBlockExecution stubbedExecution = new StubbedBuildingBlockExecution();
79 public void testCreateCreateASRequest_withValidValues_storesRequestInExecution() throws Exception {
81 final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
82 stubbedExecution.setVariable(INPUT_PARAMETER,
83 new InputParameter(Collections.emptyMap(), Collections.emptyList()));
85 objUnderTest.createCreateAsRequest(stubbedExecution);
87 final CreateAsRequest actual = stubbedExecution.getVariable(CREATE_AS_REQUEST_OBJECT);
88 assertNotNull(actual);
89 assertEquals(MODEL_INSTANCE_NAME, actual.getAsInstanceName());
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());
98 public void testCreateCreateASRequest_ForBBThrowsException_exceptionBuilderCalled() throws Exception {
100 final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
102 final BuildingBlockExecution stubbedExecutionNoReqDetails = new StubbedBuildingBlockExecution();
103 final ExecuteBuildingBlock executeBuildingBlock = stubbedExecutionNoReqDetails.getVariable("buildingBlock");
104 executeBuildingBlock.setRequestDetails(null);
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);
112 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(2000), anyString(),
118 public void invokeCnfmWithCreateAsRequest_validValues_storesResponseInExecution() throws Exception {
120 final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
121 stubbedExecution.setVariable(CREATE_AS_REQUEST_OBJECT, new CreateAsRequest());
123 when(mockedCnfmHttpServiceProvider.invokeCreateAsRequest(any(CreateAsRequest.class)))
124 .thenReturn(getAsInstance());
126 objUnderTest.invokeCnfmWithCreateAsRequest(stubbedExecution);
128 assertEquals(JOB_ID, stubbedExecution.getVariable(AS_INSTANCE_ID));
132 public void invokeCnfmWithCreateAsRequest_ForBBThrowsException_exceptionBuilderCalled() throws Exception {
134 final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
135 stubbedExecution.setVariable(CREATE_AS_REQUEST_OBJECT, new CreateAsRequest());
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());
142 objUnderTest.invokeCnfmWithCreateAsRequest(stubbedExecution);
144 assertNull(stubbedExecution.getVariable(AS_INSTANCE_ID));
145 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(2003), anyString(),
151 public void testcreateAsInstanceRequest_withValidValues_storesRequestInExecution() throws Exception {
153 final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
155 objUnderTest.createAsInstanceRequest(stubbedExecution);
157 final InstantiateAsRequest actual = stubbedExecution.getVariable(INSTANTIATE_AS_REQUEST_OBJECT);
158 assertNotNull(actual);
159 assertNotNull(actual.getDeploymentItems());
162 private Optional<AsInstance> getAsInstance() {
163 final AsInstance response = new AsInstance();
164 response.setAsInstanceid(JOB_ID);
165 return Optional.of(response);
168 private GenericVnf getGenericVnf() {
169 final GenericVnf genericVnf = new GenericVnf();
170 genericVnf.setVnfId(CNF_ID);
171 genericVnf.setModelInfoGenericVnf(getModelInfoGenericVnf());
172 genericVnf.setVnfName(CNF_NAME);
176 private ModelInfoGenericVnf getModelInfoGenericVnf() {
177 final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
178 modelInfoGenericVnf.setModelInstanceName(MODEL_INSTANCE_NAME);
179 return modelInfoGenericVnf;
182 private CnfInstantiateTask getCnfInstantiateTask() {
183 return new CnfInstantiateTask(mockedCnfmHttpServiceProvider, exceptionUtil);