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 java.io.BufferedInputStream;
23 import java.io.ByteArrayInputStream;
25 import java.io.FileInputStream;
26 import java.io.IOException;
28 import java.util.HashMap;
29 import java.util.HashSet;
32 import java.util.zip.ZipInputStream;
33 import org.apache.commons.collections4.MapUtils;
34 import org.junit.Assert;
35 import org.junit.BeforeClass;
36 import org.openecomp.core.translator.api.HeatToToscaTranslator;
37 import org.openecomp.core.translator.datatypes.TranslatorOutput;
38 import org.openecomp.core.translator.factory.HeatToToscaTranslatorFactory;
39 import org.openecomp.core.utilities.file.FileUtils;
40 import org.openecomp.core.validation.util.MessageContainerUtil;
41 import org.openecomp.sdc.be.togglz.ToggleableFeature;
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.datatypes.error.ErrorLevel;
46 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
47 import org.openecomp.sdc.translator.TestUtils;
48 import org.togglz.testing.TestFeatureManager;
49 import org.togglz.testing.TestFeatureManagerProvider;
52 public class BaseFullTranslationTest {
54 private static final String IN_POSTFIX = "/in";
55 private static final String OUT_POSTFIX = "/out";
57 protected static TestFeatureManager manager;
60 public static void enableToggleableFeatures() {
61 manager = new TestFeatureManager(ToggleableFeature.class);
63 TestFeatureManagerProvider.setFeatureManager(manager);
66 protected void testTranslationWithInit(String path) throws IOException {
67 byte[] translatedZipFile = initTranslatorAndTranslate(path);
68 testTranslation(path, translatedZipFile);
71 protected byte[] initTranslatorAndTranslate(String path) throws IOException {
72 HeatToToscaTranslator heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface();
73 return translateZipFile(path, heatToToscaTranslator);
76 protected void testTranslation(String basePath, byte[] translatedZipFile) throws IOException {
78 URL url = BaseFullTranslationTest.class.getResource(basePath + OUT_POSTFIX);
79 Set<String> expectedResultFileNameSet = new HashSet<>();
80 Map<String, byte[]> expectedResultMap = new HashMap<>();
82 String path = url.getPath();
83 File pathFile = new File(path);
84 File[] files = pathFile.listFiles();
85 Assert.assertNotNull("manifest files is empty", files);
86 for (File expectedFile : files) {
87 expectedResultFileNameSet.add(expectedFile.getName());
88 try (FileInputStream input = new FileInputStream(expectedFile)) {
89 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
93 try (ByteArrayInputStream fis = new ByteArrayInputStream(translatedZipFile);
94 BufferedInputStream bis = new BufferedInputStream(fis); ZipInputStream zis = new ZipInputStream(bis)) {
95 TestUtils.compareTranslatedOutput(expectedResultFileNameSet, expectedResultMap, zis);
97 assertEquals(0, expectedResultFileNameSet.size());
100 private byte[] translateZipFile(String basePath, HeatToToscaTranslator heatToToscaTranslator) throws IOException {
101 URL inputFilesUrl = this.getClass().getResource(basePath + IN_POSTFIX);
102 String path = inputFilesUrl.getPath();
103 TestUtils.addFilesToTranslator(heatToToscaTranslator, path);
104 TranslatorOutput translatorOutput = heatToToscaTranslator.translate();
105 Assert.assertNotNull(translatorOutput);
106 if (MapUtils.isNotEmpty(translatorOutput.getErrorMessages()) && MapUtils.isNotEmpty(
107 MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, translatorOutput.getErrorMessages()))) {
108 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
109 "Error in validation " + getErrorAsString(translatorOutput.getErrorMessages()))
110 .withId("Validation Error").withCategory(ErrorCategory.APPLICATION)
114 return new ToscaFileOutputServiceCsarImpl().createOutputFile(translatorOutput.getToscaServiceModel(), null);