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;
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;
55 * @author raviteja.kaumuri@est.tech
57 public class CnfInstantiateTaskTest extends BaseTaskTest {
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();
72 private CnfmHttpServiceProvider mockedCnfmHttpServiceProvider;
74 private final BuildingBlockExecution stubbedExecution = new StubbedBuildingBlockExecution();
77 public void testCreateCreateASRequest_withValidValues_storesRequestInExecution() throws Exception {
79 final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
80 stubbedExecution.setVariable(INPUT_PARAMETER,
81 new InputParameter(Collections.emptyMap(), Collections.emptyList()));
83 objUnderTest.createCreateAsRequest(stubbedExecution);
85 final CreateAsRequest actual = stubbedExecution.getVariable(CREATE_AS_REQUEST_OBJECT);
86 assertNotNull(actual);
87 assertEquals(MODEL_INSTANCE_NAME, actual.getAsInstanceName());
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());
96 public void testCreateCreateASRequest_ForBBThrowsException_exceptionBuilderCalled() throws Exception {
98 final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
100 final BuildingBlockExecution stubbedExecutionNoReqDetails = new StubbedBuildingBlockExecution();
101 final ExecuteBuildingBlock executeBuildingBlock = stubbedExecutionNoReqDetails.getVariable("buildingBlock");
102 executeBuildingBlock.setRequestDetails(null);
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);
110 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(2000), anyString(),
116 public void invokeCnfmWithCreateAsRequest_validValues_storesResponseInExecution() throws Exception {
118 final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
119 stubbedExecution.setVariable(CREATE_AS_REQUEST_OBJECT, new CreateAsRequest());
121 when(mockedCnfmHttpServiceProvider.invokeCreateAsRequest(any(CreateAsRequest.class)))
122 .thenReturn(getAsInstance());
124 objUnderTest.invokeCnfmWithCreateAsRequest(stubbedExecution);
126 assertEquals(JOB_ID, stubbedExecution.getVariable(AS_INSTANCE_ID));
130 public void invokeCnfmWithCreateAsRequest_ForBBThrowsException_exceptionBuilderCalled() throws Exception {
132 final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
133 stubbedExecution.setVariable(CREATE_AS_REQUEST_OBJECT, new CreateAsRequest());
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());
140 objUnderTest.invokeCnfmWithCreateAsRequest(stubbedExecution);
142 assertNull(stubbedExecution.getVariable(AS_INSTANCE_ID));
143 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(2003), anyString(),
149 public void testcreateAsInstanceRequest_withValidValues_storesRequestInExecution() throws Exception {
151 final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
153 objUnderTest.createAsInstanceRequest(stubbedExecution);
155 final InstantiateAsRequest actual = stubbedExecution.getVariable(INSTANTIATE_AS_REQUEST_OBJECT);
156 assertNotNull(actual);
157 assertNotNull(actual.getDeploymentItems());
160 private Optional<AsInstance> getAsInstance() {
161 final AsInstance response = new AsInstance();
162 response.setAsInstanceid(JOB_ID);
163 return Optional.of(response);
166 private GenericVnf getGenericVnf() {
167 final GenericVnf genericVnf = new GenericVnf();
168 genericVnf.setVnfId(CNF_ID);
169 genericVnf.setModelInfoGenericVnf(getModelInfoGenericVnf());
170 genericVnf.setVnfName(CNF_NAME);
174 private ModelInfoGenericVnf getModelInfoGenericVnf() {
175 final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
176 modelInfoGenericVnf.setModelInstanceName(MODEL_INSTANCE_NAME);
177 return modelInfoGenericVnf;
180 private CnfInstantiateTask getCnfInstantiateTask() {
181 return new CnfInstantiateTask(mockedCnfmHttpServiceProvider, exceptionUtil);