2 * Copyright © 2016-2017 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 org.apache.commons.collections4.MapUtils;
20 import org.junit.AfterClass;
21 import org.junit.Assert;
22 import org.junit.BeforeClass;
23 import org.openecomp.core.translator.api.HeatToToscaTranslator;
24 import org.openecomp.core.translator.datatypes.TranslatorOutput;
25 import org.openecomp.core.translator.factory.HeatToToscaTranslatorFactory;
26 import org.openecomp.core.utilities.file.FileUtils;
27 import org.openecomp.core.validation.util.MessageContainerUtil;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.common.errors.ErrorCategory;
30 import org.openecomp.sdc.common.errors.ErrorCode;
31 import org.openecomp.sdc.common.togglz.ToggleableFeature;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.datatypes.error.ErrorMessage;
34 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
35 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
36 import org.openecomp.sdc.translator.TestUtils;
37 import org.togglz.testing.TestFeatureManager;
38 import org.togglz.testing.TestFeatureManagerProvider;
40 import java.io.BufferedInputStream;
42 import java.io.FileInputStream;
43 import java.io.FileOutputStream;
44 import java.io.IOException;
46 import java.util.HashMap;
47 import java.util.HashSet;
48 import java.util.List;
51 import java.util.zip.ZipEntry;
52 import java.util.zip.ZipInputStream;
54 import static org.junit.Assert.assertEquals;
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);
72 public static void disableToggleableFeatures() {
75 TestFeatureManagerProvider.setFeatureManager(null);
78 protected void testTranslationWithInit(String path) throws IOException {
79 File translatedZipFile = initTranslatorAndTranslate(path);
80 testTranslation(path, translatedZipFile);
83 protected File initTranslatorAndTranslate(String path) throws IOException {
84 HeatToToscaTranslator heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface();
85 return translateZipFile(path, heatToToscaTranslator);
88 protected void testTranslation(String basePath, File translatedZipFile) throws IOException {
90 URL url = BaseFullTranslationTest.class.getResource(basePath + OUT_POSTFIX);
91 Set<String> expectedResultFileNameSet = new HashSet<>();
92 Map<String, byte[]> expectedResultMap = new HashMap<>();
94 String path = url.getPath();
95 File pathFile = new File(path);
96 File[] files = pathFile.listFiles();
97 Assert.assertNotNull("manifest files is empty", files);
98 for (File expectedFile : files) {
99 expectedResultFileNameSet.add(expectedFile.getName());
100 try (FileInputStream input = new FileInputStream(expectedFile)) {
101 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
105 try (FileInputStream fis = new FileInputStream(translatedZipFile);
106 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
112 while ((entry = zis.getNextEntry()) != null) {
114 name = entry.getName()
115 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
116 if (expectedResultFileNameSet.contains(name)) {
117 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
118 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
119 assertEquals("difference in file: " + name, expected, actual);
121 expectedResultFileNameSet.remove(name);
124 if (expectedResultFileNameSet.isEmpty()) {
125 expectedResultFileNameSet.forEach(System.out::println);
128 assertEquals(0, expectedResultFileNameSet.size());
131 private File translateZipFile(String basePath, HeatToToscaTranslator heatToToscaTranslator) throws IOException {
132 URL inputFilesUrl = this.getClass().getResource(basePath + IN_POSTFIX);
133 String path = inputFilesUrl.getPath();
134 TestUtils.addFilesToTranslator(heatToToscaTranslator, path);
135 TranslatorOutput translatorOutput = heatToToscaTranslator.translate();
136 Assert.assertNotNull(translatorOutput);
137 if (MapUtils.isNotEmpty(translatorOutput.getErrorMessages()) && MapUtils.isNotEmpty(
139 .getMessageByLevel(ErrorLevel.ERROR, translatorOutput.getErrorMessages()))) {
140 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
141 "Error in validation " + getErrorAsString(translatorOutput.getErrorMessages()))
142 .withId("Validation Error").withCategory(ErrorCategory.APPLICATION).build());
145 File file = new File(path + "/VSP.zip");
146 file.createNewFile();
148 try (FileOutputStream fos = new FileOutputStream(file)) {
149 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
151 toscaFileOutputService.createOutputFile(translatorOutput.getToscaServiceModel(), null));
157 private String getErrorAsString(Map<String, List<ErrorMessage>> errorMessages) {
158 StringBuilder sb = new StringBuilder();
159 errorMessages.entrySet().forEach(
160 entry -> sb.append("File:").append(entry.getKey()).append(System.lineSeparator())
161 .append(getErrorList(entry.getValue())));
163 return sb.toString();
166 private String getErrorList(List<ErrorMessage> errors) {
167 StringBuilder sb = new StringBuilder();
169 error -> sb.append(error.getMessage()).append("[").append(error.getLevel()).append("]")
170 .append(System.lineSeparator()));
171 return sb.toString();