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.onap.sdc.tosca.datatypes.model.ServiceTemplate;
28 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
29 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
30 import org.openecomp.sdc.tosca.services.ToscaUtil;
31 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
33 import java.io.BufferedInputStream;
34 import java.io.ByteArrayInputStream;
36 import java.io.FileInputStream;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39 import java.io.InputStream;
41 import java.nio.file.NotDirectoryException;
42 import java.util.Collection;
43 import java.util.HashMap;
44 import java.util.HashSet;
47 import java.util.zip.ZipEntry;
48 import java.util.zip.ZipInputStream;
50 import static org.junit.Assert.assertEquals;
52 public class BaseToscaEnrichmentTest {
54 protected String outputFilesPath;
56 private final static Logger log = (Logger) LoggerFactory.getLogger
57 (BaseToscaEnrichmentTest.class.getName());
59 public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
60 String globalServiceTemplatesPath,
61 String entryDefinitionServiceTemplate)
63 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
64 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
65 if (entryDefinitionServiceTemplate == null) {
66 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
69 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
70 if (globalServiceTemplatesPath != null) {
71 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
74 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
77 private static void loadServiceTemplates(String serviceTemplatesPath,
78 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
79 Map<String, ServiceTemplate> serviceTemplates)
81 URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
82 if (urlFile != null) {
83 File pathFile = new File(urlFile.getFile());
84 Collection<File> files = org.apache.commons.io.FileUtils.listFiles(pathFile, null, true);
86 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
88 throw new NotDirectoryException(serviceTemplatesPath);
91 throw new NotDirectoryException(serviceTemplatesPath);
95 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
96 Collection<File> files,
97 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
99 for (File file : files) {
100 try (InputStream yamlFile = new FileInputStream(file)) {
101 ServiceTemplate serviceTemplateFromYaml =
102 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
103 serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplateFromYaml), serviceTemplateFromYaml);
106 } catch (IOException ignore) {
107 log.debug("",ignore);
109 } catch (FileNotFoundException exception) {
111 } catch (IOException exception) {
118 /*public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
119 String globalServiceTemplatesPath,
120 String entryDefinitionServiceTemplate)
122 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
123 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
124 if (entryDefinitionServiceTemplate == null) {
125 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
128 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
129 if (globalServiceTemplatesPath != null) {
130 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
133 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
136 private static void loadServiceTemplates(String serviceTemplatesPath,
137 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
138 Map<String, ServiceTemplate> serviceTemplates)
140 URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
141 if (urlFile != null) {
142 File pathFile = new File(urlFile.getFile());
143 File[] files = pathFile.listFiles();
145 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
147 throw new NotDirectoryException(serviceTemplatesPath);
150 throw new NotDirectoryException(serviceTemplatesPath);
154 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
156 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
158 for (File file : files) {
159 try (InputStream yamlFile = new FileInputStream(file)) {
160 ServiceTemplate serviceTemplateFromYaml =
161 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
162 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
165 } catch (IOException ignore) {
167 } catch (FileNotFoundException e) {
169 } catch (IOException e) {
175 void compareActualAndExpectedModel(ToscaServiceModel toscaServiceModel) throws IOException {
177 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
178 byte[] toscaActualFile = toscaFileOutputService.createOutputFile(toscaServiceModel, null);
180 URL url = BaseToscaEnrichmentTest.class.getResource(outputFilesPath);
181 Set<String> expectedResultFileNameSet = new HashSet<>();
182 Map<String, byte[]> expectedResultMap = new HashMap<>();
183 String path = url.getPath();
184 File pathFile = new File(path);
185 File[] files = pathFile.listFiles();
186 org.junit.Assert.assertNotNull("model is empty", files);
187 for (File expectedFile : files) {
188 expectedResultFileNameSet.add(expectedFile.getName());
189 try (FileInputStream input = new FileInputStream(expectedFile)) {
190 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
194 try (InputStream fis = new ByteArrayInputStream(toscaActualFile);
195 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
201 while ((entry = zis.getNextEntry()) != null) {
202 name = entry.getName()
203 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
204 if (expectedResultFileNameSet.contains(name)) {
205 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
206 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
207 assertEquals("difference in file: " + name, expected, actual);
209 expectedResultFileNameSet.remove(name);
212 if (expectedResultFileNameSet.isEmpty()) {
213 expectedResultFileNameSet.forEach(System.out::println);
216 assertEquals(0, expectedResultFileNameSet.size());