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 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;
43 import java.io.ByteArrayInputStream;
44 import java.io.IOException;
45 import java.io.InputStream;
47 import java.util.zip.ZipEntry;
48 import java.util.zip.ZipInputStream;
50 import static org.junit.Assert.*;
52 public class CsarUtilsTest extends ComponentBaseTest {
54 public static final String ASSET_TOSCA_TEMPLATE = "assettoscatemplate";
57 public static TestName name = new TestName();
59 public CsarUtilsTest() {
60 super(name, CsarUtilsTest.class.getName());
64 public void createServiceCsarBasicTest() throws Exception {
66 Service service = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true).left().value();
68 Resource resourceVF = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
70 AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.VENDOR_LICENSE, resourceVF, UserRoleEnum.DESIGNER,
72 resourceVF = (Resource) AtomicOperationUtils
73 .changeComponentState(resourceVF, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
75 AtomicOperationUtils.addComponentInstanceToComponentContainer(resourceVF, service, UserRoleEnum.DESIGNER, true);
77 service = (Service) AtomicOperationUtils.changeComponentState(service, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
79 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
81 byte[] downloadCSAR = downloadCSAR(sdncModifierDetails, service);
83 csarBasicValidation(service, downloadCSAR);
87 public void createResourceCsarBasicTest() throws Exception {
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);
94 byte[] downloadCSAR = downloadCSAR(sdncModifierDetails, resourceVF);
96 csarBasicValidation(resourceVF, downloadCSAR);
100 public void createServiceCsarInclDeploymentArtTest() throws Exception {
102 Service service = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true).left().value();
104 Resource resourceVF1 = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
105 Resource resourceVF2 = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
107 resourceVF1 = (Resource) AtomicOperationUtils
108 .changeComponentState(resourceVF1, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
110 resourceVF2 = (Resource) AtomicOperationUtils
111 .changeComponentState(resourceVF2, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
113 AtomicOperationUtils.addComponentInstanceToComponentContainer(resourceVF1, service, UserRoleEnum.DESIGNER, true);
114 AtomicOperationUtils.addComponentInstanceToComponentContainer(resourceVF2, service, UserRoleEnum.DESIGNER, true);
116 AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.YANG_XML, service, UserRoleEnum.DESIGNER, true, true);
118 service = (Service) AtomicOperationUtils.changeComponentState(service, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
120 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
122 byte[] downloadCSAR = downloadCSAR(sdncModifierDetails, service);
124 csarBasicValidation(service, downloadCSAR);
126 validateServiceCsar(resourceVF1, resourceVF2, service, downloadCSAR, 3, 5, 1);
129 @Test(enabled = true)
130 public void createResourceCsarInclDeploymentArtTest() throws Exception {
132 Resource resourceVF1 = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
134 AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.YANG_XML, resourceVF1, UserRoleEnum.DESIGNER, true, true);
135 AtomicOperationUtils.uploadArtifactByType(ArtifactTypeEnum.HEAT_ARTIFACT, resourceVF1, UserRoleEnum.DESIGNER, true, true);
137 resourceVF1 = (Resource) AtomicOperationUtils
138 .changeComponentState(resourceVF1, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true).getLeft();
140 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
142 byte[] downloadCSAR = downloadCSAR(sdncModifierDetails, resourceVF1);
144 csarBasicValidation(resourceVF1, downloadCSAR);
146 validateVFCsar(resourceVF1, downloadCSAR, 1, 0, 1, 1);
149 private void csarBasicValidation(Component mainComponent, byte[] downloadCSAR) {
150 try (ByteArrayInputStream ins = new ByteArrayInputStream(downloadCSAR);
151 ZipInputStream zip = new ZipInputStream(ins);) {
153 String resourceYaml = null;
154 byte[] buffer = new byte[1024];
155 ZipEntry nextEntry = zip.getNextEntry();
156 StringBuffer sb = new StringBuffer();
159 while ((len = zip.read(buffer)) > 0) {
160 sb.append(new String(buffer, 0, len));
163 assertTrue(nextEntry.getName().equals("TOSCA-Metadata/TOSCA.meta"));
166 nextEntry = zip.getNextEntry();
168 while ((len = zip.read(buffer)) > 0) {
169 sb.append(new String(buffer, 0, len));
172 resourceYaml = sb.toString();
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()));
183 } catch (IOException e) {
188 private void validateServiceCsar(Component certifiedVFC1, Component certifiedVFC2, Service fetchedService,
189 byte[] resultByte, int toscaEntryIndexToPass, int generatorEntryIndexToPass,
190 int deploymentArtifactIndexToPass) {
192 // TODO Test to validate everything is right (comment out after testing)
194 FileUtils.writeByteArrayToFile(new File("c:/TestCSAR/" + fetchedService.getName() + ".zip"), resultByte);
195 } catch (IOException e) {
196 // Auto-generated catch block
200 try (ByteArrayInputStream ins = new ByteArrayInputStream(resultByte);
201 ZipInputStream zip = new ZipInputStream(ins);) {
203 String resourceYaml = null;
204 byte[] buffer = new byte[1024];
205 ZipEntry nextEntry = zip.getNextEntry();
206 StringBuffer sb = new StringBuffer();
209 while ((len = zip.read(buffer)) > 0) {
210 sb.append(new String(buffer, 0, len));
213 assertTrue(nextEntry.getName().equals("TOSCA-Metadata/TOSCA.meta"));
215 YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
217 int toscaEntryIndex = 0;
218 int generatorEntryIndex = 0;
219 int deploymentArtifactIndex = 0;
220 String fileName = null;
221 ArtifactDefinition artifactDefinition;
222 Component componentToValidate = null;
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();
231 while ((nextEntry = zip.getNextEntry()) != null) {
234 while ((len = zip.read(buffer)) > 0) {
235 sb.append(new String(buffer, 0, len));
238 String entryName = nextEntry.getName();
240 resourceYaml = sb.toString();
241 if (entryName.contains(serviceFileName)) {
242 componentToValidate = fetchedService;
243 fileName = "Definitions/" + serviceFileName;
245 assertEquals("Validate entry Name", (fileName), nextEntry.getName());
246 assertTrue(yamlToObjectConverter.isValidYaml(resourceYaml.getBytes()));
247 validateContent(resourceYaml, componentToValidate);
252 if (entryName.contains(vfc1FileName)) {
253 componentToValidate = certifiedVFC1;
254 fileName = "Definitions/" + vfc1FileName;
256 assertEquals("Validate entry Name", (fileName), nextEntry.getName());
257 assertTrue(yamlToObjectConverter.isValidYaml(resourceYaml.getBytes()));
258 validateContent(resourceYaml, componentToValidate);
262 if (entryName.contains(vfc2FileName)) {
263 componentToValidate = certifiedVFC2;
264 fileName = "Definitions/" + vfc2FileName;
266 assertEquals("Validate entry Name", (fileName), nextEntry.getName());
267 assertTrue(yamlToObjectConverter.isValidYaml(resourceYaml.getBytes()));
268 validateContent(resourceYaml, componentToValidate);
273 if (entryName.contains(".xml") && !entryName.startsWith("Artifacts/AAI")) {
274 ++deploymentArtifactIndex;
278 if (entryName.startsWith("Artifacts/AAI")) {
279 ++generatorEntryIndex;
283 assertTrue("Unexpected entry: " + entryName, true);
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);
292 } catch (IOException e) {
297 private void validateVFCsar(Component certifiedVF, byte[] resultByte, int toscaEntryIndexToPass, int ymlDeploymentArtifactIndexToPass,
298 int xmlDeploymentArtifactIndexToPass, int heatEnvDeploymentArtifactIndexToPass) {
300 // TODO Test to validate everything is right (comment out after testing)
302 FileUtils.writeByteArrayToFile(new File("c:/TestCSAR/" + fetchedService.getName() + ".zip"), resultByte);
303 } catch (IOException e) {
304 // Auto-generated catch block
308 try (ByteArrayInputStream ins = new ByteArrayInputStream(resultByte);
309 ZipInputStream zip = new ZipInputStream(ins);) {
311 String resourceYaml = null;
312 byte[] buffer = new byte[1024];
313 ZipEntry nextEntry = zip.getNextEntry();
314 StringBuffer sb = new StringBuffer();
317 while ((len = zip.read(buffer)) > 0) {
318 sb.append(new String(buffer, 0, len));
321 assertTrue(nextEntry.getName().equals("TOSCA-Metadata/TOSCA.meta"));
323 YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
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;
333 artifactDefinition = certifiedVF.getToscaArtifacts().get(ASSET_TOSCA_TEMPLATE);
334 String vfFileName = artifactDefinition.getArtifactName();
336 while ((nextEntry = zip.getNextEntry()) != null) {
339 while ((len = zip.read(buffer)) > 0) {
340 sb.append(new String(buffer, 0, len));
343 String entryName = nextEntry.getName();
345 resourceYaml = sb.toString();
346 if (entryName.contains(vfFileName)) {
347 componentToValidate = certifiedVF;
348 fileName = "Definitions/" + vfFileName;
350 assertEquals("Validate entry Name", (fileName), nextEntry.getName());
351 assertTrue(yamlToObjectConverter.isValidYaml(resourceYaml.getBytes()));
352 validateContent(resourceYaml, componentToValidate);
357 if (entryName.contains(".xml") && entryName.startsWith("Artifacts/")) {
362 if (entryName.contains(".sh") && entryName.startsWith("Artifacts/")) {
363 ++heatEnvDeploymentArtifactIndex;
367 if (entryName.contains(".yml") && entryName.startsWith("Artifacts/")) {
372 assertTrue("Unexpected entry: " + entryName, false);
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,
378 assertEquals("Validate amount of generated HEAT ENV artifacts", heatEnvDeploymentArtifactIndexToPass,
379 heatEnvDeploymentArtifactIndex);
383 } catch (IOException e) {
388 private void validateContent(String content, Component component) {
389 Yaml yaml = new Yaml();
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);
398 String name = (String) metadata.get("name");
400 assertEquals("Validate component name", component.getName(), name);
402 String invariantUUID = (String) metadata.get("invariantUUID");
403 assertNotNull(invariantUUID);
404 assertEquals("Validate component invariantUUID", component.getInvariantUUID(), invariantUUID);
406 String UUID = (String) metadata.get("UUID");
408 assertEquals("Validate component invariantUUID", component.getUUID(), UUID);
410 String type = (String) metadata.get("type");
412 if (component.getComponentType().equals(ComponentTypeEnum.SERVICE)) {
413 assertEquals("Validate component type", component.getComponentType().getValue(), type);
415 assertEquals("Validate component type", ((Resource) component).getResourceType(),
416 ResourceTypeEnum.valueOf(type));
420 private byte[] downloadCSAR(User sdncModifierDetails, Component createdComponent) throws Exception {
422 String artifactUniqeId = createdComponent.getToscaArtifacts().get("assettoscacsar").getUniqueId();
423 RestResponse getCsarResponse = null;
425 switch (createdComponent.getComponentType()) {
427 getCsarResponse = ArtifactRestUtils.downloadResourceArtifactInternalApi(createdComponent.getUniqueId(),
428 sdncModifierDetails, artifactUniqeId);
431 getCsarResponse = ArtifactRestUtils.downloadServiceArtifactInternalApi(createdComponent.getUniqueId(),
432 sdncModifierDetails, artifactUniqeId);
438 assertNotNull(getCsarResponse);
439 BaseRestUtils.checkSuccess(getCsarResponse);
441 ArtifactUiDownloadData artifactUiDownloadData = ResponseParser.parseToObject(getCsarResponse.getResponse(),
442 ArtifactUiDownloadData.class);
444 assertNotNull(artifactUiDownloadData);
446 byte[] fromUiDownload = artifactUiDownloadData.getBase64Contents().getBytes();
447 byte[] decodeBase64 = Base64.decodeBase64(fromUiDownload);