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;
19 import static org.junit.Assert.assertEquals;
21 import java.io.BufferedInputStream;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
28 import java.nio.file.NotDirectoryException;
29 import java.util.HashMap;
30 import java.util.HashSet;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipInputStream;
35 import org.junit.Assert;
36 import org.junit.Test;
37 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
38 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
39 import org.openecomp.core.enrichment.factory.EnrichmentManagerFactory;
40 import org.openecomp.core.utilities.file.FileUtils;
41 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
42 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
43 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
46 public class EnrichmentManagerImplTest {
48 private 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 = EnrichmentManagerImplTest.class.getResource(serviceTemplatesPath);
71 if (urlFile != null) {
72 File pathFile = new File(urlFile.getFile());
73 File[] files = pathFile.listFiles();
75 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
77 throw new NotDirectoryException(serviceTemplatesPath);
80 throw new NotDirectoryException(serviceTemplatesPath);
84 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
86 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
88 for (File file : files) {
89 if (!file.getName().equals("CSR.zip") && !file.isDirectory()) {
90 try (InputStream yamlFile = new FileInputStream(file)) {
91 ServiceTemplate serviceTemplateFromYaml =
92 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
93 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
100 public void testEnrichmentManagerImpl() throws Exception {
102 EnrichmentManagerFactory.getInstance().createInterface() instanceof EnrichmentManagerImpl);
105 private File getToscaModelAsFile(ToscaServiceModel toscaServiceModel) throws IOException {
107 URL inputFilesUrl = EnrichmentManagerImplTest.class.getResource("/mock/enrich/input");
108 String path = inputFilesUrl.getPath();
111 File file = new File(path + "/" + "CSR.zip");
112 file.createNewFile();
114 try (FileOutputStream fos = new FileOutputStream(file))
117 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
118 fos.write(toscaFileOutputService.createOutputFile(toscaServiceModel, null));
124 protected void compareActualAndExpected(File actualFile, String expectedOutputPath)
127 URL url = EnrichmentManagerImplTest.class.getResource(expectedOutputPath);
128 Set<String> expectedResultFileNameSet = new HashSet<>();
129 Map<String, byte[]> expectedResultMap = new HashMap<>();
130 String path = url.getPath();
131 File pathFile = new File(path);
132 File[] files = pathFile.listFiles();
133 org.junit.Assert.assertNotNull("model is empty", files);
134 for (File expectedFile : files) {
135 expectedResultFileNameSet.add(expectedFile.getName());
136 try (FileInputStream input = new FileInputStream(expectedFile)) {
137 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
141 try (FileInputStream fis = new FileInputStream(actualFile);
142 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
148 while ((entry = zis.getNextEntry()) != null) {
150 name = entry.getName()
151 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
152 if (expectedResultFileNameSet.contains(name)) {
153 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
154 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
155 assertEquals("difference in file: " + name, expected, actual);
157 expectedResultFileNameSet.remove(name);
160 if (expectedResultFileNameSet.isEmpty()) {
161 expectedResultFileNameSet.forEach(System.out::println);
164 assertEquals(0, expectedResultFileNameSet.size());