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;
23 import org.openecomp.core.enrichment.factory.EnrichmentManagerFactory;
24 import org.openecomp.core.utilities.file.FileUtils;
25 import org.openecomp.sdc.logging.api.Logger;
26 import org.openecomp.sdc.logging.api.LoggerFactory;
27 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
28 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
29 import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
30 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
31 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
32 import org.testng.Assert;
33 import org.testng.annotations.Test;
35 import java.io.BufferedInputStream;
37 import java.io.FileInputStream;
38 import java.io.FileNotFoundException;
39 import java.io.FileOutputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
43 import java.nio.file.NotDirectoryException;
44 import java.util.HashMap;
45 import java.util.HashSet;
48 import java.util.zip.ZipEntry;
49 import java.util.zip.ZipInputStream;
51 import static org.junit.Assert.assertEquals;
54 public class EnrichmentManagerImplTest {
56 private final static Logger log = (Logger) LoggerFactory.getLogger
57 (EnrichmentManagerImplTest.class.getName());
60 private static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
61 String globalServiceTemplatesPath,
62 String entryDefinitionServiceTemplate)
64 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
65 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
66 if (entryDefinitionServiceTemplate == null) {
67 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
70 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
71 if (globalServiceTemplatesPath != null) {
72 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
75 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
78 private static void loadServiceTemplates(String serviceTemplatesPath,
79 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
80 Map<String, ServiceTemplate> serviceTemplates)
82 URL urlFile = EnrichmentManagerImplTest.class.getResource(serviceTemplatesPath);
83 if (urlFile != null) {
84 File pathFile = new File(urlFile.getFile());
85 File[] files = pathFile.listFiles();
87 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
89 throw new NotDirectoryException(serviceTemplatesPath);
92 throw new NotDirectoryException(serviceTemplatesPath);
96 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
98 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
100 for (File file : files) {
101 if (!file.getName().equals("CSR.zip") && !file.isDirectory()) {
102 try (InputStream yamlFile = new FileInputStream(file)) {
103 ServiceTemplate serviceTemplateFromYaml =
104 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
105 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
108 } catch (IOException ignore) {
109 log.debug("",ignore);
111 } catch (FileNotFoundException e) {
113 } catch (IOException e) {
121 public void testEnrichmentManagerImpl() throws Exception {
123 EnrichmentManagerFactory.getInstance().createInterface() instanceof EnrichmentManagerImpl);
126 private File getToscaModelAsFile(ToscaServiceModel toscaServiceModel) throws IOException {
128 URL inputFilesUrl = EnrichmentManagerImplTest.class.getResource("/mock/enrich/input");
129 String path = inputFilesUrl.getPath();
132 File file = new File(path + "/" + "CSR.zip");
133 file.createNewFile();
135 try (FileOutputStream fos = new FileOutputStream(file))
138 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
139 fos.write(toscaFileOutputService.createOutputFile(toscaServiceModel, null));
145 protected void compareActualAndExpected(File actualFile, String expectedOutputPath)
148 URL url = EnrichmentManagerImplTest.class.getResource(expectedOutputPath);
149 Set<String> expectedResultFileNameSet = new HashSet<>();
150 Map<String, byte[]> expectedResultMap = new HashMap<>();
151 String path = url.getPath();
152 File pathFile = new File(path);
153 File[] files = pathFile.listFiles();
154 org.junit.Assert.assertNotNull("model is empty", files);
155 for (File expectedFile : files) {
156 expectedResultFileNameSet.add(expectedFile.getName());
157 try (FileInputStream input = new FileInputStream(expectedFile)) {
158 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
162 try (FileInputStream fis = new FileInputStream(actualFile);
163 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
169 while ((entry = zis.getNextEntry()) != null) {
171 name = entry.getName()
172 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
173 if (expectedResultFileNameSet.contains(name)) {
174 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
175 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
176 assertEquals("difference in file: " + name, expected, actual);
178 expectedResultFileNameSet.remove(name);
181 if (expectedResultFileNameSet.isEmpty()) {
182 expectedResultFileNameSet.forEach(System.out::println);
185 assertEquals(0, expectedResultFileNameSet.size());