Improve testing stability
[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 java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 import java.util.Collections;
29 import java.util.Enumeration;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipFile;
34
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.InjectMocks;
39 import org.mockito.MockitoAnnotations;
40 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
41 import org.openecomp.core.utilities.file.FileContentHandler;
42 import org.openecomp.sdc.common.errors.CoreException;
43 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
44 import org.openecomp.sdc.tosca.services.ToscaConstants;
45 import org.openecomp.sdc.tosca.services.ToscaUtil;
46
47 public class ToscaFileOutputServiceCsarImplTest {
48
49     @InjectMocks
50     private ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCsarImpl;
51
52     @Before
53     public void init() {
54         MockitoAnnotations.openMocks(this);
55     }
56
57     @Test
58     public void testCreationMetaFile() {
59         String createdMeta = toscaFileOutputServiceCsarImpl.createMetaFile("entryFile.yaml");
60         String expectedMeta =
61                 "TOSCA-Meta-File-Version: 1.0\n" +
62                         "CSAR-Version: 1.1\n" +
63                         "Created-By: ASDC Onboarding portal\n" +
64                         "Entry-Definitions: Definitions" + File.separator + "entryFile.yaml";
65         Assert.assertEquals(createdMeta.replaceAll("\\s+", ""), expectedMeta.replaceAll("\\s+", ""));
66     }
67
68     @Test
69     public void testCSARFileCreationWithExternalArtifacts() throws IOException {
70         ToscaFileOutputServiceCsarImpl toscaFileOutputServiceCSARImpl =
71                 new ToscaFileOutputServiceCsarImpl();
72         ServiceTemplate mainServiceTemplate = new ServiceTemplate();
73         Map<String, String> metadata1 = new HashMap<>();
74         metadata1.put("Template_author", "OPENECOMP");
75         metadata1.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "ST1");
76         metadata1.put("Template_version", "1.0.0");
77         mainServiceTemplate.setMetadata(metadata1);
78         mainServiceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0");
79         mainServiceTemplate.setDescription("testing desc tosca service template");
80
81         ServiceTemplate additionalServiceTemplate = new ServiceTemplate();
82         Map<String, String> metadata2 = new HashMap<>();
83         metadata2.put("Template_author", "OPENECOMP");
84         metadata2.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "ST2");
85         metadata2.put("Template_version", "1.0.0");
86         additionalServiceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0");
87         additionalServiceTemplate.setDescription("testing desc tosca service template");
88         additionalServiceTemplate.setMetadata(metadata2);
89
90         Map<String, ServiceTemplate> definitionsInput = new HashMap<>();
91         definitionsInput
92                 .put(ToscaUtil.getServiceTemplateFileName(mainServiceTemplate), mainServiceTemplate);
93         definitionsInput.put(ToscaUtil.getServiceTemplateFileName(additionalServiceTemplate),
94                 additionalServiceTemplate);
95
96
97         Map<String, byte[]> dummyHeatArtifacts = new HashMap<>();
98         String file1Content = "this is file number 1";
99         String file2Content = "this is file number 2";
100         String file1 = "file1.xml";
101         dummyHeatArtifacts.put(file1, file1Content.getBytes());
102         String file2 = "file2.yml";
103         dummyHeatArtifacts.put(file2, file2Content.getBytes());
104
105
106         FileContentHandler heatFiles = new FileContentHandler();
107         heatFiles.setFiles(dummyHeatArtifacts);
108         Map<String, byte[]> licenseArtifacts = new HashMap<>();
109
110         FileContentHandler licenseArtifactsFiles = new FileContentHandler();
111
112         licenseArtifacts.put(
113                 ToscaFileOutputServiceCsarImpl.EXTERNAL_ARTIFACTS_FOLDER_NAME + File.separator +
114                         "license-file-1.xml", file1Content.getBytes());
115         licenseArtifacts.put(
116                 ToscaFileOutputServiceCsarImpl.EXTERNAL_ARTIFACTS_FOLDER_NAME + File.separator +
117                         "license-file-2.xml", file1Content.getBytes());
118
119         licenseArtifactsFiles.setFiles(licenseArtifacts);
120
121         byte[] csarFile = toscaFileOutputServiceCSARImpl.createOutputFile(
122                 new ToscaServiceModel(heatFiles, definitionsInput,
123                         ToscaUtil.getServiceTemplateFileName(mainServiceTemplate)), licenseArtifactsFiles);
124
125         File file = File.createTempFile("resultFile", "zip");
126         try (FileOutputStream fos = new FileOutputStream(file)) {
127             fos.write(csarFile);
128         }
129
130         try (ZipFile zipFile = new ZipFile(file)) {
131
132             Enumeration<? extends ZipEntry> entries = zipFile.entries();
133
134             while (entries.hasMoreElements()) {
135                 entries.nextElement();
136             }
137         }
138
139         Files.delete(Paths.get(file.getAbsolutePath()));
140     }
141
142     @Test
143     public void testCSARFileCreation_noArtifacts() throws IOException {
144         ServiceTemplate serviceTemplate = new ServiceTemplate();
145         Map<String, String> metadata = new HashMap<>();
146         metadata.put("Template_author", "OPENECOMP");
147         metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "Test");
148         metadata.put("Template_version", "1.0.0");
149         serviceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0");
150         serviceTemplate.setDescription("testing desc tosca service template");
151         serviceTemplate.setMetadata(metadata);
152         Map<String, ServiceTemplate> definitionsInput = new HashMap<>();
153         String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate);
154         definitionsInput.put(serviceTemplateFileName, serviceTemplate);
155         byte[] csarFile = toscaFileOutputServiceCsarImpl
156                 .createOutputFile(new ToscaServiceModel(null, definitionsInput, serviceTemplateFileName),
157                         null);
158
159
160         File file = File.createTempFile("resultFile", "zip");
161         try (FileOutputStream fos = new FileOutputStream(file)) {
162             fos.write(csarFile);
163         }
164
165         try (ZipFile zipFile = new ZipFile(file)) {
166
167             Enumeration<? extends ZipEntry> entries = zipFile.entries();
168
169             int count = 0;
170             while (entries.hasMoreElements()) {
171                 count++;
172                 entries.nextElement();
173             }
174             Assert.assertEquals(2, count);
175         }
176
177         Files.delete(Paths.get(file.getAbsolutePath()));
178     }
179
180     @Test(expected = CoreException.class)
181     public void testCreateOutputFileEntryDefinitionServiceTemplateIsNull() {
182         ToscaServiceModel toscaServiceModel = new ToscaServiceModel();
183         toscaServiceModel.setServiceTemplates(Collections.emptyMap());
184
185         toscaFileOutputServiceCsarImpl.createOutputFile(toscaServiceModel, null);
186     }
187
188     @Test
189     public void testGetArtifactsFolderName() {
190         Assert.assertEquals("Artifacts", toscaFileOutputServiceCsarImpl.getArtifactsFolderName());
191     }
192 }