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 org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
20 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
21 import org.openecomp.core.enrichment.factory.EnrichmentManagerFactory;
22 import org.openecomp.core.utilities.file.FileUtils;
23 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
24 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
25 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
26 import org.testng.Assert;
27 import org.testng.annotations.Test;
31 import java.nio.file.NotDirectoryException;
32 import java.util.HashMap;
33 import java.util.HashSet;
36 import java.util.zip.ZipEntry;
37 import java.util.zip.ZipInputStream;
39 import static org.junit.Assert.assertEquals;
42 public class EnrichmentManagerImplTest {
44 private static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
45 String globalServiceTemplatesPath,
46 String entryDefinitionServiceTemplate)
48 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
49 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
50 if (entryDefinitionServiceTemplate == null) {
51 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
54 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
55 if (globalServiceTemplatesPath != null) {
56 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
59 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
62 private static void loadServiceTemplates(String serviceTemplatesPath,
63 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
64 Map<String, ServiceTemplate> serviceTemplates)
66 URL urlFile = EnrichmentManagerImplTest.class.getResource(serviceTemplatesPath);
67 if (urlFile != null) {
68 File pathFile = new File(urlFile.getFile());
69 File[] files = pathFile.listFiles();
71 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
73 throw new NotDirectoryException(serviceTemplatesPath);
76 throw new NotDirectoryException(serviceTemplatesPath);
80 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
82 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
84 for (File file : files) {
85 if (!file.getName().equals("CSR.zip") && !file.isDirectory()) {
86 try (InputStream yamlFile = new FileInputStream(file)) {
87 ServiceTemplate serviceTemplateFromYaml =
88 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
89 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
96 public void testEnrichmentManagerImpl() throws Exception {
98 EnrichmentManagerFactory.getInstance().createInterface() instanceof EnrichmentManagerImpl);
101 private File getToscaModelAsFile(ToscaServiceModel toscaServiceModel) throws IOException {
103 URL inputFilesUrl = EnrichmentManagerImplTest.class.getResource("/mock/enrich/input");
104 String path = inputFilesUrl.getPath();
107 File file = new File(path + "/" + "CSR.zip");
108 file.createNewFile();
110 try (FileOutputStream fos = new FileOutputStream(file))
113 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
114 fos.write(toscaFileOutputService.createOutputFile(toscaServiceModel, null));
120 protected void compareActualAndExpected(File actualFile, String expectedOutputPath)
123 URL url = EnrichmentManagerImplTest.class.getResource(expectedOutputPath);
124 Set<String> expectedResultFileNameSet = new HashSet<>();
125 Map<String, byte[]> expectedResultMap = new HashMap<>();
126 String path = url.getPath();
127 File pathFile = new File(path);
128 File[] files = pathFile.listFiles();
129 org.junit.Assert.assertNotNull("model is empty", files);
130 for (File expectedFile : files) {
131 expectedResultFileNameSet.add(expectedFile.getName());
132 try (FileInputStream input = new FileInputStream(expectedFile)) {
133 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
137 try (FileInputStream fis = new FileInputStream(actualFile);
138 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
144 while ((entry = zis.getNextEntry()) != null) {
146 name = entry.getName()
147 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
148 if (expectedResultFileNameSet.contains(name)) {
149 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
150 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
151 assertEquals("difference in file: " + name, expected, actual);
153 expectedResultFileNameSet.remove(name);
156 if (expectedResultFileNameSet.isEmpty()) {
157 expectedResultFileNameSet.forEach(System.out::println);
160 assertEquals(0, expectedResultFileNameSet.size());