bbc20706a646261f7d2de83704da24da50d1d1b3
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 TechMahindra.
6  * Copyright (C) 2019 Nokia.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27 import com.fasterxml.jackson.databind.JsonNode;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import org.camunda.bpm.engine.delegate.DelegateExecution;
34 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.so.bpmn.common.BuildingBlockExecution;
38 import org.onap.so.bpmn.common.DelegateExecutionImpl;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
41 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
42 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
43 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
44 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters;
45 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
46 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoServiceInstance;
47 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
48 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
49 import org.onap.so.client.exception.ExceptionBuilder;
50 import org.onap.so.serviceinstancebeans.ModelInfo;
51 import org.onap.so.serviceinstancebeans.Resources;
52 import org.onap.so.serviceinstancebeans.Service;
53 import org.onap.so.serviceinstancebeans.Vnfs;
54
55 public class ConfigAssignVnfTest {
56
57     private static final String GENERIC_VNF_ID = "vnfId_configVnfTest";
58     private static final String GENERIC_VNF_NAME = "vnfName_configVnfTest";
59     private static final String VNF_MODEL_CUSTOMIZATION_ID = "0c1ac643-377e-475b-be50-6be65f91a7ad";
60     private static final String SERVICE_INSTANCE_ID = "serviceInst_configTest";
61     private static final String SERVICE_MODEL_UUID = "5af91c26-8418-4d3f-944c-965842deda94";
62     private static final String TARGET_VNF_MODEL_CUSTOMIZATION_UUID = "0c1ac643-377e-475b-be50-6be65f91a7ad";
63     private static final String GENERAL_BLOCK_EXECUTION_MAP_KEY = "gBBInput";
64     private static final int THE_NUMBER_OF_EXPECTED_CONFIG_PROPERTIES = 8;
65
66     private static final String INSTANCE_PARAM1_NAME = "paramName1";
67     private static final String INSTANCE_PARAM1_VALUE = "paramValue1";
68     private static final String INSTANCE_PARAM2_NAME = "paramName2";
69     private static final String INSTANCE_PARAM2_VALUE = "paramValue2";
70     private static final String INSTANCE_PARAM3_NAME = "paramName3";
71     private static final String INSTANCE_PARAM3_VALUE = "paramValue3";
72     private static final String INSTANCE_PARAM4_NAME = "paramName4";
73     private static final String INSTANCE_PARAM4_VALUE = "paramValue4";
74     private static final String INSTANCE_PARAM5_NAME = "paramName5";
75     private static final String INSTANCE_PARAM5_VALUE = "paramValue5";
76
77
78     private ConfigAssignVnf testedObject;
79
80     private BuildingBlockExecution buildingBlockExecution;
81     private ExtractPojosForBB extractPojosForBB;
82
83     @Before
84     public void setup() {
85         buildingBlockExecution = createBuildingBlockExecution();
86         extractPojosForBB = mock(ExtractPojosForBB.class);
87         testedObject = new ConfigAssignVnf(extractPojosForBB, new ExceptionBuilder());
88     }
89
90     @Test
91     public void prepareAbstractCDSPropertiesBean_success() throws Exception {
92         // given
93         when(extractPojosForBB.extractByKey(buildingBlockExecution, ResourceKey.GENERIC_VNF_ID))
94                 .thenReturn(createGenericVnf());
95         when(extractPojosForBB.extractByKey(buildingBlockExecution, ResourceKey.SERVICE_INSTANCE_ID))
96                 .thenReturn(createServiceInstance());
97         // when
98         testedObject.preProcessAbstractCDSProcessing(buildingBlockExecution);
99         // then
100         verifyConfigAssignPropertiesJsonContent();
101     }
102
103     private void verifyConfigAssignPropertiesJsonContent() throws Exception {
104         AbstractCDSPropertiesBean abstractCDSPropertiesBean = buildingBlockExecution.getVariable("executionObject");
105         String payload = abstractCDSPropertiesBean.getRequestObject();
106         ObjectMapper mapper = new ObjectMapper();
107         JsonNode payloadJson = mapper.readTree(payload);
108         JsonNode configAssignPropertiesNode = payloadJson.findValue("config-assign-properties");
109         assertThat(configAssignPropertiesNode.size()).isEqualTo(THE_NUMBER_OF_EXPECTED_CONFIG_PROPERTIES);
110         assertThat(configAssignPropertiesNode.get("service-instance-id").asText()).isEqualTo(SERVICE_INSTANCE_ID);
111         assertThat(configAssignPropertiesNode.get("vnf-id").asText()).isEqualTo(GENERIC_VNF_ID);
112         assertThat(configAssignPropertiesNode.get("vnf-name").asText()).isEqualTo(GENERIC_VNF_NAME);
113         assertThat(configAssignPropertiesNode.get("service-model-uuid").asText()).isEqualTo(SERVICE_MODEL_UUID);
114         assertThat(configAssignPropertiesNode.get("vnf-customization-uuid").asText())
115                 .isEqualTo(VNF_MODEL_CUSTOMIZATION_ID);
116         assertThat(configAssignPropertiesNode.has(INSTANCE_PARAM1_NAME)).isTrue();
117         assertThat(configAssignPropertiesNode.get(INSTANCE_PARAM1_NAME).asText()).isEqualTo(INSTANCE_PARAM1_VALUE);
118         assertThat(configAssignPropertiesNode.has(INSTANCE_PARAM2_NAME)).isTrue();
119         assertThat(configAssignPropertiesNode.get(INSTANCE_PARAM2_NAME).asText()).isEqualTo(INSTANCE_PARAM2_VALUE);
120         assertThat(configAssignPropertiesNode.has(INSTANCE_PARAM3_NAME)).isTrue();
121         assertThat(configAssignPropertiesNode.get(INSTANCE_PARAM3_NAME).asText()).isEqualTo(INSTANCE_PARAM3_VALUE);
122     }
123
124     private BuildingBlockExecution createBuildingBlockExecution() {
125         DelegateExecution execution = new DelegateExecutionFake();
126         execution.setVariable(GENERAL_BLOCK_EXECUTION_MAP_KEY, createGeneralBuildingBlock());
127         return new DelegateExecutionImpl(execution);
128     }
129
130     private ServiceInstance createServiceInstance() {
131         ServiceInstance serviceInstance = new ServiceInstance();
132         serviceInstance.setServiceInstanceId(SERVICE_INSTANCE_ID);
133         ModelInfoServiceInstance modelInfoServiceInstance = new ModelInfoServiceInstance();
134         modelInfoServiceInstance.setModelUuid(SERVICE_MODEL_UUID);
135         serviceInstance.setModelInfoServiceInstance(modelInfoServiceInstance);
136         return serviceInstance;
137     }
138
139     private GenericVnf createGenericVnf() {
140         GenericVnf genericVnf = new GenericVnf();
141         genericVnf.setVnfId(GENERIC_VNF_ID);
142         genericVnf.setVnfName(GENERIC_VNF_NAME);
143         ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
144         modelInfoGenericVnf.setModelCustomizationUuid(TARGET_VNF_MODEL_CUSTOMIZATION_UUID);
145         modelInfoGenericVnf.setBlueprintName("blueprintTest");
146         modelInfoGenericVnf.setBlueprintVersion("blueprintVerTest");
147         genericVnf.setModelInfoGenericVnf(modelInfoGenericVnf);
148         return genericVnf;
149     }
150
151     private GeneralBuildingBlock createGeneralBuildingBlock() {
152         GeneralBuildingBlock generalBuildingBlock = new GeneralBuildingBlock();
153         RequestContext requestContext = new RequestContext();
154         RequestParameters requestParameters = new RequestParameters();
155         requestParameters.setUserParams(createRequestUserParams());
156         requestContext.setRequestParameters(requestParameters);
157         generalBuildingBlock.setRequestContext(requestContext);
158         return generalBuildingBlock;
159     }
160
161     private List<Map<String, Object>> createRequestUserParams() {
162         List<Map<String, Object>> userParams = new ArrayList<>();
163         Map<String, Object> userParamMap = new HashMap<>();
164         userParamMap.put("service", createService());
165         userParams.add(userParamMap);
166         return userParams;
167     }
168
169     private Service createService() {
170         Service service = new Service();
171         Resources resources = new Resources();
172         resources.setVnfs(createVnfList());
173         service.setResources(resources);
174         return service;
175     }
176
177     private List<Vnfs> createVnfList() {
178         List<Map<String, String>> instanceParamsListSearchedVnf = new ArrayList<>();
179         Map<String, String> instanceParam = new HashMap<>();
180         instanceParam.put(INSTANCE_PARAM1_NAME, INSTANCE_PARAM1_VALUE);
181         instanceParam.put(INSTANCE_PARAM2_NAME, INSTANCE_PARAM2_VALUE);
182         Map<String, String> instanceParam2 = new HashMap<>();
183         instanceParam2.put(INSTANCE_PARAM3_NAME, INSTANCE_PARAM3_VALUE);
184         instanceParamsListSearchedVnf.add(instanceParam);
185         instanceParamsListSearchedVnf.add(instanceParam2);
186         Vnfs searchedVnf = createVnf(VNF_MODEL_CUSTOMIZATION_ID, instanceParamsListSearchedVnf);
187
188         List<Map<String, String>> instanceParamsListForAnotherVnf = new ArrayList<>();
189         Map<String, String> instanceParam3 = new HashMap<>();
190         instanceParam3.put(INSTANCE_PARAM4_NAME, INSTANCE_PARAM4_VALUE);
191         instanceParam3.put(INSTANCE_PARAM5_NAME, INSTANCE_PARAM5_VALUE);
192         instanceParamsListForAnotherVnf.add(instanceParam3);
193         Vnfs anotherVnf = createVnf("2d1ac656-377e-467b-be50-6ce65f66a7ca", instanceParamsListForAnotherVnf);
194
195         List<Vnfs> vnfList = new ArrayList<>();
196         vnfList.add(searchedVnf);
197         vnfList.add(anotherVnf);
198         return vnfList;
199     }
200
201     private Vnfs createVnf(String vnfModelCustomizationId, List<Map<String, String>> instanceParamsList) {
202         Vnfs vnf = new Vnfs();
203         ModelInfo modelInfo = new ModelInfo();
204         modelInfo.setModelCustomizationId(vnfModelCustomizationId);
205         vnf.setModelInfo(modelInfo);
206         vnf.setInstanceParams(instanceParamsList);
207         return vnf;
208     }
209 }