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.logging.api.Logger;
25 import org.openecomp.sdc.logging.api.LoggerFactory;
26 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
27 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
28 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
29 import org.openecomp.sdc.tosca.services.ToscaUtil;
30 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
31 import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
35 import java.nio.file.NotDirectoryException;
36 import java.util.Collection;
37 import java.util.HashMap;
38 import java.util.HashSet;
41 import java.util.zip.ZipEntry;
42 import java.util.zip.ZipInputStream;
44 import static org.junit.Assert.assertEquals;
46 public class BaseToscaEnrichmentTest {
48 protected String outputFilesPath;
50 private final static Logger log = (Logger) LoggerFactory.getLogger
51 (BaseToscaEnrichmentTest.class.getName());
53 public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
54 String globalServiceTemplatesPath,
55 String entryDefinitionServiceTemplate)
57 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
58 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
59 if (entryDefinitionServiceTemplate == null) {
60 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
63 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
64 if (globalServiceTemplatesPath != null) {
65 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
68 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
71 private static void loadServiceTemplates(String serviceTemplatesPath,
72 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
73 Map<String, ServiceTemplate> serviceTemplates)
75 URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
76 if (urlFile != null) {
77 File pathFile = new File(urlFile.getFile());
78 Collection<File> files = org.apache.commons.io.FileUtils.listFiles(pathFile, null, true);
80 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
82 throw new NotDirectoryException(serviceTemplatesPath);
85 throw new NotDirectoryException(serviceTemplatesPath);
89 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
90 Collection<File> files,
91 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
93 for (File file : files) {
94 try (InputStream yamlFile = new FileInputStream(file)) {
95 ServiceTemplate serviceTemplateFromYaml =
96 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
97 serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplateFromYaml), serviceTemplateFromYaml);
100 } catch (IOException ignore) {
101 log.debug("",ignore);
103 } catch (FileNotFoundException exception) {
105 } catch (IOException exception) {
112 /*public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
113 String globalServiceTemplatesPath,
114 String entryDefinitionServiceTemplate)
116 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
117 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
118 if (entryDefinitionServiceTemplate == null) {
119 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
122 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
123 if (globalServiceTemplatesPath != null) {
124 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
127 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
130 private static void loadServiceTemplates(String serviceTemplatesPath,
131 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
132 Map<String, ServiceTemplate> serviceTemplates)
134 URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
135 if (urlFile != null) {
136 File pathFile = new File(urlFile.getFile());
137 File[] files = pathFile.listFiles();
139 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
141 throw new NotDirectoryException(serviceTemplatesPath);
144 throw new NotDirectoryException(serviceTemplatesPath);
148 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
150 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
152 for (File file : files) {
153 try (InputStream yamlFile = new FileInputStream(file)) {
154 ServiceTemplate serviceTemplateFromYaml =
155 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
156 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
159 } catch (IOException ignore) {
161 } catch (FileNotFoundException e) {
163 } catch (IOException e) {
169 void compareActualAndExpectedModel(ToscaServiceModel toscaServiceModel) throws IOException {
171 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
172 byte[] toscaActualFile = toscaFileOutputService.createOutputFile(toscaServiceModel, null);
174 URL url = BaseToscaEnrichmentTest.class.getResource(outputFilesPath);
175 Set<String> expectedResultFileNameSet = new HashSet<>();
176 Map<String, byte[]> expectedResultMap = new HashMap<>();
177 String path = url.getPath();
178 File pathFile = new File(path);
179 File[] files = pathFile.listFiles();
180 org.junit.Assert.assertNotNull("model is empty", files);
181 for (File expectedFile : files) {
182 expectedResultFileNameSet.add(expectedFile.getName());
183 try (FileInputStream input = new FileInputStream(expectedFile)) {
184 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
188 try (InputStream fis = new ByteArrayInputStream(toscaActualFile);
189 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
195 while ((entry = zis.getNextEntry()) != null) {
196 name = entry.getName()
197 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
198 if (expectedResultFileNameSet.contains(name)) {
199 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
200 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
201 assertEquals("difference in file: " + name, expected, actual);
203 expectedResultFileNameSet.remove(name);
206 if (expectedResultFileNameSet.isEmpty()) {
207 expectedResultFileNameSet.forEach(System.out::println);
210 assertEquals(0, expectedResultFileNameSet.size());