2 * Copyright © 2016-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.translator.services.heattotosca.impl.resourcetranslation;
19 import static org.junit.Assert.assertEquals;
21 import org.apache.commons.collections4.MapUtils;
22 import org.junit.AfterClass;
23 import org.junit.Assert;
24 import org.junit.BeforeClass;
25 import org.openecomp.core.translator.api.HeatToToscaTranslator;
26 import org.openecomp.core.translator.datatypes.TranslatorOutput;
27 import org.openecomp.core.translator.factory.HeatToToscaTranslatorFactory;
28 import org.openecomp.core.utilities.file.FileUtils;
29 import org.openecomp.core.validation.util.MessageContainerUtil;
30 import org.openecomp.sdc.common.errors.CoreException;
31 import org.openecomp.sdc.common.errors.ErrorCategory;
32 import org.openecomp.sdc.common.errors.ErrorCode;
33 import org.openecomp.sdc.common.togglz.ToggleableFeature;
34 import org.openecomp.sdc.datatypes.error.ErrorLevel;
35 import org.openecomp.sdc.datatypes.error.ErrorMessage;
36 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
37 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
38 import org.openecomp.sdc.translator.TestUtils;
39 import org.togglz.testing.TestFeatureManager;
40 import org.togglz.testing.TestFeatureManagerProvider;
42 import java.io.BufferedInputStream;
44 import java.io.FileInputStream;
45 import java.io.FileOutputStream;
46 import java.io.IOException;
48 import java.util.HashMap;
49 import java.util.HashSet;
50 import java.util.List;
53 import java.util.zip.ZipEntry;
54 import java.util.zip.ZipInputStream;
57 public class BaseFullTranslationTest {
59 public static final String IN_POSTFIX = "/in";
60 public static final String OUT_POSTFIX = "/out";
62 protected static TestFeatureManager manager;
65 public static void enableToggleableFeatures(){
66 manager = new TestFeatureManager(ToggleableFeature.class);
68 TestFeatureManagerProvider.setFeatureManager(manager);
73 public static void disableToggleableFeatures() {
76 TestFeatureManagerProvider.setFeatureManager(null);
79 protected void testTranslationWithInit(String path) throws IOException {
80 File translatedZipFile = initTranslatorAndTranslate(path);
81 testTranslation(path, translatedZipFile);
84 protected File initTranslatorAndTranslate(String path) throws IOException {
85 HeatToToscaTranslator heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface();
86 return translateZipFile(path, heatToToscaTranslator);
89 protected void testTranslation(String basePath, File translatedZipFile) throws IOException {
91 URL url = BaseFullTranslationTest.class.getResource(basePath + OUT_POSTFIX);
92 Set<String> expectedResultFileNameSet = new HashSet<>();
93 Map<String, byte[]> expectedResultMap = new HashMap<>();
95 String path = url.getPath();
96 File pathFile = new File(path);
97 File[] files = pathFile.listFiles();
98 Assert.assertNotNull("manifest files is empty", files);
99 for (File expectedFile : files) {
100 expectedResultFileNameSet.add(expectedFile.getName());
101 try (FileInputStream input = new FileInputStream(expectedFile)) {
102 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
106 try (FileInputStream fis = new FileInputStream(translatedZipFile);
107 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
113 while ((entry = zis.getNextEntry()) != null) {
115 name = entry.getName()
116 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
117 if (expectedResultFileNameSet.contains(name)) {
118 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
119 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
120 assertEquals("difference in file: " + name, expected, actual);
122 expectedResultFileNameSet.remove(name);
125 if (expectedResultFileNameSet.isEmpty()) {
126 expectedResultFileNameSet.forEach(System.out::println);
129 assertEquals(0, expectedResultFileNameSet.size());
130 translatedZipFile.delete();
133 private File translateZipFile(String basePath, HeatToToscaTranslator heatToToscaTranslator) throws IOException {
134 URL inputFilesUrl = this.getClass().getResource(basePath + IN_POSTFIX);
135 String path = inputFilesUrl.getPath();
136 TestUtils.addFilesToTranslator(heatToToscaTranslator, path);
137 TranslatorOutput translatorOutput = heatToToscaTranslator.translate();
138 Assert.assertNotNull(translatorOutput);
139 if (MapUtils.isNotEmpty(translatorOutput.getErrorMessages()) && MapUtils.isNotEmpty(
141 .getMessageByLevel(ErrorLevel.ERROR, translatorOutput.getErrorMessages()))) {
142 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
143 "Error in validation " + getErrorAsString(translatorOutput.getErrorMessages()))
144 .withId("Validation Error").withCategory(ErrorCategory.APPLICATION).build());
147 File file = File.createTempFile("VSP", "zip");
149 try (FileOutputStream fos = new FileOutputStream(file)) {
150 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
152 toscaFileOutputService.createOutputFile(translatorOutput.getToscaServiceModel(), null));
158 private String getErrorAsString(Map<String, List<ErrorMessage>> errorMessages) {
159 StringBuilder sb = new StringBuilder();
160 errorMessages.entrySet().forEach(
161 entry -> sb.append("File:").append(entry.getKey()).append(System.lineSeparator())
162 .append(getErrorList(entry.getValue())));
164 return sb.toString();
167 private String getErrorList(List<ErrorMessage> errors) {
168 StringBuilder sb = new StringBuilder();
170 error -> sb.append(error.getMessage()).append("[").append(error.getLevel()).append("]")
171 .append(System.lineSeparator()));
172 return sb.toString();