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 static org.junit.Assert.assertEquals;
25 import org.openecomp.core.enrichment.api.EnrichmentManager;
26 import org.openecomp.core.enrichment.factory.EnrichmentManagerFactory;
27 import org.openecomp.core.utilities.file.FileUtils;
28 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
29 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
30 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
31 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
32 import org.openecomp.sdc.tosca.services.yamlutil.ToscaExtensionYamlUtil;
33 import org.openecomp.sdc.versioning.dao.types.Version;
34 import org.testng.Assert;
35 import org.testng.annotations.Test;
37 import java.io.BufferedInputStream;
39 import java.io.FileInputStream;
40 import java.io.FileNotFoundException;
41 import java.io.FileOutputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
45 import java.nio.file.NotDirectoryException;
46 import java.util.HashMap;
47 import java.util.HashSet;
50 import java.util.zip.ZipEntry;
51 import java.util.zip.ZipInputStream;
54 public class EnrichmentManagerImplTest {
57 private static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
58 String globalServiceTemplatesPath,
59 String entryDefinitionServiceTemplate)
61 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
62 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
63 if (entryDefinitionServiceTemplate == null) {
64 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
67 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
68 if (globalServiceTemplatesPath != null) {
69 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
72 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
75 private static void loadServiceTemplates(String serviceTemplatesPath,
76 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
77 Map<String, ServiceTemplate> serviceTemplates)
79 URL urlFile = EnrichmentManagerImplTest.class.getResource(serviceTemplatesPath);
80 if (urlFile != null) {
81 File pathFile = new File(urlFile.getFile());
82 File[] files = pathFile.listFiles();
84 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
86 throw new NotDirectoryException(serviceTemplatesPath);
89 throw new NotDirectoryException(serviceTemplatesPath);
93 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
95 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
97 for (File file : files) {
98 if (!file.getName().equals("CSR.zip") && !file.isDirectory()) {
99 try (InputStream yamlFile = new FileInputStream(file)) {
100 ServiceTemplate serviceTemplateFromYaml =
101 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
102 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
105 } catch (IOException ignore) {
107 } catch (FileNotFoundException e) {
109 } catch (IOException e) {
117 public void testEnrichmentManagerImpl() throws Exception {
119 EnrichmentManagerFactory.getInstance().createInterface() instanceof EnrichmentManagerImpl);
122 private File getToscaModelAsFile(ToscaServiceModel toscaServiceModel) throws IOException {
124 URL inputFilesUrl = EnrichmentManagerImplTest.class.getResource("/mock/enrich/input");
125 String path = inputFilesUrl.getPath();
128 File file = new File(path + "/" + "CSR.zip");
129 file.createNewFile();
131 try (FileOutputStream fos = new FileOutputStream(file))
134 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
135 fos.write(toscaFileOutputService.createOutputFile(toscaServiceModel, null));
141 protected void compareActualAndExpected(File actualFile, String expectedOutputPath)
144 URL url = EnrichmentManagerImplTest.class.getResource(expectedOutputPath);
145 Set<String> expectedResultFileNameSet = new HashSet<>();
146 Map<String, byte[]> expectedResultMap = new HashMap<>();
147 String path = url.getPath();
148 File pathFile = new File(path);
149 File[] files = pathFile.listFiles();
150 org.junit.Assert.assertNotNull("model is empty", files);
151 for (File expectedFile : files) {
152 expectedResultFileNameSet.add(expectedFile.getName());
153 try (FileInputStream input = new FileInputStream(expectedFile)) {
154 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
158 try (FileInputStream fis = new FileInputStream(actualFile);
159 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
165 while ((entry = zis.getNextEntry()) != null) {
167 name = entry.getName()
168 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
169 if (expectedResultFileNameSet.contains(name)) {
170 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
171 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
172 assertEquals("difference in file: " + name, expected, actual);
174 expectedResultFileNameSet.remove(name);
177 if (expectedResultFileNameSet.isEmpty()) {
178 expectedResultFileNameSet.forEach(System.out::println);
181 assertEquals(0, expectedResultFileNameSet.size());