re base code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / imports / ImportToscaCapabilitiesWithProperties.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.imports;
22
23 import com.google.gson.Gson;
24 import org.apache.commons.codec.binary.Base64;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.junit.Rule;
27 import org.junit.rules.TestName;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
30 import org.openecomp.sdc.be.model.*;
31 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
32 import org.openecomp.sdc.ci.tests.datatypes.ImportReqDetails;
33 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
34 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
35 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
36 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
37 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
38 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
39 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
40 import org.testng.annotations.Test;
41
42 import java.io.IOException;
43 import java.nio.file.Files;
44 import java.nio.file.Path;
45 import java.nio.file.Paths;
46 import java.util.ArrayList;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.stream.Collectors;
50
51 import static org.testng.AssertJUnit.assertEquals;
52 import static org.testng.AssertJUnit.assertTrue;
53
54 /**
55  * US US730518 Story [BE] - TOSCA capabilities with properties - import "As a
56  * resource designer, I would like to add my VFC capabilities with properties."
57  * 
58  * @author ns019t
59  *
60  */
61 public class ImportToscaCapabilitiesWithProperties extends ComponentBaseTest {
62         @Rule
63         public static TestName name = new TestName();
64
65         Gson gson = new Gson();
66
67         /**
68          * public Constructor ImportToscaCapabilitiesWithProperties
69          */
70         public ImportToscaCapabilitiesWithProperties() {
71                 super(name, ImportToscaCapabilitiesWithProperties.class.getName());
72         }
73
74         /**
75          * String constants
76          */
77         public static String propertyForTestName = "propertyfortest";
78         public static String rootPath = System.getProperty("user.dir");
79         public static String scalable = "tosca.capabilities.Scalable";
80         public static String container = "tosca.capabilities.Container";
81         public static String minInstances = "min_instances";
82         public static String userDefinedNodeYaml = "mycompute.yml";
83
84         /**
85          * Capability Type - capability type on the graph should already have
86          * properties modeled on it. please verify. The import of the capability
87          * types should support adding those properties. when importing, validate
88          * name uniqueness between the capability's properties see capability
89          * tosca.capabilities.Container
90          * 
91          * Acceptance Criteria: validate capability type properties (for example,
92          * compute have capability Container -> the properties of this capability
93          * should be in the Json response)
94          * 
95          * @throws IOException
96          */
97         @Test
98         public void validateCapabilityTypePropertiesSucceed() throws IOException {
99                 User user = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
100                 RestResponse createResourceRes = ResourceRestUtils.getResourceByNameAndVersion(user.getUserId(), "Compute",
101                                 "1.0");
102                 BaseRestUtils.checkSuccess(createResourceRes);
103                 Resource resource = ResponseParser.convertResourceResponseToJavaObject(createResourceRes.getResponse());
104                 Map<String, List<CapabilityDefinition>> capabilities = resource.getCapabilities();
105                 assertEquals(capabilities.size(), 6);
106
107                 CapabilityDefinition capability = capabilities.get(scalable).get(0);
108                 List<ComponentInstanceProperty> properties = capability.getProperties();
109                 assertEquals(properties.size(), 3);
110                 assertTrue(!properties.stream().filter(p -> p.getName().equalsIgnoreCase(propertyForTestName)).findAny()
111                                 .isPresent());
112
113                 ComponentInstanceProperty originalProperty = properties.stream()
114                                 .filter(p -> p.getName().equalsIgnoreCase(minInstances)).findAny().get();
115                 assertEquals(originalProperty.getType(), "integer");
116                 assertEquals(originalProperty.getDefaultValue(), "1");
117
118                 capability = capabilities.get(container).get(0);
119                 properties = capability.getProperties();
120                 assertEquals(properties.size(), 4);
121         }
122
123         /**
124          * Capability Definition on VFC / CP / VL - properties can also be defined
125          * on the capability when the capability is declared. (property definition
126          * with default value) If the property name (case insensitive) already
127          * defined on the capability type, it overrides the capability from the
128          * capability type Import of VFC / CP /VL should support adding properties
129          * to the capability. when importing, validate name uniqueness between the
130          * capability's properties
131          * 
132          * Acceptance Criteria: import node type with capability definition on it.
133          * use the attached "myCompute"
134          * 
135          * @throws Exception
136          */
137         @Test
138         public void importNodeTypeWithCapabilityWithPropertiesFromYmlSucceed() throws Exception {
139                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
140                 ImportReqDetails resourceDetails = ElementFactory.getDefaultImportResource();
141                 RestResponse createResource = importUserDefinedNodeType(userDefinedNodeYaml, sdncModifierDetails,
142                                 resourceDetails);
143                 BaseRestUtils.checkCreateResponse(createResource);
144                 Resource resource = ResponseParser.parseToObjectUsingMapper(createResource.getResponse(), Resource.class);
145
146                 Map<String, List<CapabilityDefinition>> capabilities = resource.getCapabilities();
147                 assertEquals(capabilities.size(), 6);
148
149                 CapabilityDefinition capability = capabilities.get(scalable).get(0);
150                 List<ComponentInstanceProperty> properties = capability.getProperties();
151                 assertEquals(properties.size(), 4);
152
153                 ComponentInstanceProperty newProperty = properties.stream()
154                                 .filter(p -> p.getName().equalsIgnoreCase(propertyForTestName)).findAny().get();
155                 assertEquals(newProperty.getType(), "string");
156                 assertEquals(newProperty.getDescription(), "test");
157                 assertEquals(newProperty.getDefaultValue(), "success");
158
159                 ComponentInstanceProperty overriddenProperty = properties.stream()
160                                 .filter(p -> p.getName().equalsIgnoreCase(minInstances)).collect(Collectors.toList()).get(0);
161                 assertEquals(overriddenProperty.getType(), "integer");
162                 assertEquals(overriddenProperty.getDefaultValue(), "3");
163
164         }
165
166         /**
167          * importNodeTypeWithCapabilityWithPropertiesFromYmlFailed
168          * 
169          * @throws Exception
170          */
171         @Test
172         public void importNodeTypeWithCapabilityWithPropertiesFromYmlFailed() throws Exception {
173                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
174                 ImportReqDetails resourceDetails = ElementFactory.getDefaultImportResource();
175                 RestResponse createResource = importUserDefinedNodeType("mycompute_failed.yml", sdncModifierDetails,
176                                 resourceDetails);
177                 BaseRestUtils.checkErrorMessageResponse(createResource, ActionStatus.PROPERTY_NAME_ALREADY_EXISTS);
178         }
179
180         /**
181          * Capability Assignment (on node_template / resource instance) - should
182          * support assignment of the property (property value). On the resource
183          * instance level, value can be assigned to either properties that are
184          * defined on the capability type or on the capability definition. When
185          * importing a VF - the node_template can have capability's property value.
186          * It should be imported and saved on the graph Acceptance Criteria: import
187          * a VF that assign values to property of capability that was defined on the
188          * capability type
189          * 
190          * @throws Exception
191          */
192         @Test
193         public void importResourceWithCapabilityWithPropertiesOverridingCapTypePropertiesSucceed() throws Exception {
194                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
195                 String payloadName = "vf_with_cap_prop_override_cap_type_prop.csar";
196                 ImportReqDetails resourceDetails = ElementFactory.getDefaultImportResource();
197                 Path path = Paths.get(rootPath + "/src/test/resources/CI/csars/vf_with_cap_prop_override_cap_type_prop.csar");
198                 byte[] data = Files.readAllBytes(path);
199                 String payloadData = Base64.encodeBase64String(data);
200                 resourceDetails.setPayloadData(payloadData);
201
202                 resourceDetails.setPayloadName(payloadName);
203                 resourceDetails.setResourceType(ResourceTypeEnum.VF.name());
204                 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
205                 BaseRestUtils.checkCreateResponse(createResource);
206
207                 List<ImmutablePair<String, String>> propertyNamesValues = new ArrayList<>();
208                 propertyNamesValues.add(new ImmutablePair<String, String>("num_cpus", "2"));
209                 propertyNamesValues.add(new ImmutablePair<String, String>("mem_size", "2000 MB"));
210                 checkResource(createResource, 8, container, "DBMS", propertyNamesValues);
211
212                 ResourceReqDetails resourceDetails2 = ElementFactory.getDefaultResource();
213                 resourceDetails2.setCsarUUID("vf_with_cap_prop_override_cap_type_prop.csar");
214                 resourceDetails2.setResourceType(ResourceTypeEnum.VF.name());
215                 createResource = ResourceRestUtils.createResource(resourceDetails2, sdncModifierDetails);
216                 BaseRestUtils.checkCreateResponse(createResource);
217
218                 checkResource(createResource, 8, container, "DBMS", propertyNamesValues);
219         }
220
221         /**
222          * importResourceWithCapabilityWithPropertiesOverridingCapTypePropertiesFailed
223          * 
224          * @throws Exception
225          */
226         @Test
227         public void importResourceWithCapabilityWithPropertiesOverridingCapTypePropertiesFailed() throws Exception {
228                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
229                 String payloadName = "vf_with_cap_prop_override_cap_type_prop_failed.csar";
230                 ImportReqDetails resourceDetails = ElementFactory.getDefaultImportResource();
231                 Path path = Paths
232                                 .get(rootPath + "/src/test/resources/CI/csars/vf_with_cap_prop_override_cap_type_prop_failed.csar");
233                 byte[] data = Files.readAllBytes(path);
234                 String payloadData = Base64.encodeBase64String(data);
235                 resourceDetails.setPayloadData(payloadData);
236
237                 resourceDetails.setPayloadName(payloadName);
238                 resourceDetails.setResourceType(ResourceTypeEnum.VF.name());
239                 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
240                 BaseRestUtils.checkErrorMessageResponse(createResource, ActionStatus.INVALID_PROPERTY);
241
242                 ResourceReqDetails resourceDetails2 = ElementFactory.getDefaultResource();
243                 resourceDetails2.setCsarUUID("vf_with_cap_prop_override_cap_type_prop_failed.csar");
244                 resourceDetails2.setResourceType(ResourceTypeEnum.VF.name());
245                 createResource = ResourceRestUtils.createResource(resourceDetails2, sdncModifierDetails);
246                 BaseRestUtils.checkErrorMessageResponse(createResource, ActionStatus.INVALID_PROPERTY);
247
248         }
249
250         /**
251          * Capability Assignment (on node_template / resource instance) - should
252          * support assignment of the property (property value). On the resource
253          * instance level, value can be assigned to either properties that are
254          * defined on the capability type or on the capability definition. When
255          * importing a VF - the node_template can have capability's property value.
256          * It should be imported and saved on the graph Acceptance Criteria: import
257          * a VF that assign values to property of capability that was defined on the
258          * capability definition (on the node type)
259          * 
260          * @throws Exception
261          */
262         @Test
263         public void importResourceWithCapabilityWithPropertiesOverridingNodeTypeCapPropertiesSucceed() throws Exception {
264
265                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
266                 ImportReqDetails resourceDetails = ElementFactory.getDefaultImportResource();
267                 RestResponse createResource = importUserDefinedNodeType(userDefinedNodeYaml, sdncModifierDetails,
268                                 resourceDetails);
269                 BaseRestUtils.checkCreateResponse(createResource);
270                 Resource userDefinedNodeType = ResponseParser.parseToObjectUsingMapper(createResource.getResponse(),
271                                 Resource.class);
272
273                 String payloadName = "vf_with_cap_prop_override_cap_type_prop1.csar";
274                 resourceDetails = ElementFactory.getDefaultImportResource();
275                 Path path = Paths.get(rootPath + "/src/test/resources/CI/csars/vf_with_cap_prop_override_cap_type_prop1.csar");
276                 byte[] data = Files.readAllBytes(path);
277                 String payloadData = Base64.encodeBase64String(data);
278                 resourceDetails.setPayloadData(payloadData);
279
280                 resourceDetails.setPayloadName(payloadName);
281                 resourceDetails.setResourceType(ResourceTypeEnum.VF.name());
282                 createResource = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
283                 BaseRestUtils.checkCreateResponse(createResource);
284
285                 List<ImmutablePair<String, String>> propertyNamesValues = new ArrayList<>();
286                 propertyNamesValues.add(new ImmutablePair<String, String>("num_cpus", "2"));
287                 propertyNamesValues.add(new ImmutablePair<String, String>("mem_size", "2000 MB"));
288                 checkResource(createResource, 8, container, "DBMS", propertyNamesValues);
289
290                 List<ImmutablePair<String, String>> propertyNamesValues1 = new ArrayList<>();
291                 propertyNamesValues1.add(new ImmutablePair<String, String>(propertyForTestName, "success_again"));
292                 propertyNamesValues1.add(new ImmutablePair<String, String>(minInstances, "4"));
293                 checkResource(createResource, 8, scalable, userDefinedNodeType.getName(), propertyNamesValues1);
294
295                 ResourceReqDetails resourceDetails2 = ElementFactory.getDefaultResource();
296                 resourceDetails2.setCsarUUID("vf_with_cap_prop_override_cap_type_prop1.csar");
297                 resourceDetails2.setResourceType(ResourceTypeEnum.VF.name());
298                 createResource = ResourceRestUtils.createResource(resourceDetails2, sdncModifierDetails);
299                 BaseRestUtils.checkCreateResponse(createResource);
300
301                 checkResource(createResource, 8, container, "DBMS", propertyNamesValues);
302                 checkResource(createResource, 8, scalable, userDefinedNodeType.getName(), propertyNamesValues1);
303
304         }
305
306         /**
307          * importResourceWithCapabilityWithPropertiesOverridingNodeTypeCapPropertiesFailed
308          * 
309          * @throws Exception
310          */
311         @Test
312         public void importResourceWithCapabilityWithPropertiesOverridingNodeTypeCapPropertiesFailed() throws Exception {
313
314                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
315                 ImportReqDetails resourceDetails = ElementFactory.getDefaultImportResource();
316                 RestResponse createResource = importUserDefinedNodeType(userDefinedNodeYaml, sdncModifierDetails,
317                                 resourceDetails);
318                 BaseRestUtils.checkCreateResponse(createResource);
319
320                 String payloadName = "vf_with_cap_prop_override_cap_type_prop1_failed.csar";
321                 resourceDetails = ElementFactory.getDefaultImportResource();
322                 Path path = Paths
323                                 .get(rootPath + "/src/test/resources/CI/csars/vf_with_cap_prop_override_cap_type_prop1_failed.csar");
324                 byte[] data = Files.readAllBytes(path);
325                 String payloadData = Base64.encodeBase64String(data);
326                 resourceDetails.setPayloadData(payloadData);
327
328                 resourceDetails.setPayloadName(payloadName);
329                 resourceDetails.setResourceType(ResourceTypeEnum.VF.name());
330                 createResource = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
331                 BaseRestUtils.checkErrorResponse(createResource, ActionStatus.PROPERTY_NAME_ALREADY_EXISTS,
332                                 propertyForTestName);
333
334                 ResourceReqDetails resourceDetails2 = ElementFactory.getDefaultResource();
335                 resourceDetails2.setCsarUUID("vf_with_cap_prop_override_cap_type_prop1_failed.csar");
336                 resourceDetails2.setResourceType(ResourceTypeEnum.VF.name());
337                 createResource = ResourceRestUtils.createResource(resourceDetails2, sdncModifierDetails);
338                 BaseRestUtils.checkErrorResponse(createResource, ActionStatus.PROPERTY_NAME_ALREADY_EXISTS,
339                                 propertyForTestName);
340         }
341
342         private RestResponse importUserDefinedNodeType(String payloadName, User sdncModifierDetails,
343                         ImportReqDetails resourceDetails) throws Exception {
344
345                 Path path = Paths.get(rootPath + "/src/test/resources/CI/csars/" + payloadName);
346                 byte[] data = Files.readAllBytes(path);
347                 String payloadData = Base64.encodeBase64String(data);
348                 resourceDetails.setPayloadData(payloadData);
349
350                 resourceDetails.setPayloadName(payloadName);
351                 resourceDetails.setResourceType(ResourceTypeEnum.VFC.name());
352                 return ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
353         }
354
355         // TODO Tal: Since Cashing change partial resource returned that causes null
356         // pointer exception
357         // commented out till fixing
358         private void checkResource(RestResponse createResource, int capNum, String capType, String riName,
359                         List<ImmutablePair<String, String>> propertyNamesValues) {
360                 Resource resource = ResponseParser.parseToObjectUsingMapper(createResource.getResponse(), Resource.class);
361
362                 Map<String, List<CapabilityDefinition>> capabilities = resource.getCapabilities();
363                 // TODO Tal: Since Cashing change partial resource returned that causes
364                 // null pointer exception
365                 /* assertEquals(capabilities.size(), capNum); */
366                 /*
367                  * List<CapabilityDefinition> capabilitesContainer =
368                  * capabilities.get(capType);
369                  */
370
371                 ComponentInstance resourceRI = resource.getComponentInstances().stream()
372                                 .filter(ri -> ri.getComponentName().equals(riName)).collect(Collectors.toList()).get(0);
373                 // TODO Tal: Since Cashing change partial resource returned that causes
374                 // null pointer exception
375                 /*
376                  * CapabilityDefinition capabilityFromContainer =
377                  * capabilitesContainer.stream()
378                  * .filter(cap->cap.getOwnerId().equals(resourceRI.getUniqueId())).
379                  * collect(Collectors.toList()).get(0);
380                  */
381
382                 CapabilityDefinition capabilityFromRI = resourceRI.getCapabilities().get(capType).get(0);
383                 for (ImmutablePair<String, String> propValuePair : propertyNamesValues) {
384                         // TODO Tal: Since Cashing change partial resource returned that
385                         // causes null pointer exception
386                         /*
387                          * Map<String, ComponentInstanceProperty> propertiesFromContainer =
388                          * capabilityFromContainer.getProperties()
389                          * .stream().filter(p->p.getName().equalsIgnoreCase(propValuePair.
390                          * getLeft())) .collect(Collectors.toMap(p->p.getName(), p->p));
391                          */
392
393                         List<ComponentInstanceProperty> propertiesFromRI = capabilityFromRI.getProperties().stream()
394                                         .filter(p -> p.getName().equalsIgnoreCase(propValuePair.getLeft())).collect(Collectors.toList());
395                         // TODO Tal: Since Cashing change partial resource returned that
396                         // causes null pointer exception
397                         /*
398                          * for(ComponentInstanceProperty riProp : propertiesFromRI){
399                          * assertTrue(propertiesFromContainer.containsKey(riProp.getName()))
400                          * ; ComponentInstanceProperty containerProp =
401                          * propertiesFromContainer.get(riProp.getName());
402                          * assertEquals(riProp.getValue(), containerProp.getValue());
403                          * if(riProp.getName().equals(propValuePair.getLeft()))
404                          * assertEquals(riProp.getValue(), propValuePair.getRight());
405                          * 
406                          * }
407                          */
408                 }
409         }
410
411 }