445915f4f84127ca585f758e8a426cc45e2fcfb4
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / test / java / org / openecomp / sdc / tosca / services / impl / ToscaFileOutputServiceCsarImplTest.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.tosca.services.impl;
22
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;
30
31 import java.io.File;
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;
38 import java.util.Map;
39 import java.util.zip.ZipEntry;
40 import java.util.zip.ZipFile;
41
42 public class ToscaFileOutputServiceCsarImplTest {
43
44   @Test
45   public void testCreationMetaFile() {
46     ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCSARImpl =
47         new ToscaFileOutputServiceCsarImpl();
48     String createdMeta = toscaFileOutputServiceCSARImpl.createMetaFile("entryFile.yaml");
49     String expectedMeta =
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+", ""));
55   }
56
57   @Test
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");
69
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);
78
79     Map<String, ServiceTemplate> definitionsInput = new HashMap<>();
80     definitionsInput
81         .put(ToscaUtil.getServiceTemplateFileName(mainServiceTemplate), mainServiceTemplate);
82     definitionsInput.put(ToscaUtil.getServiceTemplateFileName(additionalServiceTemplate),
83         additionalServiceTemplate);
84
85
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());
93
94
95     FileContentHandler heatFiles = new FileContentHandler();
96     heatFiles.putAll(dummyHeatArtifacts);
97     Map<String, byte[]> licenseArtifacts = new HashMap<>();
98
99     FileContentHandler licenseArtifactsFiles = new FileContentHandler();
100
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());
107
108     licenseArtifactsFiles.putAll(licenseArtifacts);
109
110     byte[] csarFile = toscaFileOutputServiceCSARImpl.createOutputFile(
111         new ToscaServiceModel(heatFiles, definitionsInput,
112             ToscaUtil.getServiceTemplateFileName(mainServiceTemplate)), licenseArtifactsFiles);
113
114     File file = File.createTempFile("resultFile", "zip");
115     try (FileOutputStream fos = new FileOutputStream(file)) {
116       fos.write(csarFile);
117     }
118
119     try (ZipFile zipFile = new ZipFile(file)) {
120
121       Enumeration<? extends ZipEntry> entries = zipFile.entries();
122
123       int count = 0;
124       while (entries.hasMoreElements()) {
125         count++;
126         entries.nextElement();
127       }
128     }
129
130     Files.delete(Paths.get(file.getAbsolutePath()));
131   }
132
133   @Test
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),
150             null);
151
152
153     File file = File.createTempFile("resultFile", "zip");
154     try (FileOutputStream fos = new FileOutputStream(file)) {
155       fos.write(csarFile);
156     }
157
158     try (ZipFile zipFile = new ZipFile(file)) {
159
160       Enumeration<? extends ZipEntry> entries = zipFile.entries();
161
162       int count = 0;
163       while (entries.hasMoreElements()) {
164         count++;
165         entries.nextElement();
166       }
167       Assert.assertEquals(2, count);
168     }
169
170     Files.delete(Paths.get(file.getAbsolutePath()));
171   }
172 }