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