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