0910684f3aed233e7d5f9a083258e75e3f1a6a62
[sdc.git] /
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     String resultFileName = "resultFile.zip";
115     File file = new File(resultFileName);
116     try (FileOutputStream fos = new FileOutputStream(file)) {
117       fos.write(csarFile);
118     }
119
120     try (ZipFile zipFile = new ZipFile(resultFileName)) {
121
122       Enumeration<? extends ZipEntry> entries = zipFile.entries();
123
124       int count = 0;
125       while (entries.hasMoreElements()) {
126         count++;
127         entries.nextElement();
128       }
129       Assert.assertEquals(7, count);
130     }
131
132     Files.delete(Paths.get(file.getPath()));
133   }
134
135   @Test
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),
152             null);
153
154
155     String resultFileName = "resultFile.zip";
156     File file = new File(resultFileName);
157     try (FileOutputStream fos = new FileOutputStream(file)) {
158       fos.write(csarFile);
159     }
160
161     try (ZipFile zipFile = new ZipFile(resultFileName)) {
162
163       Enumeration<? extends ZipEntry> entries = zipFile.entries();
164
165       int count = 0;
166       while (entries.hasMoreElements()) {
167         count++;
168         entries.nextElement();
169       }
170       Assert.assertEquals(2, count);
171     }
172
173     Files.delete(Paths.get(file.getPath()));
174   }
175 }