2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.enrichment.impl.tosca;
23 import org.openecomp.core.utilities.file.FileUtils;
24 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
25 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
26 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
27 import org.openecomp.sdc.tosca.services.ToscaUtil;
28 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
29 import org.openecomp.sdc.tosca.services.yamlutil.ToscaExtensionYamlUtil;
33 import java.nio.file.NotDirectoryException;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.HashSet;
39 import java.util.zip.ZipEntry;
40 import java.util.zip.ZipInputStream;
42 import static org.junit.Assert.assertEquals;
44 public class BaseToscaEnrichmentTest {
46 protected String outputFilesPath;
48 public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
49 String globalServiceTemplatesPath,
50 String entryDefinitionServiceTemplate)
52 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
53 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
54 if (entryDefinitionServiceTemplate == null) {
55 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
58 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
59 if (globalServiceTemplatesPath != null) {
60 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
63 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
66 private static void loadServiceTemplates(String serviceTemplatesPath,
67 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
68 Map<String, ServiceTemplate> serviceTemplates)
70 URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
71 if (urlFile != null) {
72 File pathFile = new File(urlFile.getFile());
73 Collection<File> files = org.apache.commons.io.FileUtils.listFiles(pathFile, null, true);
75 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
77 throw new NotDirectoryException(serviceTemplatesPath);
80 throw new NotDirectoryException(serviceTemplatesPath);
84 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
85 Collection<File> files,
86 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
88 for (File file : files) {
89 try (InputStream yamlFile = new FileInputStream(file)) {
90 ServiceTemplate serviceTemplateFromYaml =
91 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
92 serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplateFromYaml), serviceTemplateFromYaml);
95 } catch (IOException ignore) {
97 } catch (FileNotFoundException exception) {
99 } catch (IOException exception) {
106 /*public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
107 String globalServiceTemplatesPath,
108 String entryDefinitionServiceTemplate)
110 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
111 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
112 if (entryDefinitionServiceTemplate == null) {
113 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
116 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
117 if (globalServiceTemplatesPath != null) {
118 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
121 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
124 private static void loadServiceTemplates(String serviceTemplatesPath,
125 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
126 Map<String, ServiceTemplate> serviceTemplates)
128 URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
129 if (urlFile != null) {
130 File pathFile = new File(urlFile.getFile());
131 File[] files = pathFile.listFiles();
133 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
135 throw new NotDirectoryException(serviceTemplatesPath);
138 throw new NotDirectoryException(serviceTemplatesPath);
142 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
144 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
146 for (File file : files) {
147 try (InputStream yamlFile = new FileInputStream(file)) {
148 ServiceTemplate serviceTemplateFromYaml =
149 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
150 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
153 } catch (IOException ignore) {
155 } catch (FileNotFoundException e) {
157 } catch (IOException e) {
163 void compareActualAndExpectedModel(ToscaServiceModel toscaServiceModel) throws IOException {
165 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
166 byte[] toscaActualFile = toscaFileOutputService.createOutputFile(toscaServiceModel, null);
168 URL url = BaseToscaEnrichmentTest.class.getResource(outputFilesPath);
169 Set<String> expectedResultFileNameSet = new HashSet<>();
170 Map<String, byte[]> expectedResultMap = new HashMap<>();
171 String path = url.getPath();
172 File pathFile = new File(path);
173 File[] files = pathFile.listFiles();
174 org.junit.Assert.assertNotNull("model is empty", files);
175 for (File expectedFile : files) {
176 expectedResultFileNameSet.add(expectedFile.getName());
177 try (FileInputStream input = new FileInputStream(expectedFile)) {
178 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
182 try (InputStream fis = new ByteArrayInputStream(toscaActualFile);
183 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
189 while ((entry = zis.getNextEntry()) != null) {
190 name = entry.getName()
191 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
192 if (expectedResultFileNameSet.contains(name)) {
193 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
194 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
195 assertEquals("difference in file: " + name, expected, actual);
197 expectedResultFileNameSet.remove(name);
200 if (expectedResultFileNameSet.isEmpty()) {
201 expectedResultFileNameSet.forEach(System.out::println);
204 assertEquals(0, expectedResultFileNameSet.size());