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.tosca.services.impl;
23 import org.junit.Assert;
24 import org.junit.Test;
25 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
26 import org.openecomp.core.utilities.file.FileContentHandler;
27 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
28 import org.openecomp.sdc.tosca.services.ToscaConstants;
29 import org.openecomp.sdc.tosca.services.ToscaUtil;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.util.Enumeration;
37 import java.util.HashMap;
39 import java.util.zip.ZipEntry;
40 import java.util.zip.ZipFile;
42 public class ToscaFileOutputServiceCsarImplTest {
45 public void testCreationMetaFile() {
46 ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCSARImpl =
47 new ToscaFileOutputServiceCsarImpl();
48 String createdMeta = toscaFileOutputServiceCSARImpl.createMetaFile("entryFile.yaml");
50 "TOSCA-Meta-File-Version: 1.0\n" +
51 "CSAR-Version: 1.1\n" +
52 "Created-By: ASDC Onboarding portal\n" +
53 "Entry-Definitions: Definitions" + File.separator + "entryFile.yaml";
54 Assert.assertEquals(createdMeta.replaceAll("\\s+", ""), expectedMeta.replaceAll("\\s+", ""));
58 public void testCSARFileCreationWithExternalArtifacts() throws IOException {
59 ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCSARImpl =
60 new ToscaFileOutputServiceCsarImpl();
61 ServiceTemplate mainServiceTemplate = new ServiceTemplate();
62 Map<String, String> metadata1 = new HashMap<>();
63 metadata1.put("Template_author", "OPENECOMP");
64 metadata1.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME,"ST1");
65 metadata1.put("Template_version", "1.0.0");
66 mainServiceTemplate.setMetadata(metadata1);
67 mainServiceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0");
68 mainServiceTemplate.setDescription("testing desc tosca service template");
70 ServiceTemplate additionalServiceTemplate = new ServiceTemplate();
71 Map<String, String> metadata2 = new HashMap<>();
72 metadata2.put("Template_author", "OPENECOMP");
73 metadata2.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "ST2");
74 metadata2.put("Template_version", "1.0.0");
75 additionalServiceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0");
76 additionalServiceTemplate.setDescription("testing desc tosca service template");
77 additionalServiceTemplate.setMetadata(metadata2);
79 Map<String, ServiceTemplate> definitionsInput = new HashMap<>();
81 .put(ToscaUtil.getServiceTemplateFileName(mainServiceTemplate), mainServiceTemplate);
82 definitionsInput.put(ToscaUtil.getServiceTemplateFileName(additionalServiceTemplate),
83 additionalServiceTemplate);
86 Map<String, byte[]> dummyHeatArtifacts = new HashMap<>();
87 String file1Content = "this is file number 1";
88 String file2Content = "this is file number 2";
89 String file1 = "file1.xml";
90 dummyHeatArtifacts.put(file1, file1Content.getBytes());
91 String file2 = "file2.yml";
92 dummyHeatArtifacts.put(file2, file2Content.getBytes());
95 FileContentHandler heatFiles = new FileContentHandler();
96 heatFiles.putAll(dummyHeatArtifacts);
97 Map<String, byte[]> licenseArtifacts = new HashMap<>();
99 FileContentHandler licenseArtifactsFiles = new FileContentHandler();
101 licenseArtifacts.put(
102 ToscaFileOutputServiceCsarImpl.EXTERNAL_ARTIFACTS_FOLDER_NAME + File.separator +
103 "license-file-1.xml", file1Content.getBytes());
104 licenseArtifacts.put(
105 ToscaFileOutputServiceCsarImpl.EXTERNAL_ARTIFACTS_FOLDER_NAME + File.separator +
106 "license-file-2.xml", file1Content.getBytes());
108 licenseArtifactsFiles.putAll(licenseArtifacts);
110 byte[] csarFile = toscaFileOutputServiceCSARImpl.createOutputFile(
111 new ToscaServiceModel(heatFiles, definitionsInput,
112 ToscaUtil.getServiceTemplateFileName(mainServiceTemplate)), licenseArtifactsFiles);
114 File file = File.createTempFile("resultFile", "zip");
115 try (FileOutputStream fos = new FileOutputStream(file)) {
119 try (ZipFile zipFile = new ZipFile(file)) {
121 Enumeration<? extends ZipEntry> entries = zipFile.entries();
124 while (entries.hasMoreElements()) {
126 entries.nextElement();
130 Files.delete(Paths.get(file.getAbsolutePath()));
134 public void testCSARFileCreation_noArtifacts() throws IOException {
135 ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCSARImpl =
136 new ToscaFileOutputServiceCsarImpl();
137 ServiceTemplate serviceTemplate = new ServiceTemplate();
138 Map<String, String> metadata = new HashMap<>();
139 metadata.put("Template_author", "OPENECOMP");
140 metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "Test");
141 metadata.put("Template_version", "1.0.0");
142 serviceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0");
143 serviceTemplate.setDescription("testing desc tosca service template");
144 serviceTemplate.setMetadata(metadata);
145 Map<String, ServiceTemplate> definitionsInput = new HashMap<>();
146 String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate);
147 definitionsInput.put(serviceTemplateFileName, serviceTemplate);
148 byte[] csarFile = toscaFileOutputServiceCSARImpl
149 .createOutputFile(new ToscaServiceModel(null, definitionsInput, serviceTemplateFileName),
153 File file = File.createTempFile("resultFile", "zip");
154 try (FileOutputStream fos = new FileOutputStream(file)) {
158 try (ZipFile zipFile = new ZipFile(file)) {
160 Enumeration<? extends ZipEntry> entries = zipFile.entries();
163 while (entries.hasMoreElements()) {
165 entries.nextElement();
167 Assert.assertEquals(2, count);
170 Files.delete(Paths.get(file.getAbsolutePath()));