2 * Copyright © 2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.enrichment.impl.tosca;
19 import org.openecomp.core.utilities.file.FileUtils;
20 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
21 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
22 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
23 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
24 import org.openecomp.sdc.tosca.services.ToscaUtil;
25 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
27 import java.io.BufferedInputStream;
28 import java.io.ByteArrayInputStream;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
34 import java.nio.file.NotDirectoryException;
35 import java.util.Collection;
36 import java.util.HashMap;
37 import java.util.HashSet;
40 import java.util.zip.ZipEntry;
41 import java.util.zip.ZipInputStream;
43 import static org.junit.Assert.assertEquals;
45 public class BaseToscaEnrichmentTest {
47 protected String outputFilesPath;
49 public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
50 String globalServiceTemplatesPath,
51 String entryDefinitionServiceTemplate)
53 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
54 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
55 if (entryDefinitionServiceTemplate == null) {
56 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
59 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
60 if (globalServiceTemplatesPath != null) {
61 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
64 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
67 private static void loadServiceTemplates(String serviceTemplatesPath,
68 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
69 Map<String, ServiceTemplate> serviceTemplates)
71 URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
72 if (urlFile != null) {
73 File pathFile = new File(urlFile.getFile());
74 Collection<File> files = org.apache.commons.io.FileUtils.listFiles(pathFile, null, true);
76 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
78 throw new NotDirectoryException(serviceTemplatesPath);
81 throw new NotDirectoryException(serviceTemplatesPath);
85 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
86 Collection<File> files,
87 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
89 for (File file : files) {
90 try (InputStream yamlFile = new FileInputStream(file)) {
91 ServiceTemplate serviceTemplateFromYaml =
92 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
93 serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplateFromYaml), serviceTemplateFromYaml);
99 /*public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
100 String globalServiceTemplatesPath,
101 String entryDefinitionServiceTemplate)
103 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
104 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
105 if (entryDefinitionServiceTemplate == null) {
106 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
109 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
110 if (globalServiceTemplatesPath != null) {
111 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
114 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
117 private static void loadServiceTemplates(String serviceTemplatesPath,
118 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
119 Map<String, ServiceTemplate> serviceTemplates)
121 URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
122 if (urlFile != null) {
123 File pathFile = new File(urlFile.getFile());
124 File[] files = pathFile.listFiles();
126 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
128 throw new NotDirectoryException(serviceTemplatesPath);
131 throw new NotDirectoryException(serviceTemplatesPath);
135 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
137 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
139 for (File file : files) {
140 try (InputStream yamlFile = new FileInputStream(file)) {
141 ServiceTemplate serviceTemplateFromYaml =
142 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
143 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
146 } catch (IOException ignore) {
148 } catch (FileNotFoundException e) {
150 } catch (IOException e) {
156 void compareActualAndExpectedModel(ToscaServiceModel toscaServiceModel) throws IOException {
158 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
159 byte[] toscaActualFile = toscaFileOutputService.createOutputFile(toscaServiceModel, null);
161 URL url = BaseToscaEnrichmentTest.class.getResource(outputFilesPath);
162 Set<String> expectedResultFileNameSet = new HashSet<>();
163 Map<String, byte[]> expectedResultMap = new HashMap<>();
164 String path = url.getPath();
165 File pathFile = new File(path);
166 File[] files = pathFile.listFiles();
167 org.junit.Assert.assertNotNull("model is empty", files);
168 for (File expectedFile : files) {
169 expectedResultFileNameSet.add(expectedFile.getName());
170 try (FileInputStream input = new FileInputStream(expectedFile)) {
171 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
175 try (InputStream fis = new ByteArrayInputStream(toscaActualFile);
176 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
182 while ((entry = zis.getNextEntry()) != null) {
183 name = entry.getName()
184 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
185 if (expectedResultFileNameSet.contains(name)) {
186 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
187 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
188 assertEquals("difference in file: " + name, expected, actual);
190 expectedResultFileNameSet.remove(name);
193 if (expectedResultFileNameSet.isEmpty()) {
194 expectedResultFileNameSet.forEach(System.out::println);
197 assertEquals(0, expectedResultFileNameSet.size());