Adding namespace support
[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.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.doThrow;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.INPUT_PARAMETER;
34 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.CLOUD_OWNER_PARAM_KEY;
35 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.CLOUD_REGION_PARAM_KEY;
36 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.NAMESPACE_KEY;
37 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.TENANT_ID_PARAM_KEY;
38 import java.util.Collections;
39 import java.util.Optional;
40 import java.util.UUID;
41 import org.camunda.bpm.engine.delegate.BpmnError;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.junit.MockitoJUnitRunner;
46 import org.onap.so.bpmn.common.BuildingBlockExecution;
47 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParameter;
48 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
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 NAME_SPACE = "default";
70     private static final String JOB_ID = UUID.randomUUID().toString();
71
72     @Mock
73     private CnfmHttpServiceProvider mockedCnfmHttpServiceProvider;
74
75     private final BuildingBlockExecution stubbedExecution = new StubbedBuildingBlockExecution();
76
77     @Test
78     public void testCreateCreateASRequest_withValidValues_storesRequestInExecution() throws Exception {
79
80         final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
81         stubbedExecution.setVariable(INPUT_PARAMETER,
82                 new InputParameter(Collections.emptyMap(), Collections.emptyList()));
83
84         objUnderTest.createCreateAsRequest(stubbedExecution);
85
86         final CreateAsRequest actual = stubbedExecution.getVariable(CREATE_AS_REQUEST_OBJECT);
87         assertNotNull(actual);
88         assertEquals(MODEL_INSTANCE_NAME, actual.getAsInstanceName());
89
90         assertEquals(CLOUD_OWNER, actual.getAdditionalParams().get(CLOUD_OWNER_PARAM_KEY).toString());
91         assertEquals(LCP_CLOUD_REGION_ID, actual.getAdditionalParams().get(CLOUD_REGION_PARAM_KEY).toString());
92         assertEquals(StubbedBuildingBlockExecution.getTenantId(),
93                 actual.getAdditionalParams().get(TENANT_ID_PARAM_KEY).toString());
94         assertEquals(NAME_SPACE, actual.getAdditionalParams().get(NAMESPACE_KEY).toString());
95
96     }
97
98     @Test
99     public void testCreateCreateASRequest_ForBBThrowsException_exceptionBuilderCalled() throws Exception {
100
101         final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
102
103         final BuildingBlockExecution stubbedExecutionNoReqDetails = new StubbedBuildingBlockExecution();
104         final ExecuteBuildingBlock executeBuildingBlock = stubbedExecutionNoReqDetails.getVariable("buildingBlock");
105         executeBuildingBlock.setRequestDetails(null);
106
107         doThrow(BpmnError.class).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class),
108                 eq(2000), anyString(), any());
109         objUnderTest.createCreateAsRequest(stubbedExecutionNoReqDetails);
110         final CreateAsRequest actual = stubbedExecutionNoReqDetails.getVariable(CREATE_AS_REQUEST_OBJECT);
111
112         assertNull(actual);
113         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(2000), anyString(),
114                 any());
115
116     }
117
118     @Test
119     public void invokeCnfmWithCreateAsRequest_validValues_storesResponseInExecution() throws Exception {
120
121         final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
122         stubbedExecution.setVariable(CREATE_AS_REQUEST_OBJECT, new CreateAsRequest());
123
124         when(mockedCnfmHttpServiceProvider.invokeCreateAsRequest(any(CreateAsRequest.class)))
125                 .thenReturn(getAsInstance());
126
127         objUnderTest.invokeCnfmWithCreateAsRequest(stubbedExecution);
128
129         assertEquals(JOB_ID, stubbedExecution.getVariable(AS_INSTANCE_ID));
130     }
131
132     @Test
133     public void invokeCnfmWithCreateAsRequest_ForBBThrowsException_exceptionBuilderCalled() throws Exception {
134
135         final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
136         stubbedExecution.setVariable(CREATE_AS_REQUEST_OBJECT, new CreateAsRequest());
137
138         when(mockedCnfmHttpServiceProvider.invokeCreateAsRequest(any(CreateAsRequest.class)))
139                 .thenReturn(Optional.empty());
140         doThrow(BpmnError.class).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class),
141                 eq(2003), anyString(), any());
142
143         objUnderTest.invokeCnfmWithCreateAsRequest(stubbedExecution);
144
145         assertNull(stubbedExecution.getVariable(AS_INSTANCE_ID));
146         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(2003), anyString(),
147                 any());
148
149     }
150
151     @Test
152     public void testcreateAsInstanceRequest_withValidValues_storesRequestInExecution() throws Exception {
153
154         final CnfInstantiateTask objUnderTest = getCnfInstantiateTask();
155
156         objUnderTest.createAsInstanceRequest(stubbedExecution);
157
158         final InstantiateAsRequest actual = stubbedExecution.getVariable(INSTANTIATE_AS_REQUEST_OBJECT);
159         assertNotNull(actual);
160         assertNotNull(actual.getDeploymentItems());
161         assertEquals(1, actual.getDeploymentItems().size());
162         assertFalse(actual.getDeploymentItems().get(0).getDeploymentItemsId().isBlank());
163     }
164
165     private Optional<AsInstance> getAsInstance() {
166         final AsInstance response = new AsInstance();
167         response.setAsInstanceid(JOB_ID);
168         return Optional.of(response);
169     }
170
171     private CnfInstantiateTask getCnfInstantiateTask() {
172         return new CnfInstantiateTask(mockedCnfmHttpServiceProvider, exceptionUtil);
173     }
174 }