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.openecomp.core.utilities.file.FileContentHandler;
26 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
27 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
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 String resultFileName = "resultFile.zip";
115 File file = new File(resultFileName);
116 try (FileOutputStream fos = new FileOutputStream(file)) {
120 try (ZipFile zipFile = new ZipFile(resultFileName)) {
122 Enumeration<? extends ZipEntry> entries = zipFile.entries();
125 while (entries.hasMoreElements()) {
127 entries.nextElement();
129 Assert.assertEquals(7, count);
132 Files.delete(Paths.get(file.getPath()));
136 public void testCSARFileCreation_noArtifacts() throws IOException {
137 ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCSARImpl =
138 new ToscaFileOutputServiceCsarImpl();
139 ServiceTemplate serviceTemplate = new ServiceTemplate();
140 Map<String, String> metadata = new HashMap<>();
141 metadata.put("Template_author", "OPENECOMP");
142 metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "Test");
143 metadata.put("Template_version", "1.0.0");
144 serviceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0");
145 serviceTemplate.setDescription("testing desc tosca service template");
146 serviceTemplate.setMetadata(metadata);
147 Map<String, ServiceTemplate> definitionsInput = new HashMap<>();
148 String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate);
149 definitionsInput.put(serviceTemplateFileName, serviceTemplate);
150 byte[] csarFile = toscaFileOutputServiceCSARImpl
151 .createOutputFile(new ToscaServiceModel(null, definitionsInput, serviceTemplateFileName),
155 String resultFileName = "resultFile.zip";
156 File file = new File(resultFileName);
157 try (FileOutputStream fos = new FileOutputStream(file)) {
161 try (ZipFile zipFile = new ZipFile(resultFileName)) {
163 Enumeration<? extends ZipEntry> entries = zipFile.entries();
166 while (entries.hasMoreElements()) {
168 entries.nextElement();
170 Assert.assertEquals(2, count);
173 Files.delete(Paths.get(file.getPath()));