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 org.apache.commons.collections4.MapUtils;
20 import org.junit.Assert;
21 import org.junit.BeforeClass;
22 import org.openecomp.core.translator.api.HeatToToscaTranslator;
23 import org.openecomp.core.translator.datatypes.TranslatorOutput;
24 import org.openecomp.core.translator.factory.HeatToToscaTranslatorFactory;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.core.validation.util.MessageContainerUtil;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.openecomp.sdc.common.errors.ErrorCategory;
29 import org.openecomp.sdc.common.errors.ErrorCode;
30 import org.openecomp.sdc.common.togglz.ToggleableFeature;
31 import org.openecomp.sdc.datatypes.error.ErrorLevel;
32 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
33 import org.openecomp.sdc.translator.TestUtils;
34 import org.togglz.testing.TestFeatureManager;
35 import org.togglz.testing.TestFeatureManagerProvider;
39 import java.util.HashMap;
40 import java.util.HashSet;
43 import java.util.zip.ZipInputStream;
45 import static org.junit.Assert.assertEquals;
46 import static org.openecomp.sdc.translator.TestUtils.getErrorAsString;
49 public class BaseFullTranslationTest {
51 private static final String IN_POSTFIX = "/in";
52 private static final String OUT_POSTFIX = "/out";
54 protected static TestFeatureManager manager;
57 public static void enableToggleableFeatures() {
58 manager = new TestFeatureManager(ToggleableFeature.class);
60 TestFeatureManagerProvider.setFeatureManager(manager);
63 protected void testTranslationWithInit(String path) throws IOException {
64 byte[] translatedZipFile = initTranslatorAndTranslate(path);
65 testTranslation(path, translatedZipFile);
68 protected byte[] initTranslatorAndTranslate(String path) throws IOException {
69 HeatToToscaTranslator heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface();
70 return translateZipFile(path, heatToToscaTranslator);
73 protected void testTranslation(String basePath, byte[] translatedZipFile) throws IOException {
75 URL url = BaseFullTranslationTest.class.getResource(basePath + OUT_POSTFIX);
76 Set<String> expectedResultFileNameSet = new HashSet<>();
77 Map<String, byte[]> expectedResultMap = new HashMap<>();
79 String path = url.getPath();
80 File pathFile = new File(path);
81 File[] files = pathFile.listFiles();
82 Assert.assertNotNull("manifest files is empty", files);
83 for (File expectedFile : files) {
84 expectedResultFileNameSet.add(expectedFile.getName());
85 try (FileInputStream input = new FileInputStream(expectedFile)) {
86 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
90 try (ByteArrayInputStream fis = new ByteArrayInputStream(translatedZipFile);
91 BufferedInputStream bis = new BufferedInputStream(fis);
92 ZipInputStream zis = new ZipInputStream(bis)) {
93 TestUtils.compareTranslatedOutput(expectedResultFileNameSet, expectedResultMap, zis);
95 assertEquals(0, expectedResultFileNameSet.size());
98 private byte[] translateZipFile(String basePath, HeatToToscaTranslator heatToToscaTranslator) throws IOException {
99 URL inputFilesUrl = this.getClass().getResource(basePath + IN_POSTFIX);
100 String path = inputFilesUrl.getPath();
101 TestUtils.addFilesToTranslator(heatToToscaTranslator, path);
102 TranslatorOutput translatorOutput = heatToToscaTranslator.translate();
103 Assert.assertNotNull(translatorOutput);
104 if (MapUtils.isNotEmpty(translatorOutput.getErrorMessages()) && MapUtils.isNotEmpty(
105 MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, translatorOutput.getErrorMessages()))) {
106 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
107 "Error in validation " + getErrorAsString(translatorOutput.getErrorMessages()))
108 .withId("Validation Error")
109 .withCategory(ErrorCategory.APPLICATION).build());
112 return new ToscaFileOutputServiceCsarImpl().createOutputFile(translatorOutput.getToscaServiceModel(), null);