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 java.io.BufferedInputStream;
22 import java.io.ByteArrayInputStream;
24 import java.io.FileInputStream;
25 import java.io.IOException;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipInputStream;
34 import org.apache.commons.collections4.MapUtils;
35 import org.junit.Assert;
36 import org.junit.BeforeClass;
37 import org.openecomp.core.translator.api.HeatToToscaTranslator;
38 import org.openecomp.core.translator.datatypes.TranslatorOutput;
39 import org.openecomp.core.translator.factory.HeatToToscaTranslatorFactory;
40 import org.openecomp.core.utilities.file.FileUtils;
41 import org.openecomp.core.validation.util.MessageContainerUtil;
42 import org.openecomp.sdc.common.errors.CoreException;
43 import org.openecomp.sdc.common.errors.ErrorCategory;
44 import org.openecomp.sdc.common.errors.ErrorCode;
45 import org.openecomp.sdc.common.togglz.ToggleableFeature;
46 import org.openecomp.sdc.datatypes.error.ErrorLevel;
47 import org.openecomp.sdc.datatypes.error.ErrorMessage;
48 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
49 import org.openecomp.sdc.translator.TestUtils;
50 import org.togglz.testing.TestFeatureManager;
51 import org.togglz.testing.TestFeatureManagerProvider;
54 public class BaseFullTranslationTest {
56 public static final String IN_POSTFIX = "/in";
57 public static final String OUT_POSTFIX = "/out";
60 protected static TestFeatureManager manager;
63 public static void enableToggleableFeatures() {
64 manager = new TestFeatureManager(ToggleableFeature.class);
66 TestFeatureManagerProvider.setFeatureManager(manager);
70 public static void disableToggleableFeatures() {
73 TestFeatureManagerProvider.setFeatureManager(null);
76 protected void testTranslationWithInit(String path) throws IOException {
77 byte[] translatedZipFile = initTranslatorAndTranslate(path);
78 testTranslation(path, translatedZipFile);
81 protected byte[] initTranslatorAndTranslate(String path) throws IOException {
82 HeatToToscaTranslator heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface();
83 return translateZipFile(path, heatToToscaTranslator);
86 protected void testTranslation(String basePath, byte[] translatedZipFile) throws IOException {
88 URL url = BaseFullTranslationTest.class.getResource(basePath + OUT_POSTFIX);
89 Set<String> expectedResultFileNameSet = new HashSet<>();
90 Map<String, byte[]> expectedResultMap = new HashMap<>();
92 String path = url.getPath();
93 File pathFile = new File(path);
94 File[] files = pathFile.listFiles();
95 Assert.assertNotNull("manifest files is empty", files);
96 for (File expectedFile : files) {
97 expectedResultFileNameSet.add(expectedFile.getName());
98 try (FileInputStream input = new FileInputStream(expectedFile)) {
99 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
103 try (ByteArrayInputStream fis = new ByteArrayInputStream(translatedZipFile);
104 BufferedInputStream bis = new BufferedInputStream(fis); ZipInputStream zis = new ZipInputStream(bis)) {
110 while ((entry = zis.getNextEntry()) != null) {
112 name = entry.getName()
113 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
114 if (expectedResultFileNameSet.contains(name)) {
115 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
116 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
117 assertEquals("difference in file: " + name, expected, actual);
119 expectedResultFileNameSet.remove(name);
122 if (expectedResultFileNameSet.isEmpty()) {
123 expectedResultFileNameSet.forEach(System.out::println);
126 assertEquals(0, expectedResultFileNameSet.size());
129 private byte[] translateZipFile(String basePath, HeatToToscaTranslator heatToToscaTranslator) throws IOException {
130 URL inputFilesUrl = this.getClass().getResource(basePath + IN_POSTFIX);
131 String path = inputFilesUrl.getPath();
132 TestUtils.addFilesToTranslator(heatToToscaTranslator, path);
133 TranslatorOutput translatorOutput = heatToToscaTranslator.translate();
134 Assert.assertNotNull(translatorOutput);
135 if (MapUtils.isNotEmpty(translatorOutput.getErrorMessages()) && MapUtils.isNotEmpty(
136 MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, translatorOutput.getErrorMessages()))) {
137 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
138 "Error in validation " + getErrorAsString(translatorOutput.getErrorMessages()))
139 .withId("Validation Error")
140 .withCategory(ErrorCategory.APPLICATION).build());
144 new ToscaFileOutputServiceCsarImpl().createOutputFile(translatorOutput.getToscaServiceModel(), null);
149 private String getErrorAsString(Map<String, List<ErrorMessage>> errorMessages) {
150 StringBuilder sb = new StringBuilder();
151 errorMessages.entrySet().forEach(
152 entry -> sb.append("File:").append(entry.getKey()).append(System.lineSeparator())
153 .append(getErrorList(entry.getValue())));
155 return sb.toString();
158 private String getErrorList(List<ErrorMessage> errors) {
159 StringBuilder sb = new StringBuilder();
160 errors.forEach(error -> sb.append(error.getMessage()).append("[").append(error.getLevel()).append("]")
161 .append(System.lineSeparator()));
162 return sb.toString();