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.onap.sdc.tosca.datatypes.model.ServiceTemplate;
20 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
21 import org.openecomp.core.utilities.file.FileUtils;
22 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
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;
29 import java.nio.file.NotDirectoryException;
31 import java.util.zip.ZipEntry;
32 import java.util.zip.ZipInputStream;
34 import static org.junit.Assert.assertEquals;
36 public class BaseToscaEnrichmentTest {
38 protected String outputFilesPath;
40 public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
41 String globalServiceTemplatesPath,
42 String entryDefinitionServiceTemplate)
44 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
45 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
46 if (entryDefinitionServiceTemplate == null) {
47 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
50 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
51 if (globalServiceTemplatesPath != null) {
52 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
55 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
58 private static void loadServiceTemplates(String serviceTemplatesPath,
59 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
60 Map<String, ServiceTemplate> serviceTemplates)
62 URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
63 if (urlFile != null) {
64 File pathFile = new File(urlFile.getFile());
65 Collection<File> files = org.apache.commons.io.FileUtils.listFiles(pathFile, null, true);
67 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
69 throw new NotDirectoryException(serviceTemplatesPath);
72 throw new NotDirectoryException(serviceTemplatesPath);
76 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
77 Collection<File> files,
78 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
80 for (File file : files) {
81 try (InputStream yamlFile = new FileInputStream(file)) {
82 ServiceTemplate serviceTemplateFromYaml =
83 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
84 serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplateFromYaml), serviceTemplateFromYaml);
90 /*public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
91 String globalServiceTemplatesPath,
92 String entryDefinitionServiceTemplate)
94 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
95 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
96 if (entryDefinitionServiceTemplate == null) {
97 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
100 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
101 if (globalServiceTemplatesPath != null) {
102 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
105 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
108 private static void loadServiceTemplates(String serviceTemplatesPath,
109 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
110 Map<String, ServiceTemplate> serviceTemplates)
112 URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
113 if (urlFile != null) {
114 File pathFile = new File(urlFile.getFile());
115 File[] files = pathFile.listFiles();
117 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
119 throw new NotDirectoryException(serviceTemplatesPath);
122 throw new NotDirectoryException(serviceTemplatesPath);
126 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
128 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
130 for (File file : files) {
131 try (InputStream yamlFile = new FileInputStream(file)) {
132 ServiceTemplate serviceTemplateFromYaml =
133 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
134 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
137 } catch (IOException ignore) {
139 } catch (FileNotFoundException e) {
141 } catch (IOException e) {
147 void compareActualAndExpectedModel(ToscaServiceModel toscaServiceModel) throws IOException {
149 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
150 byte[] toscaActualFile = toscaFileOutputService.createOutputFile(toscaServiceModel, null);
152 URL url = BaseToscaEnrichmentTest.class.getResource(outputFilesPath);
153 Set<String> expectedResultFileNameSet = new HashSet<>();
154 Map<String, byte[]> expectedResultMap = new HashMap<>();
155 String path = url.getPath();
156 File pathFile = new File(path);
157 File[] files = pathFile.listFiles();
158 org.junit.Assert.assertNotNull("model is empty", files);
159 for (File expectedFile : files) {
160 expectedResultFileNameSet.add(expectedFile.getName());
161 try (FileInputStream input = new FileInputStream(expectedFile)) {
162 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
166 try (InputStream fis = new ByteArrayInputStream(toscaActualFile);
167 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
173 while ((entry = zis.getNextEntry()) != null) {
174 name = entry.getName()
175 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
176 if (expectedResultFileNameSet.contains(name)) {
177 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
178 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
179 assertEquals("difference in file: " + name, expected, actual);
181 expectedResultFileNameSet.remove(name);
184 if (expectedResultFileNameSet.isEmpty()) {
185 expectedResultFileNameSet.forEach(System.out::println);
188 assertEquals(0, expectedResultFileNameSet.size());