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.openecomp.core.enrichment.factory.EnrichmentManagerFactory;
20 import org.openecomp.core.utilities.file.FileUtils;
21 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
22 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
23 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
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;
29 import java.io.BufferedInputStream;
31 import java.io.FileInputStream;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
36 import java.nio.file.NotDirectoryException;
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;
47 public class EnrichmentManagerImplTest {
49 private static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
50 String globalServiceTemplatesPath,
51 String entryDefinitionServiceTemplate)
53 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
54 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
55 if (entryDefinitionServiceTemplate == null) {
56 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
59 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
60 if (globalServiceTemplatesPath != null) {
61 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
64 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
67 private static void loadServiceTemplates(String serviceTemplatesPath,
68 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
69 Map<String, ServiceTemplate> serviceTemplates)
71 URL urlFile = EnrichmentManagerImplTest.class.getResource(serviceTemplatesPath);
72 if (urlFile != null) {
73 File pathFile = new File(urlFile.getFile());
74 File[] files = pathFile.listFiles();
76 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
78 throw new NotDirectoryException(serviceTemplatesPath);
81 throw new NotDirectoryException(serviceTemplatesPath);
85 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
87 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
89 for (File file : files) {
90 if (!file.getName().equals("CSR.zip") && !file.isDirectory()) {
91 try (InputStream yamlFile = new FileInputStream(file)) {
92 ServiceTemplate serviceTemplateFromYaml =
93 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
94 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
101 public void testEnrichmentManagerImpl() throws Exception {
103 EnrichmentManagerFactory.getInstance().createInterface() instanceof EnrichmentManagerImpl);
106 private File getToscaModelAsFile(ToscaServiceModel toscaServiceModel) throws IOException {
108 URL inputFilesUrl = EnrichmentManagerImplTest.class.getResource("/mock/enrich/input");
109 String path = inputFilesUrl.getPath();
112 File file = new File(path + "/" + "CSR.zip");
113 file.createNewFile();
115 try (FileOutputStream fos = new FileOutputStream(file))
118 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
119 fos.write(toscaFileOutputService.createOutputFile(toscaServiceModel, null));
125 protected void compareActualAndExpected(File actualFile, String expectedOutputPath)
128 URL url = EnrichmentManagerImplTest.class.getResource(expectedOutputPath);
129 Set<String> expectedResultFileNameSet = new HashSet<>();
130 Map<String, byte[]> expectedResultMap = new HashMap<>();
131 String path = url.getPath();
132 File pathFile = new File(path);
133 File[] files = pathFile.listFiles();
134 org.junit.Assert.assertNotNull("model is empty", files);
135 for (File expectedFile : files) {
136 expectedResultFileNameSet.add(expectedFile.getName());
137 try (FileInputStream input = new FileInputStream(expectedFile)) {
138 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
142 try (FileInputStream fis = new FileInputStream(actualFile);
143 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
149 while ((entry = zis.getNextEntry()) != null) {
151 name = entry.getName()
152 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
153 if (expectedResultFileNameSet.contains(name)) {
154 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
155 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
156 assertEquals("difference in file: " + name, expected, actual);
158 expectedResultFileNameSet.remove(name);
161 if (expectedResultFileNameSet.isEmpty()) {
162 expectedResultFileNameSet.forEach(System.out::println);
165 assertEquals(0, expectedResultFileNameSet.size());