06defaa84c888920abe5d0eb4fe7bb66a05e28d3
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / client / cds / AbstractVnfCDSRequestProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix
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  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.so.client.cds;
21
22 import com.google.gson.JsonParser;
23 import org.camunda.bpm.engine.delegate.DelegateExecution;
24 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
25 import org.junit.Before;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.so.bpmn.common.BuildingBlockExecution;
30 import org.onap.so.bpmn.common.DelegateExecutionImpl;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
34 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
35 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
36 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
37 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
38 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters;
39 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
40 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoServiceInstance;
41 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoVfModule;
42 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
43 import org.onap.so.serviceinstancebeans.*;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.LinkedList;
48 import java.util.Map;
49
50 @RunWith(MockitoJUnitRunner.Silent.class)
51 public abstract class AbstractVnfCDSRequestProviderTest {
52
53     protected static final String GENERIC_VNF_ID = "vnfId_configVnfTest1";
54     protected static final String VF_MODULE_ID = "vf-module-id-1";
55     protected static final String VF_MODULE_NAME = "vf-module-name-1";
56     protected static final String VF_MODULE_CUSTOMIZATION_UUID = "23ce9ac4-e5dd-11e9-81b4-2a2ae2dbcce1";
57     protected static final String GENERIC_VNF_NAME = "vnf-name-1";
58     protected static final String SERVICE_INSTANCE_ID = "serviceInst_configTest";
59     protected static final String SERVICE_MODEL_UUID = "b45b5780-e5dd-11e9-81b4-2a2ae2dbcce4";
60     protected static final String SERVICE_INSTANCE_NAME = "test-service-instance";
61     protected static final String VNF_MODEL_CUSTOMIZATION_UUID = "23ce9ac4-e5dd-11e9-81b4-2a2ae2dbcce4";
62     protected static final String GENERAL_BLOCK_EXECUTION_MAP_KEY = "gBBInput";
63     protected static final String VNF_SCOPE = "vnf";
64     protected static final String SERVICE_SCOPE = "service";
65     protected static final String SERVICE_ACTION = "create";
66     protected static final String VF_SCOPE = "vfModule";
67     protected static final String ASSIGN_ACTION = "configAssign";
68     protected static final String DEPLOY_ACTION = "configDeploy";
69     protected static final String MSO_REQUEST_ID = "1234";
70     protected static final String BUILDING_BLOCK = "buildingBlock";
71     protected static final String PUBLIC_NET_ID = "public-net-id";
72     protected static final String CLOUD_REGION = "acl-cloud-region";
73
74     @Mock
75     protected ExtractPojosForBB extractPojosForBB;
76
77     protected BuildingBlockExecution buildingBlockExecution;
78
79     protected ExecuteBuildingBlock executeBuildingBlock;
80
81
82     @Before
83     public void setUp() {
84         buildingBlockExecution = createBuildingBlockExecution();
85         executeBuildingBlock = new ExecuteBuildingBlock();
86     }
87
88     protected BuildingBlockExecution createBuildingBlockExecution() {
89         DelegateExecution execution = new DelegateExecutionFake();
90         execution.setVariable(GENERAL_BLOCK_EXECUTION_MAP_KEY, createGeneralBuildingBlock());
91         return new DelegateExecutionImpl(execution);
92     }
93
94     protected GeneralBuildingBlock createGeneralBuildingBlock() {
95         GeneralBuildingBlock generalBuildingBlock = new GeneralBuildingBlock();
96         RequestContext requestContext = new RequestContext();
97         RequestParameters requestParameters = new RequestParameters();
98         requestParameters.setUserParams(createRequestUserParams());
99         requestContext.setRequestParameters(requestParameters);
100         requestContext.setMsoRequestId(MSO_REQUEST_ID);
101         generalBuildingBlock.setRequestContext(requestContext);
102         return generalBuildingBlock;
103     }
104
105     protected ServiceInstance createServiceInstance() {
106         ServiceInstance serviceInstance = new ServiceInstance();
107         serviceInstance.setServiceInstanceName(SERVICE_INSTANCE_NAME);
108         serviceInstance.setServiceInstanceId(SERVICE_INSTANCE_ID);
109         ModelInfoServiceInstance modelInfoServiceInstance = new ModelInfoServiceInstance();
110         modelInfoServiceInstance.setModelUuid(SERVICE_MODEL_UUID);
111         serviceInstance.setModelInfoServiceInstance(modelInfoServiceInstance);
112         return serviceInstance;
113     }
114
115     protected GenericVnf createGenericVnf() {
116         GenericVnf genericVnf = new GenericVnf();
117         genericVnf.setVnfId(GENERIC_VNF_ID);
118         genericVnf.setVnfName(GENERIC_VNF_NAME);
119         genericVnf.setBlueprintName("test");
120         genericVnf.setBlueprintVersion("1.0.0");
121         ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
122         modelInfoGenericVnf.setModelCustomizationUuid(VNF_MODEL_CUSTOMIZATION_UUID);
123         genericVnf.setModelInfoGenericVnf(modelInfoGenericVnf);
124         return genericVnf;
125     }
126
127     protected VfModule createVfModule() {
128         VfModule vfModule = new VfModule();
129         vfModule.setVfModuleId(VF_MODULE_ID);
130         vfModule.setVfModuleName(VF_MODULE_NAME);
131         ModelInfoVfModule modelInfoVfModule = new ModelInfoVfModule();
132         modelInfoVfModule.setModelCustomizationUUID(VF_MODULE_CUSTOMIZATION_UUID);
133         vfModule.setModelInfoVfModule(modelInfoVfModule);
134         return vfModule;
135     }
136
137     protected List<Map<String, Object>> createRequestUserParams() {
138         List<Map<String, Object>> userParams = new ArrayList<>();
139         Map<String, Object> userParamMap = new HashMap<>();
140         userParamMap.put("service", getUserParams());
141         userParams.add(userParamMap);
142         return userParams;
143     }
144
145     protected Service getUserParams() {
146         Service service = new Service();
147         Resources resources = new Resources();
148         resources.setVnfs(createVnfList());
149         service.setResources(resources);
150         return service;
151     }
152
153     protected List<Vnfs> createVnfList() {
154         List<Map<String, String>> instanceParamsListSearchedVnf = new ArrayList<>();
155         Map<String, String> instanceParam = new HashMap<>();
156         instanceParam.put("public_net_id", PUBLIC_NET_ID);
157         instanceParam.put("acl-cloud-region", CLOUD_REGION);
158         instanceParamsListSearchedVnf.add(instanceParam);
159         Vnfs searchedVnf = createVnf(instanceParamsListSearchedVnf);
160         List<Vnfs> vnfList = new ArrayList<>();
161         vnfList.add(searchedVnf);
162         return vnfList;
163     }
164
165     protected Vnfs createVnf(List<Map<String, String>> instanceParamsList) {
166         Vnfs vnf = new Vnfs();
167         ModelInfo modelInfo = new ModelInfo();
168         modelInfo.setModelCustomizationId(VNF_MODEL_CUSTOMIZATION_UUID);
169         vnf.setModelInfo(modelInfo);
170         vnf.setInstanceParams(instanceParamsList);
171
172         // Set instance parameters and modelinfo for vf-module
173         VfModules vfModule = new VfModules();
174         ModelInfo modelInfoForVfModule = new ModelInfo();
175         modelInfoForVfModule.setModelCustomizationId(VF_MODULE_CUSTOMIZATION_UUID);
176         vfModule.setModelInfo(modelInfoForVfModule);
177
178         List<Map<String, String>> instanceParamsListSearchedVfModule = new ArrayList<>();
179         Map<String, String> instanceParams = new HashMap<>();
180         instanceParams.put("public-net-vf-module-id", PUBLIC_NET_ID);
181         instanceParams.put("aci-cloud-region-vf-module", CLOUD_REGION);
182
183         instanceParamsListSearchedVfModule.add(instanceParams);
184         vfModule.setInstanceParams(instanceParamsListSearchedVfModule);
185
186         List<VfModules> vfModules = new ArrayList<>();
187         vfModules.add(vfModule);
188
189         vnf.setVfModules(vfModules);
190
191         return vnf;
192     }
193
194     protected boolean verfiyJsonFromString(String payload) {
195         JsonParser parser = new JsonParser();
196         return parser.parse(payload).isJsonObject();
197     }
198
199     protected void setScopeAndAction(String scope, String action) {
200         BuildingBlock buildingBlock = new BuildingBlock();
201         buildingBlock.setBpmnScope(scope);
202         buildingBlock.setBpmnAction(action);
203         executeBuildingBlock.setBuildingBlock(buildingBlock);
204         buildingBlockExecution.setVariable(BUILDING_BLOCK, executeBuildingBlock);
205     }
206
207     protected void setScopeAndActionWithoutUserParams(String scope, String action) {
208         buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getRequestParameters()
209                 .setUserParams(new LinkedList<>());
210         setScopeAndAction(scope, action);
211     }
212 }