2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.execute.imports;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
27 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipInputStream;
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;
61 public class CsarUtilsTest extends ComponentBaseTest {
63 public static final String ASSET_TOSCA_TEMPLATE = "assettoscatemplate";
66 public static TestName name = new TestName();
68 public CsarUtilsTest() {
69 super(name, CsarUtilsTest.class.getName());
73 public void createServiceCsarBasicTest() throws Exception {
75 Service service = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true).left().value();
77 Resource resourceVF = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
79 AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.VENDOR_LICENSE, resourceVF, UserRoleEnum.DESIGNER,
81 resourceVF = (Resource) AtomicOperationUtils
82 .changeComponentState(resourceVF, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
84 AtomicOperationUtils.addComponentInstanceToComponentContainer(resourceVF, service, UserRoleEnum.DESIGNER, true);
86 service = (Service) AtomicOperationUtils.changeComponentState(service, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
88 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
90 byte[] downloadCSAR = downloadCSAR(sdncModifierDetails, service);
92 csarBasicValidation(service, downloadCSAR);
96 public void createResourceCsarBasicTest() throws Exception {
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);
103 byte[] downloadCSAR = downloadCSAR(sdncModifierDetails, resourceVF);
105 csarBasicValidation(resourceVF, downloadCSAR);
108 @Test(enabled = true)
109 public void createServiceCsarInclDeploymentArtTest() throws Exception {
111 Service service = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true).left().value();
113 Resource resourceVF1 = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
114 Resource resourceVF2 = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
116 resourceVF1 = (Resource) AtomicOperationUtils
117 .changeComponentState(resourceVF1, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
119 resourceVF2 = (Resource) AtomicOperationUtils
120 .changeComponentState(resourceVF2, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
122 AtomicOperationUtils.addComponentInstanceToComponentContainer(resourceVF1, service, UserRoleEnum.DESIGNER, true);
123 AtomicOperationUtils.addComponentInstanceToComponentContainer(resourceVF2, service, UserRoleEnum.DESIGNER, true);
125 AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.YANG_XML, service, UserRoleEnum.DESIGNER, true, true);
127 service = (Service) AtomicOperationUtils.changeComponentState(service, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
129 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
131 byte[] downloadCSAR = downloadCSAR(sdncModifierDetails, service);
133 csarBasicValidation(service, downloadCSAR);
135 validateServiceCsar(resourceVF1, resourceVF2, service, downloadCSAR, 3, 5, 1);
138 @Test(enabled = true)
139 public void createResourceCsarInclDeploymentArtTest() throws Exception {
141 Resource resourceVF1 = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
143 AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.YANG_XML, resourceVF1, UserRoleEnum.DESIGNER, true, true);
144 AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.HEAT_ARTIFACT, resourceVF1, UserRoleEnum.DESIGNER, true, true);
146 resourceVF1 = (Resource) AtomicOperationUtils
147 .changeComponentState(resourceVF1, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
149 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
151 byte[] downloadCSAR = downloadCSAR(sdncModifierDetails, resourceVF1);
153 csarBasicValidation(resourceVF1, downloadCSAR);
155 validateVFCsar(resourceVF1, downloadCSAR, 1, 0, 1, 1);
158 private void csarBasicValidation(Component mainComponent, byte[] downloadCSAR) {
159 try (ByteArrayInputStream ins = new ByteArrayInputStream(downloadCSAR);
160 ZipInputStream zip = new ZipInputStream(ins);) {
162 String resourceYaml = null;
163 byte[] buffer = new byte[1024];
164 ZipEntry nextEntry = zip.getNextEntry();
165 StringBuffer sb = new StringBuffer();
168 while ((len = zip.read(buffer)) > 0) {
169 sb.append(new String(buffer, 0, len));
172 assertTrue(nextEntry.getName().equals("TOSCA-Metadata/TOSCA.meta"));
175 nextEntry = zip.getNextEntry();
177 while ((len = zip.read(buffer)) > 0) {
178 sb.append(new String(buffer, 0, len));
181 resourceYaml = sb.toString();
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()));
192 } catch (IOException e) {
197 private void validateServiceCsar(Component certifiedVFC1, Component certifiedVFC2, Service fetchedService,
198 byte[] resultByte, int toscaEntryIndexToPass, int generatorEntryIndexToPass,
199 int deploymentArtifactIndexToPass) {
201 // TODO Test to validate everything is right (comment out after testing)
203 FileUtils.writeByteArrayToFile(new File("c:/TestCSAR/" + fetchedService.getName() + ".zip"), resultByte);
204 } catch (IOException e) {
205 // Auto-generated catch block
209 try (ByteArrayInputStream ins = new ByteArrayInputStream(resultByte);
210 ZipInputStream zip = new ZipInputStream(ins);) {
212 String resourceYaml = null;
213 byte[] buffer = new byte[1024];
214 ZipEntry nextEntry = zip.getNextEntry();
215 StringBuffer sb = new StringBuffer();
218 while ((len = zip.read(buffer)) > 0) {
219 sb.append(new String(buffer, 0, len));
222 assertTrue(nextEntry.getName().equals("TOSCA-Metadata/TOSCA.meta"));
224 YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
226 int toscaEntryIndex = 0;
227 int generatorEntryIndex = 0;
228 int deploymentArtifactIndex = 0;
229 String fileName = null;
230 ArtifactDefinition artifactDefinition;
231 Component componentToValidate = null;
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();
240 while ((nextEntry = zip.getNextEntry()) != null) {
243 while ((len = zip.read(buffer)) > 0) {
244 sb.append(new String(buffer, 0, len));
247 String entryName = nextEntry.getName();
249 resourceYaml = sb.toString();
250 if (entryName.contains(serviceFileName)) {
251 componentToValidate = fetchedService;
252 fileName = "Definitions/" + serviceFileName;
254 assertEquals("Validate entry Name", (fileName), nextEntry.getName());
255 assertTrue(yamlToObjectConverter.isValidYaml(resourceYaml.getBytes()));
256 validateContent(resourceYaml, componentToValidate);
261 if (entryName.contains(vfc1FileName)) {
262 componentToValidate = certifiedVFC1;
263 fileName = "Definitions/" + vfc1FileName;
265 assertEquals("Validate entry Name", (fileName), nextEntry.getName());
266 assertTrue(yamlToObjectConverter.isValidYaml(resourceYaml.getBytes()));
267 validateContent(resourceYaml, componentToValidate);
271 if (entryName.contains(vfc2FileName)) {
272 componentToValidate = certifiedVFC2;
273 fileName = "Definitions/" + vfc2FileName;
275 assertEquals("Validate entry Name", (fileName), nextEntry.getName());
276 assertTrue(yamlToObjectConverter.isValidYaml(resourceYaml.getBytes()));
277 validateContent(resourceYaml, componentToValidate);
282 if (entryName.contains(".xml") && !entryName.startsWith("Artifacts/AAI")) {
283 ++deploymentArtifactIndex;
287 if (entryName.startsWith("Artifacts/AAI")) {
288 ++generatorEntryIndex;
292 assertTrue("Unexpected entry: " + entryName, true);
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);
301 } catch (IOException e) {
306 private void validateVFCsar(Component certifiedVF, byte[] resultByte, int toscaEntryIndexToPass, int ymlDeploymentArtifactIndexToPass,
307 int xmlDeploymentArtifactIndexToPass, int heatEnvDeploymentArtifactIndexToPass) {
309 // TODO Test to validate everything is right (comment out after testing)
311 FileUtils.writeByteArrayToFile(new File("c:/TestCSAR/" + fetchedService.getName() + ".zip"), resultByte);
312 } catch (IOException e) {
313 // Auto-generated catch block
317 try (ByteArrayInputStream ins = new ByteArrayInputStream(resultByte);
318 ZipInputStream zip = new ZipInputStream(ins);) {
320 String resourceYaml = null;
321 byte[] buffer = new byte[1024];
322 ZipEntry nextEntry = zip.getNextEntry();
323 StringBuffer sb = new StringBuffer();
326 while ((len = zip.read(buffer)) > 0) {
327 sb.append(new String(buffer, 0, len));
330 assertTrue(nextEntry.getName().equals("TOSCA-Metadata/TOSCA.meta"));
332 YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
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;
342 artifactDefinition = certifiedVF.getToscaArtifacts().get(ASSET_TOSCA_TEMPLATE);
343 String vfFileName = artifactDefinition.getArtifactName();
345 while ((nextEntry = zip.getNextEntry()) != null) {
348 while ((len = zip.read(buffer)) > 0) {
349 sb.append(new String(buffer, 0, len));
352 String entryName = nextEntry.getName();
354 resourceYaml = sb.toString();
355 if (entryName.contains(vfFileName)) {
356 componentToValidate = certifiedVF;
357 fileName = "Definitions/" + vfFileName;
359 assertEquals("Validate entry Name", (fileName), nextEntry.getName());
360 assertTrue(yamlToObjectConverter.isValidYaml(resourceYaml.getBytes()));
361 validateContent(resourceYaml, componentToValidate);
366 if (entryName.contains(".xml") && entryName.startsWith("Artifacts/")) {
371 if (entryName.contains(".sh") && entryName.startsWith("Artifacts/")) {
372 ++heatEnvDeploymentArtifactIndex;
376 if (entryName.contains(".yml") && entryName.startsWith("Artifacts/")) {
381 assertTrue("Unexpected entry: " + entryName, false);
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,
387 assertEquals("Validate amount of generated HEAT ENV artifacts", heatEnvDeploymentArtifactIndexToPass,
388 heatEnvDeploymentArtifactIndex);
392 } catch (IOException e) {
397 private void validateContent(String content, Component component) {
398 Yaml yaml = new Yaml();
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);
407 String name = (String) metadata.get("name");
409 assertEquals("Validate component name", component.getName(), name);
411 String invariantUUID = (String) metadata.get("invariantUUID");
412 assertNotNull(invariantUUID);
413 assertEquals("Validate component invariantUUID", component.getInvariantUUID(), invariantUUID);
415 String UUID = (String) metadata.get("UUID");
417 assertEquals("Validate component invariantUUID", component.getUUID(), UUID);
419 String type = (String) metadata.get("type");
421 if (component.getComponentType().equals(ComponentTypeEnum.SERVICE)) {
422 assertEquals("Validate component type", component.getComponentType().getValue(), type);
424 assertEquals("Validate component type", ((Resource) component).getResourceType(),
425 ResourceTypeEnum.valueOf(type));
429 private byte[] downloadCSAR(User sdncModifierDetails, Component createdComponent) throws Exception {
431 String artifactUniqeId = createdComponent.getToscaArtifacts().get("assettoscacsar").getUniqueId();
432 RestResponse getCsarResponse = null;
434 switch (createdComponent.getComponentType()) {
436 getCsarResponse = ArtifactRestUtils.downloadResourceArtifactInternalApi(createdComponent.getUniqueId(),
437 sdncModifierDetails, artifactUniqeId);
440 getCsarResponse = ArtifactRestUtils.downloadServiceArtifactInternalApi(createdComponent.getUniqueId(),
441 sdncModifierDetails, artifactUniqeId);
447 assertNotNull(getCsarResponse);
448 BaseRestUtils.checkSuccess(getCsarResponse);
450 ArtifactUiDownloadData artifactUiDownloadData = ResponseParser.parseToObject(getCsarResponse.getResponse(),
451 ArtifactUiDownloadData.class);
453 assertNotNull(artifactUiDownloadData);
455 byte[] fromUiDownload = artifactUiDownloadData.getBase64Contents().getBytes();
456 byte[] decodeBase64 = Base64.decodeBase64(fromUiDownload);