[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / imports / CsarUtilsTest.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 static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Map;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipInputStream;
34
35 import org.apache.commons.codec.binary.Base64;
36 import org.apache.commons.io.FileUtils;
37 import org.junit.Rule;
38 import org.junit.rules.TestName;
39 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
40 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
41 import org.openecomp.sdc.be.model.ArtifactDefinition;
42 import org.openecomp.sdc.be.model.ArtifactUiDownloadData;
43 import org.openecomp.sdc.be.model.Component;
44 import org.openecomp.sdc.be.model.Resource;
45 import org.openecomp.sdc.be.model.Service;
46 import org.openecomp.sdc.be.model.User;
47 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
48 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
49 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
50 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
51 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
52 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
53 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
54 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
55 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
56 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
57 import org.openecomp.sdc.common.util.YamlToObjectConverter;
58 import org.testng.annotations.Test;
59 import org.yaml.snakeyaml.Yaml;
60
61 public class CsarUtilsTest extends ComponentBaseTest {
62         
63         public static final String ASSET_TOSCA_TEMPLATE = "assettoscatemplate";
64         
65         @Rule
66         public static TestName name = new TestName();
67         
68         public CsarUtilsTest() {
69                 super(name, CsarUtilsTest.class.getName());
70         }
71         
72         @Test(enabled = true)
73         public void createServiceCsarBasicTest() throws Exception {
74                 
75                 Service service = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true).left().value();
76
77                 Resource resourceVF = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
78                 
79                 AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.VENDOR_LICENSE, resourceVF, UserRoleEnum.DESIGNER,
80                                 true, true);
81                 resourceVF = (Resource) AtomicOperationUtils
82                                 .changeComponentState(resourceVF, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
83                 
84                 AtomicOperationUtils.addComponentInstanceToComponentContainer(resourceVF, service, UserRoleEnum.DESIGNER, true);
85                 
86                 service = (Service) AtomicOperationUtils.changeComponentState(service, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
87                 
88                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
89                 
90                 byte[] downloadCSAR = downloadCSAR(sdncModifierDetails, service);
91                 
92                 csarBasicValidation(service, downloadCSAR);
93         }
94         
95         @Test(enabled = true)
96         public void createResourceCsarBasicTest() throws Exception {
97
98                 Resource resourceVF = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
99                 resourceVF = (Resource) AtomicOperationUtils
100                                 .changeComponentState(resourceVF, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
101                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
102                 
103                 byte[] downloadCSAR = downloadCSAR(sdncModifierDetails, resourceVF);
104                 
105                 csarBasicValidation(resourceVF, downloadCSAR);
106         }
107         
108         @Test(enabled = true)
109         public void createServiceCsarInclDeploymentArtTest() throws Exception {
110                 
111                 Service service = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true).left().value();
112
113                 Resource resourceVF1 = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
114                 Resource resourceVF2 = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
115                 
116                 resourceVF1 = (Resource) AtomicOperationUtils
117                                 .changeComponentState(resourceVF1, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
118                 
119                 resourceVF2 = (Resource) AtomicOperationUtils
120                                 .changeComponentState(resourceVF2, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
121                 
122                 AtomicOperationUtils.addComponentInstanceToComponentContainer(resourceVF1, service, UserRoleEnum.DESIGNER, true);
123                 AtomicOperationUtils.addComponentInstanceToComponentContainer(resourceVF2, service, UserRoleEnum.DESIGNER, true);
124                 
125                 AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.YANG_XML, service, UserRoleEnum.DESIGNER, true, true);
126                 
127                 service = (Service) AtomicOperationUtils.changeComponentState(service, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
128                 
129                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
130                 
131                 byte[] downloadCSAR = downloadCSAR(sdncModifierDetails, service);
132                 
133                 csarBasicValidation(service, downloadCSAR);
134                 
135                 validateServiceCsar(resourceVF1, resourceVF2, service, downloadCSAR, 3, 5, 1);
136         }
137         
138         @Test(enabled = true)
139         public void createResourceCsarInclDeploymentArtTest() throws Exception {
140
141                 Resource resourceVF1 = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
142                 
143                 AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.YANG_XML, resourceVF1, UserRoleEnum.DESIGNER, true, true);
144                 AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.HEAT_ARTIFACT, resourceVF1, UserRoleEnum.DESIGNER, true, true);
145                 
146                 resourceVF1 = (Resource) AtomicOperationUtils
147                                 .changeComponentState(resourceVF1, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
148
149                 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
150                 
151                 byte[] downloadCSAR = downloadCSAR(sdncModifierDetails, resourceVF1);
152                 
153                 csarBasicValidation(resourceVF1, downloadCSAR);
154                 
155                 validateVFCsar(resourceVF1, downloadCSAR, 1, 0, 1, 1);
156         }
157         
158         private void csarBasicValidation(Component mainComponent, byte[] downloadCSAR) {
159                 try (ByteArrayInputStream ins = new ByteArrayInputStream(downloadCSAR);
160                                 ZipInputStream zip = new ZipInputStream(ins);) {
161
162                         String resourceYaml = null;
163                         byte[] buffer = new byte[1024];
164                         ZipEntry nextEntry = zip.getNextEntry();
165                         StringBuffer sb = new StringBuffer();
166                         int len;
167
168                         while ((len = zip.read(buffer)) > 0) {
169                                 sb.append(new String(buffer, 0, len));
170                         }
171
172                         assertTrue(nextEntry.getName().equals("TOSCA-Metadata/TOSCA.meta"));
173
174                         sb.setLength(0);
175                         nextEntry = zip.getNextEntry();
176
177                         while ((len = zip.read(buffer)) > 0) {
178                                 sb.append(new String(buffer, 0, len));
179                         }
180
181                         resourceYaml = sb.toString();
182
183                         YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
184                         ArtifactDefinition artifactDefinition = mainComponent.getToscaArtifacts()
185                                         .get(ASSET_TOSCA_TEMPLATE);
186                         String fileName = artifactDefinition.getArtifactName();
187                         assertEquals("Tosca-Template file name: ", "Definitions/" + fileName, nextEntry.getName());
188                         assertTrue("Tosca template Yaml validation: ", yamlToObjectConverter.isValidYaml(resourceYaml.getBytes()));
189
190                         ins.close();
191                         zip.close();
192                 } catch (IOException e) {
193                         e.printStackTrace();
194                 }
195         }
196         
197         private void validateServiceCsar(Component certifiedVFC1, Component certifiedVFC2, Service fetchedService,
198                         byte[] resultByte, int toscaEntryIndexToPass, int generatorEntryIndexToPass,
199                         int deploymentArtifactIndexToPass) {
200                 
201                 // TODO Test to validate everything is right (comment out after testing)
202                 /*try {
203                         FileUtils.writeByteArrayToFile(new File("c:/TestCSAR/" + fetchedService.getName() + ".zip"), resultByte);
204                 } catch (IOException e) {
205                         // Auto-generated catch block
206                         e.printStackTrace();
207                 }*/
208
209                 try (ByteArrayInputStream ins = new ByteArrayInputStream(resultByte);
210                                 ZipInputStream zip = new ZipInputStream(ins);) {
211                         
212                         String resourceYaml = null;
213                         byte[] buffer = new byte[1024];
214                         ZipEntry nextEntry = zip.getNextEntry();
215                         StringBuffer sb = new StringBuffer();
216                         int len;
217
218                         while ((len = zip.read(buffer)) > 0) {
219                                 sb.append(new String(buffer, 0, len));
220                         }
221
222                         assertTrue(nextEntry.getName().equals("TOSCA-Metadata/TOSCA.meta"));
223
224                         YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
225
226                         int toscaEntryIndex = 0;
227                         int generatorEntryIndex = 0;
228                         int deploymentArtifactIndex = 0;
229                         String fileName = null;
230                         ArtifactDefinition artifactDefinition;
231                         Component componentToValidate = null;
232
233                         artifactDefinition = fetchedService.getToscaArtifacts().get(ASSET_TOSCA_TEMPLATE);
234                         String serviceFileName = artifactDefinition.getArtifactName();
235                         artifactDefinition = certifiedVFC1.getToscaArtifacts().get(ASSET_TOSCA_TEMPLATE);
236                         String vfc1FileName = artifactDefinition.getArtifactName();
237                         artifactDefinition = certifiedVFC2.getToscaArtifacts().get(ASSET_TOSCA_TEMPLATE);
238                         String vfc2FileName = artifactDefinition.getArtifactName();
239
240                         while ((nextEntry = zip.getNextEntry()) != null) {
241                                 sb.setLength(0);
242
243                                 while ((len = zip.read(buffer)) > 0) {
244                                         sb.append(new String(buffer, 0, len));
245                                 }
246
247                                 String entryName = nextEntry.getName();
248
249                                 resourceYaml = sb.toString();
250                                 if (entryName.contains(serviceFileName)) {
251                                         componentToValidate = fetchedService;
252                                         fileName = "Definitions/" + serviceFileName;
253                                         
254                                         assertEquals("Validate entry Name", (fileName), nextEntry.getName());
255                                         assertTrue(yamlToObjectConverter.isValidYaml(resourceYaml.getBytes()));
256                                         validateContent(resourceYaml, componentToValidate);
257                                         ++toscaEntryIndex;
258                                         continue;
259                                 }
260                                 
261                                 if (entryName.contains(vfc1FileName)) {
262                                         componentToValidate = certifiedVFC1;
263                                         fileName = "Definitions/" + vfc1FileName;
264                                         
265                                         assertEquals("Validate entry Name", (fileName), nextEntry.getName());
266                                         assertTrue(yamlToObjectConverter.isValidYaml(resourceYaml.getBytes()));
267                                         validateContent(resourceYaml, componentToValidate);
268                                         ++toscaEntryIndex;
269                                         continue;
270                                 }
271                                 if (entryName.contains(vfc2FileName)) {
272                                         componentToValidate = certifiedVFC2;
273                                         fileName = "Definitions/" + vfc2FileName;
274                                         
275                                         assertEquals("Validate entry Name", (fileName), nextEntry.getName());
276                                         assertTrue(yamlToObjectConverter.isValidYaml(resourceYaml.getBytes()));
277                                         validateContent(resourceYaml, componentToValidate);
278                                         ++toscaEntryIndex;
279                                         continue;
280                                 }
281
282                                 if (entryName.contains(".xml") && !entryName.startsWith("Artifacts/AAI")) {
283                                         ++deploymentArtifactIndex;
284                                         continue;
285                                 }
286
287                                 if (entryName.startsWith("Artifacts/AAI")) {
288                                         ++generatorEntryIndex;
289                                         continue;
290                                 }
291                                 
292                                 assertTrue("Unexpected entry: " + entryName, true);
293                         }
294                         assertEquals("Validate amount of entries", toscaEntryIndexToPass, toscaEntryIndex);
295                         assertEquals("Validate amount of generated AAI artifacts", generatorEntryIndexToPass, generatorEntryIndex);
296                         assertEquals("Validate amount of generated Deployment artifacts", deploymentArtifactIndexToPass,
297                                         deploymentArtifactIndex);
298
299                         ins.close();
300                         zip.close();
301                 } catch (IOException e) {
302                         e.printStackTrace();
303                 }
304         }
305         
306         private void validateVFCsar(Component certifiedVF, byte[] resultByte, int toscaEntryIndexToPass, int ymlDeploymentArtifactIndexToPass,
307                         int xmlDeploymentArtifactIndexToPass, int heatEnvDeploymentArtifactIndexToPass) {
308                 
309                 // TODO Test to validate everything is right (comment out after testing)
310                 /*try {
311                         FileUtils.writeByteArrayToFile(new File("c:/TestCSAR/" + fetchedService.getName() + ".zip"), resultByte);
312                 } catch (IOException e) {
313                         // Auto-generated catch block
314                         e.printStackTrace();
315                 }*/
316
317                 try (ByteArrayInputStream ins = new ByteArrayInputStream(resultByte);
318                                 ZipInputStream zip = new ZipInputStream(ins);) {
319                         
320                         String resourceYaml = null;
321                         byte[] buffer = new byte[1024];
322                         ZipEntry nextEntry = zip.getNextEntry();
323                         StringBuffer sb = new StringBuffer();
324                         int len;
325
326                         while ((len = zip.read(buffer)) > 0) {
327                                 sb.append(new String(buffer, 0, len));
328                         }
329
330                         assertTrue(nextEntry.getName().equals("TOSCA-Metadata/TOSCA.meta"));
331
332                         YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
333
334                         int toscaEntryIndex = 0;
335                         int ymlEntryIndex = 0;
336                         int xmlArtifactsIndex = 0;
337                         int heatEnvDeploymentArtifactIndex = 0;
338                         String fileName = null;
339                         ArtifactDefinition artifactDefinition;
340                         Component componentToValidate = null;
341
342                         artifactDefinition = certifiedVF.getToscaArtifacts().get(ASSET_TOSCA_TEMPLATE);
343                         String vfFileName = artifactDefinition.getArtifactName();
344
345                         while ((nextEntry = zip.getNextEntry()) != null) {
346                                 sb.setLength(0);
347
348                                 while ((len = zip.read(buffer)) > 0) {
349                                         sb.append(new String(buffer, 0, len));
350                                 }
351
352                                 String entryName = nextEntry.getName();
353
354                                 resourceYaml = sb.toString();
355                                 if (entryName.contains(vfFileName)) {
356                                         componentToValidate = certifiedVF;
357                                         fileName = "Definitions/" + vfFileName;
358                                         
359                                         assertEquals("Validate entry Name", (fileName), nextEntry.getName());
360                                         assertTrue(yamlToObjectConverter.isValidYaml(resourceYaml.getBytes()));
361                                         validateContent(resourceYaml, componentToValidate);
362                                         ++toscaEntryIndex;
363                                         continue;
364                                 }
365
366                                 if (entryName.contains(".xml") && entryName.startsWith("Artifacts/")) {
367                                         ++xmlArtifactsIndex;
368                                         continue;
369                                 }
370
371                                 if (entryName.contains(".sh") && entryName.startsWith("Artifacts/")) {
372                                         ++heatEnvDeploymentArtifactIndex;
373                                         continue;
374                                 }
375                                 
376                                 if (entryName.contains(".yml") && entryName.startsWith("Artifacts/")) {
377                                         ++ymlEntryIndex;
378                                         continue;
379                                 }
380                                 
381                                 assertTrue("Unexpected entry: " + entryName, false);
382                         }
383                         assertEquals("Validate amount of entries", toscaEntryIndexToPass, toscaEntryIndex);
384                         assertEquals("Validate amount of YAML artifacts", ymlDeploymentArtifactIndexToPass, ymlEntryIndex);
385                         assertEquals("Validate amount of generated XML artifacts", xmlDeploymentArtifactIndexToPass,
386                                         xmlArtifactsIndex);
387                         assertEquals("Validate amount of generated HEAT ENV artifacts", heatEnvDeploymentArtifactIndexToPass,
388                                         heatEnvDeploymentArtifactIndex);
389
390                         ins.close();
391                         zip.close();
392                 } catch (IOException e) {
393                         e.printStackTrace();
394                 }
395         }
396         
397         private void validateContent(String content, Component component) {
398                 Yaml yaml = new Yaml();
399
400                 InputStream inputStream = new ByteArrayInputStream(content.getBytes());
401                 @SuppressWarnings("unchecked")
402                 Map<String, Object> load = (Map<String, Object>) yaml.load(inputStream);
403                 @SuppressWarnings("unchecked")
404                 Map<String, Object> metadata = (Map<String, Object>) load.get("metadata");
405                 assertNotNull(metadata);
406
407                 String name = (String) metadata.get("name");
408                 assertNotNull(name);
409                 assertEquals("Validate component name", component.getName(), name);
410
411                 String invariantUUID = (String) metadata.get("invariantUUID");
412                 assertNotNull(invariantUUID);
413                 assertEquals("Validate component invariantUUID", component.getInvariantUUID(), invariantUUID);
414
415                 String UUID = (String) metadata.get("UUID");
416                 assertNotNull(UUID);
417                 assertEquals("Validate component invariantUUID", component.getUUID(), UUID);
418
419                 String type = (String) metadata.get("type");
420                 assertNotNull(type);
421                 if (component.getComponentType().equals(ComponentTypeEnum.SERVICE)) {
422                         assertEquals("Validate component type", component.getComponentType().getValue(), type);
423                 } else {
424                         assertEquals("Validate component type", ((Resource) component).getResourceType(),
425                                         ResourceTypeEnum.valueOf(type));
426                 }
427         }
428         
429         private byte[] downloadCSAR(User sdncModifierDetails, Component createdComponent) throws Exception {
430
431                 String artifactUniqeId = createdComponent.getToscaArtifacts().get("assettoscacsar").getUniqueId();
432                 RestResponse getCsarResponse = null;
433                 
434                 switch (createdComponent.getComponentType()) {
435                 case RESOURCE:
436                         getCsarResponse = ArtifactRestUtils.downloadResourceArtifactInternalApi(createdComponent.getUniqueId(),
437                                         sdncModifierDetails, artifactUniqeId);
438                         break;                  
439                 case SERVICE:
440                         getCsarResponse = ArtifactRestUtils.downloadServiceArtifactInternalApi(createdComponent.getUniqueId(),
441                                         sdncModifierDetails, artifactUniqeId);
442                         break;
443                 default:
444                         break;
445                 }
446                 
447                 assertNotNull(getCsarResponse);
448                 BaseRestUtils.checkSuccess(getCsarResponse);
449
450                 ArtifactUiDownloadData artifactUiDownloadData = ResponseParser.parseToObject(getCsarResponse.getResponse(),
451                                 ArtifactUiDownloadData.class);
452
453                 assertNotNull(artifactUiDownloadData);
454
455                 byte[] fromUiDownload = artifactUiDownloadData.getBase64Contents().getBytes();
456                 byte[] decodeBase64 = Base64.decodeBase64(fromUiDownload);
457
458                 return decodeBase64;
459         }
460 }