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;
20 import static org.openecomp.sdc.translator.TestUtils.getErrorAsString;
22 import org.apache.commons.collections4.MapUtils;
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.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;
41 import java.io.ByteArrayInputStream;
43 import java.io.FileInputStream;
44 import java.io.IOException;
46 import java.util.HashMap;
47 import java.util.HashSet;
50 import java.util.zip.ZipInputStream;
53 public class BaseFullTranslationTest {
55 private static final String IN_POSTFIX = "/in";
56 private static final String OUT_POSTFIX = "/out";
58 protected static TestFeatureManager manager;
61 public static void enableToggleableFeatures() {
62 manager = new TestFeatureManager(ToggleableFeature.class);
64 TestFeatureManagerProvider.setFeatureManager(manager);
67 protected void testTranslationWithInit(String path) throws IOException {
68 byte[] translatedZipFile = initTranslatorAndTranslate(path);
69 testTranslation(path, translatedZipFile);
72 protected byte[] initTranslatorAndTranslate(String path) throws IOException {
73 HeatToToscaTranslator heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface();
74 return translateZipFile(path, heatToToscaTranslator);
77 protected void testTranslation(String basePath, byte[] translatedZipFile) throws IOException {
79 URL url = BaseFullTranslationTest.class.getResource(basePath + OUT_POSTFIX);
80 Set<String> expectedResultFileNameSet = new HashSet<>();
81 Map<String, byte[]> expectedResultMap = new HashMap<>();
83 String path = url.getPath();
84 File pathFile = new File(path);
85 File[] files = pathFile.listFiles();
86 Assert.assertNotNull("manifest files is empty", files);
87 for (File expectedFile : files) {
88 expectedResultFileNameSet.add(expectedFile.getName());
89 try (FileInputStream input = new FileInputStream(expectedFile)) {
90 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
94 try (ByteArrayInputStream fis = new ByteArrayInputStream(translatedZipFile);
95 BufferedInputStream bis = new BufferedInputStream(fis);
96 ZipInputStream zis = new ZipInputStream(bis)) {
97 TestUtils.compareTranslatedOutput(expectedResultFileNameSet, expectedResultMap, zis);
99 assertEquals(0, expectedResultFileNameSet.size());
102 private byte[] translateZipFile(String basePath, HeatToToscaTranslator heatToToscaTranslator) throws IOException {
103 URL inputFilesUrl = this.getClass().getResource(basePath + IN_POSTFIX);
104 String path = inputFilesUrl.getPath();
105 TestUtils.addFilesToTranslator(heatToToscaTranslator, path);
106 TranslatorOutput translatorOutput = heatToToscaTranslator.translate();
107 Assert.assertNotNull(translatorOutput);
108 if (MapUtils.isNotEmpty(translatorOutput.getErrorMessages()) && MapUtils.isNotEmpty(
109 MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, translatorOutput.getErrorMessages()))) {
110 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
111 "Error in validation " + getErrorAsString(translatorOutput.getErrorMessages()))
112 .withId("Validation Error")
113 .withCategory(ErrorCategory.APPLICATION).build());
116 return new ToscaFileOutputServiceCsarImpl().createOutputFile(translatorOutput.getToscaServiceModel(), null);