Fix for radio buttons
[sdc.git] / asdc-tests / src / main / java / org / openecomp / sdc / ci / tests / execute / inputs / InputsApiTests.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.execute.inputs;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.stream.Collectors;
35
36 import org.apache.commons.codec.binary.Base64;
37 import org.apache.commons.lang3.tuple.Pair;
38 import org.junit.Rule;
39 import org.junit.rules.TestName;
40 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
41 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
42 import org.openecomp.sdc.be.model.ArtifactUiDownloadData;
43 import org.openecomp.sdc.be.model.Component;
44 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
45 import org.openecomp.sdc.be.model.ComponentInstance;
46 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
47 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
48 import org.openecomp.sdc.be.model.InputDefinition;
49 import org.openecomp.sdc.be.model.PropertyDefinition;
50 import org.openecomp.sdc.be.model.Resource;
51 import org.openecomp.sdc.be.model.Service;
52 import org.openecomp.sdc.be.model.User;
53 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
54 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
55 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
56 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
57 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
58 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
59 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
60 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
61 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
62 import org.openecomp.sdc.ci.tests.utils.rest.ComponentInstanceRestUtils;
63 import org.openecomp.sdc.ci.tests.utils.rest.InputsRestUtils;
64 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
65 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
66 import org.openecomp.sdc.ci.tests.utils.validation.BaseValidationUtils;
67 import org.testng.annotations.Test;
68 import org.yaml.snakeyaml.Yaml;
69
70 import com.google.gson.Gson;
71 import com.google.gson.reflect.TypeToken;
72
73 import fj.data.Either;
74
75 /**
76  * CI-Tests for inputs 
77  * @author il0695
78  *
79  */
80 public class InputsApiTests extends ComponentBaseTest {
81
82         private static String inputCsar1 = "FCGI_with_inputs.csar";
83         private static String inputCsar2 = "LDSA1_with_inputs.csar";
84         private static User  sdncDesignerDetails = null;
85         
86         @Rule
87         public static TestName name = new TestName();
88         
89         /**
90          * Constructor
91          */
92         public InputsApiTests() {
93                 super(name, InputsApiTests.class.getName());
94                 sdncDesignerDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
95         }
96         
97         /**
98          * Create VF with inputs from CSAR file
99          * 
100          * @throws Exception 
101          */
102         @Test
103         public void testCreateResourceInstanceWithInputsFromCsar() throws Exception {
104                 Resource vf = AtomicOperationUtils.importResourceFromCSAR(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, inputCsar1);
105                 assertTrue("Success creating VF from CSAR", !vf.getInputs().isEmpty());
106         }
107         
108         /**
109          * Create service and add to it VF instance with inputs
110          * 
111          * @throws Exception 
112          */
113         @Test
114         public void testAddVfInstanceWithInputsToService() throws Exception {
115                 createServiceWithVFInstanceWithInputs();
116         }
117         
118         /**
119          * General test to check most functionality of inputs
120          * <ul>
121          *      <li>Create service with VF instance that has inputs)</li>
122          *      <li>Get all inputs of VF instance</li>
123          *      <li>Add inputs to service</li>
124          *      <li>Get service inputs</li>
125          *      <li>Delete service inputs</li>
126          * </ul>
127          * 
128          * @throws Exception 
129          */
130         @Test
131         public void testInputsMainFunctionality() throws Exception {
132                 Service service = createServiceWithVFInstanceWithInputs();
133                 int totalInputsBeforeAdd = service.getInputs().size();
134                 
135                 // Get component instances
136                 RestResponse getInstancesResponse = ComponentInstanceRestUtils.getComponentInstances(ComponentTypeEnum.SERVICE, service.getUniqueId(), sdncDesignerDetails);
137                 BaseValidationUtils.checkSuccess(getInstancesResponse);
138                 List<ComponentInstance> serviceInstances = new Gson().fromJson(getInstancesResponse.getResponse(), new TypeToken<ArrayList<ComponentInstance>>(){}.getType());
139                 
140                 // Get all inputs of first instance
141                 ComponentInstance vfInstance = serviceInstances.get(0);
142                 RestResponse getComponentInstanceInputsResponse = InputsRestUtils.getComponentInstanceInputs(service, vfInstance);
143                 BaseValidationUtils.checkSuccess(getComponentInstanceInputsResponse);
144                 List<ComponentInstancePropInput> instanceInputs = new Gson().fromJson(getComponentInstanceInputsResponse.getResponse(), new TypeToken<ArrayList<ComponentInstancePropInput>>(){}.getType());
145                 
146                 // Take only the 2 first inputs
147                 List<ComponentInstancePropInput> inputsToAdd = instanceInputs.stream().limit(2).collect(Collectors.toList());
148                 
149                 // Build component instances input map to add to server
150                 ComponentInstInputsMap buildComponentInstInputsMap = buildComponentInstInputsMap(vfInstance.getUniqueId(), inputsToAdd);
151                 RestResponse addInputResponse = InputsRestUtils.addInput(service, buildComponentInstInputsMap, UserRoleEnum.DESIGNER);
152                 BaseValidationUtils.checkSuccess(addInputResponse);
153                 
154                 // Get service inputs count
155                 RestResponse getComponentInputsResponse = InputsRestUtils.getComponentInputs(service);
156                 BaseValidationUtils.checkSuccess(getComponentInputsResponse);
157                 List<InputDefinition> serviceInputsAfterAdd = new Gson().fromJson(getComponentInputsResponse.getResponse(), new TypeToken<ArrayList<InputDefinition>>(){}.getType());
158                 if (serviceInputsAfterAdd.size()-totalInputsBeforeAdd!=2) {
159                         assertTrue("Error adding inputs to service (service should have 2 inputs)", false);
160                 }
161                 
162                 // Delete 1 input from service
163                 RestResponse deleteInputFromComponentResponse = InputsRestUtils.deleteInputFromComponent(service, serviceInputsAfterAdd.get(0).getUniqueId());
164                 BaseValidationUtils.checkSuccess(deleteInputFromComponentResponse);
165                 
166                 // Get service inputs count after delete
167                 RestResponse getComponentInputsResponseAfterDelete = InputsRestUtils.getComponentInputs(service);
168                 BaseValidationUtils.checkSuccess(getComponentInputsResponseAfterDelete);
169                 List<InputDefinition> serviceInputsAfterDelete = new Gson().fromJson(getComponentInputsResponseAfterDelete.getResponse(), new TypeToken<ArrayList<InputDefinition>>(){}.getType());
170                 if (serviceInputsAfterDelete.size()-totalInputsBeforeAdd!=1) {
171                         assertTrue("Error deleting inputs from service (service should have 1 input)", false);
172                 }
173                 
174                 assertTrue("Success testing inputs main functionality", true);
175         }
176
177         /**
178          * Test to check deletion of inputs related to CP/VL property values 
179          * @throws Exception 
180          */
181         @Test
182         public void testDeleteInputsRelatedToPropertyValues() throws Exception {
183                 Service service = createServiceWithVLinstance();
184                 User user = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
185                 String simplePropName = "network_technology";
186                 String complexPropName = "network_assignments";
187                 String propertiesName = "network_assignments#ipv6_subnet_default_assignment#ip_network_address_plan";
188                 String vlInstanceId = service.getComponentInstances().get(0).getUniqueId();
189                 
190                 // Take a simple property type and a complex property type 
191                 List<ComponentInstanceProperty> inputsToAdd = service.getComponentInstancesProperties().get(vlInstanceId)
192                                 .stream()
193                                 .filter(p -> simplePropName.equals(p.getName()) || complexPropName.equals(p.getName()))
194                                 .collect(Collectors.toList());
195                 
196                 List<ComponentInstancePropInput> propInputList = new ArrayList<>();
197                 for (ComponentInstanceProperty prop : inputsToAdd) {
198                         if (simplePropName.equals(prop.getName())) {
199                                 propInputList.add(new ComponentInstancePropInput(prop));
200                         } else {
201                                 propInputList.add(buildComponentInstNetworkAssignmentIpv6AssignmentComplexPropertyInput(prop, propertiesName));
202                         }
203                 }
204                 // Set component instance property input map and add to inputs service
205                 ComponentInstInputsMap componentInstInputsMap = buildComponentInstPropertyInputsMap(vlInstanceId, propInputList);
206                 RestResponse addInputResponse = InputsRestUtils.addInput(service, componentInstInputsMap, UserRoleEnum.DESIGNER);
207                 BaseValidationUtils.checkSuccess(addInputResponse);
208                 
209                 // Get the updated service
210                 ServiceReqDetails serviceDetails = new ServiceReqDetails(service);
211                 RestResponse getServiceResponse = ServiceRestUtils.getService(serviceDetails, user);
212                 service = ResponseParser.parseToObjectUsingMapper(getServiceResponse.getResponse(), Service.class);
213                 
214                 // validate instance get_input property values in service and tosca template
215                 ComponentInstance vlInstance = service.getComponentInstances().get(0);  
216                 String simplePropValue = "{\"get_input\":\"" + vlInstance.getNormalizedName() + "_" + simplePropName + "\"}";
217                 String complexPropValue = "{\"ipv6_subnet_default_assignment\":{\"ip_network_address_plan\":{\"get_input\":\"" + vlInstance.getNormalizedName() + "_" + propertiesName.replaceAll("#", "_") + "\"}}}";
218                 // download and compare
219                 Map<String, Object> load = downloadAndParseServiceToscaTemplate(user, service);
220                 validateGetInputInstancePropertyValues(load, service, simplePropName, simplePropValue, complexPropName, complexPropValue, vlInstance);
221                 
222                 // Delete inputs from service
223                 RestResponse deleteInputResponse = InputsRestUtils.deleteInputFromComponent(service, service.getInputs().get(0).getUniqueId());
224                 BaseValidationUtils.checkSuccess(deleteInputResponse);
225                 deleteInputResponse = InputsRestUtils.deleteInputFromComponent(service, service.getInputs().get(1).getUniqueId());
226                 BaseValidationUtils.checkSuccess(deleteInputResponse);
227                 
228                 // Get the updated service
229                 getServiceResponse = ServiceRestUtils.getService(serviceDetails, user);
230                 service = ResponseParser.parseToObjectUsingMapper(getServiceResponse.getResponse(), Service.class);
231                 
232                 // download and compare (get_input property values removed)
233                 load = downloadAndParseServiceToscaTemplate(user, service);
234                 simplePropValue = "{}";
235                 complexPropValue = "{\"ipv6_subnet_default_assignment\":{\"ip_network_address_plan\":{}}}"; 
236                 validateGetInputInstancePropertyValues(load, service, simplePropName, simplePropValue, complexPropName, complexPropValue, vlInstance);
237         
238         }
239         
240         /**
241          * Private method to create service with VF instance that has inputs
242          * This is private method to be used by multiple tests
243          * 
244          * @return {@link org.openecomp.sdc.be.model}
245          * @throws Exception
246          * @throws IOException
247          */
248         private Service createServiceWithVFInstanceWithInputs() throws Exception, IOException {
249                 // Create default service
250                 Either<Service, RestResponse> createDefaultServiceEither = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true);
251                 if (createDefaultServiceEither.isRight()){
252                         assertTrue("Error creating default service", false);
253                 }
254                 Service service = createDefaultServiceEither.left().value();
255                 
256                 // Create VF from CSAR file
257                 Resource vfWithInputs = AtomicOperationUtils.importResourceFromCSAR(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, inputCsar2);
258
259                 // Certify VF
260                 Pair<Component, RestResponse> changeComponentState = AtomicOperationUtils.changeComponentState(vfWithInputs, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true);
261                 assertTrue("response code is BaseRestUtils.STATUS_CODE_SUCCESS, returned :" + changeComponentState.getRight().getErrorCode(), changeComponentState.getRight().getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
262                 
263                 // Add VF instance to service
264                 Either<ComponentInstance, RestResponse> addComponentInstanceToComponentContainerEither = AtomicOperationUtils.addComponentInstanceToComponentContainer(vfWithInputs, service, UserRoleEnum.DESIGNER, true);
265                 if (addComponentInstanceToComponentContainerEither.isRight()){
266                         assertTrue("Error adding VF to service", false);
267                 }
268                                 
269                 // Get service response
270                 ServiceReqDetails serviceDetails = new ServiceReqDetails(service);
271                 RestResponse getServiceResponse = ServiceRestUtils.getService(serviceDetails, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
272                 service = ResponseParser.parseToObjectUsingMapper(getServiceResponse.getResponse(), Service.class);
273                 
274                 // Get VF instance from service
275                 ComponentInstance vfInstance = service.getComponentInstances().get(0);
276                 if (vfInstance!=null){
277                         assertTrue("Success creating service with VF instance", true);
278                 } else {
279                         assertTrue("Error creating service with VF instance", false);
280                 }
281                 return service;
282         }
283         
284         
285         private Service createServiceWithVLinstance() throws Exception, IOException {
286                 // Create default service
287                 Either<Service, RestResponse> createDefaultServiceEither = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true);
288                 if (createDefaultServiceEither.isRight()){
289                         assertTrue("Error creating default service", false);
290                 }
291                 Service service = createDefaultServiceEither.left().value();
292                 
293                 Resource vl = AtomicOperationUtils.getResourceObjectByNameAndVersion(UserRoleEnum.DESIGNER, "ExtVL", "1.0");
294                 
295                 // add VL instance to service  
296                 Either<ComponentInstance, RestResponse> vlInstDetails = AtomicOperationUtils.addComponentInstanceToComponentContainer(vl, service, UserRoleEnum.DESIGNER, true);
297                 if (vlInstDetails.isRight()){
298                         assertTrue("Error adding VF to service", false);
299                 }
300                                 
301                 // Get service response
302                 ServiceReqDetails serviceDetails = new ServiceReqDetails(service);
303                 RestResponse getServiceResponse = ServiceRestUtils.getService(serviceDetails, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
304                 service = ResponseParser.parseToObjectUsingMapper(getServiceResponse.getResponse(), Service.class);
305                 
306                 // Get instances from service
307                 ComponentInstance instance = service.getComponentInstances().get(0);
308                 if (instance != null){
309                         assertTrue("Success creating service with VF instance", true);
310                 } else {
311                         assertTrue("Error creating service with VF instance", false);
312                 }
313                 
314                 return service;
315         }
316         
317         /**     
318          * Return default ComponentInstInputsMap
319          * 
320          * @param addToInput
321          * @param inputs
322          * @return {@link org.openecomp.sdc.be.model.ComponentInstInputsMap}
323          */
324         private ComponentInstInputsMap buildComponentInstInputsMap (String addToInput, List<ComponentInstancePropInput> inputs) {
325                 Map<String, List<ComponentInstancePropInput>> map = new HashMap<>();
326                 map.put(addToInput, inputs);
327                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
328                 componentInstInputsMap.setComponentInstanceInputsMap(map);              
329                 return componentInstInputsMap;
330         }
331         
332         private ComponentInstInputsMap buildComponentInstPropertyInputsMap (String instanceId, List<ComponentInstancePropInput> props) {
333                 Map<String, List<ComponentInstancePropInput>> map = new HashMap<>();
334                 map.put(instanceId, props);
335                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
336                 componentInstInputsMap.setComponentInstancePropInput(map);              
337                 return componentInstInputsMap;
338         }
339                 
340         
341         private ComponentInstancePropInput buildComponentInstNetworkAssignmentIpv6AssignmentComplexPropertyInput (ComponentInstanceProperty prop, String propertiesName) {
342                 ComponentInstancePropInput componentInstancePropInput = new ComponentInstancePropInput(prop);
343                 componentInstancePropInput.setPropertiesName(propertiesName);
344                 PropertyDefinition input = new PropertyDefinition();
345                 input.setUniqueId("org.openecomp.datatypes.network.SubnetAssignments.datatype.ip_network_address_plan");
346                 input.setName("ip_network_address_plan");
347                 input.setParentUniqueId("org.openecomp.datatypes.network.SubnetAssignments.datatype");
348                 input.setType("string");
349                 componentInstancePropInput.setInput(input);
350                 return componentInstancePropInput;
351         
352         }
353         
354         private Map<String, Object> downloadAndParseServiceToscaTemplate(User user, Service service) throws Exception {
355                 String artifactUniqeId = service.getToscaArtifacts().get("assettoscatemplate").getUniqueId();
356                 RestResponse toscaTemplate = ArtifactRestUtils.downloadServiceArtifactInternalApi(service.getUniqueId(), user, artifactUniqeId);        
357                 BaseRestUtils.checkSuccess(toscaTemplate);
358                 ArtifactUiDownloadData artifactUiDownloadData = ResponseParser.parseToObject(toscaTemplate.getResponse(), ArtifactUiDownloadData.class);
359                 byte[] fromUiDownload = artifactUiDownloadData.getBase64Contents().getBytes();
360                 byte[] decodeBase64 = Base64.decodeBase64(fromUiDownload);
361                 Yaml yaml = new Yaml();
362                 InputStream inputStream = new ByteArrayInputStream(decodeBase64);
363                 Map<String, Object> load = (Map<String, Object>) yaml.load(inputStream);
364                 return load;
365         }
366         
367         private void validateGetInputInstancePropertyValues (Map<String, Object> load, Service service, String simplePropName, String simplePropValue, String complexPropName, String complexPropValue, ComponentInstance instance) {
368         
369                 String instanceName = instance.getName();
370                 String instanceId = instance.getUniqueId();
371                 ComponentInstanceProperty simpleProp = service.getComponentInstancesProperties().get(instanceId).stream().filter(p -> p.getName().equals(simplePropName)).findAny().get();
372                 ComponentInstanceProperty complexProp = service.getComponentInstancesProperties().get(instanceId).stream().filter(p -> p.getName().equals(complexPropName)).findAny().get();
373
374                 assertEquals(simpleProp.getValue(), simplePropValue);
375                 assertEquals(complexProp.getValue(), complexPropValue);
376                 // compare with downloaded tosca template  
377                 assertNotNull(load);
378                 Map<String, Object> topology_template = (Map<String, Object>) load.get("topology_template");
379                 assertNotNull(topology_template);
380                 Map<String, Object> nodes = (Map<String, Object>) topology_template.get("node_templates");
381                 assertNotNull(nodes);
382                 Map<String, Object> vlInstanceObj = (Map<String, Object>) nodes.get(instanceName);
383                 assertNotNull(vlInstanceObj);
384                 Map<String, Object> props = (Map<String, Object>) vlInstanceObj.get("properties");
385                 assertNotNull(props);
386                 Map<String, Object> complexPropObj = (Map<String, Object>) props.get(complexPropName);
387                 assertNotNull(complexPropObj);
388                 Gson gson = new Gson();
389                 assertEquals(gson.toJson(complexPropObj), complexProp.getValue());
390                 
391                 // if simpleProp has an empty value it will not be generated in the tosca map
392                 if (!simpleProp.getValue().equals("{}")){
393                         Map<String, Object> simplePropObj = (Map<String, Object>) props.get(simplePropName);
394                         assertNotNull(simplePropObj);
395                         assertEquals(gson.toJson(simplePropObj), simpleProp.getValue());
396                 }
397                 
398                 
399                 
400                 
401                 
402                 
403         }
404
405
406
407 }