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);
67 if (!ToggleableFeature.FORWARDER_CAPABILITY.isActive()) {
68 manager.enable(ToggleableFeature.FORWARDER_CAPABILITY);
70 if (!ToggleableFeature.ANNOTATIONS.isActive()) {
71 manager.enable(ToggleableFeature.ANNOTATIONS);
73 if(!ToggleableFeature.VLAN_TAGGING.isActive()) {
74 manager.enable(ToggleableFeature.VLAN_TAGGING);
80 public static void disableToggleableFeatures() {
81 manager.disable(ToggleableFeature.FORWARDER_CAPABILITY);
82 manager.disable(ToggleableFeature.ANNOTATIONS);
83 manager.disable(ToggleableFeature.VLAN_TAGGING);
85 TestFeatureManagerProvider.setFeatureManager(null);
88 protected void testTranslationWithInit(String path) throws IOException {
89 File translatedZipFile = initTranslatorAndTranslate(path);
90 testTranslation(path, translatedZipFile);
93 protected File initTranslatorAndTranslate(String path) throws IOException {
94 HeatToToscaTranslator heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface();
95 return translateZipFile(path, heatToToscaTranslator);
98 protected void testTranslation(String basePath, File translatedZipFile) throws IOException {
100 URL url = BaseFullTranslationTest.class.getResource(basePath + OUT_POSTFIX);
101 Set<String> expectedResultFileNameSet = new HashSet<>();
102 Map<String, byte[]> expectedResultMap = new HashMap<>();
104 String path = url.getPath();
105 File pathFile = new File(path);
106 File[] files = pathFile.listFiles();
107 Assert.assertNotNull("manifest files is empty", files);
108 for (File expectedFile : files) {
109 expectedResultFileNameSet.add(expectedFile.getName());
110 try (FileInputStream input = new FileInputStream(expectedFile)) {
111 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
115 try (FileInputStream fis = new FileInputStream(translatedZipFile);
116 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
122 while ((entry = zis.getNextEntry()) != null) {
124 name = entry.getName()
125 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
126 if (expectedResultFileNameSet.contains(name)) {
127 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
128 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
129 assertEquals("difference in file: " + name, expected, actual);
131 expectedResultFileNameSet.remove(name);
134 if (expectedResultFileNameSet.isEmpty()) {
135 expectedResultFileNameSet.forEach(System.out::println);
138 assertEquals(0, expectedResultFileNameSet.size());
141 private File translateZipFile(String basePath, HeatToToscaTranslator heatToToscaTranslator) throws IOException {
142 URL inputFilesUrl = this.getClass().getResource(basePath + IN_POSTFIX);
143 String path = inputFilesUrl.getPath();
144 TestUtils.addFilesToTranslator(heatToToscaTranslator, path);
145 TranslatorOutput translatorOutput = heatToToscaTranslator.translate();
146 Assert.assertNotNull(translatorOutput);
147 if (MapUtils.isNotEmpty(translatorOutput.getErrorMessages()) && MapUtils.isNotEmpty(
149 .getMessageByLevel(ErrorLevel.ERROR, translatorOutput.getErrorMessages()))) {
150 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
151 "Error in validation " + getErrorAsString(translatorOutput.getErrorMessages()))
152 .withId("Validation Error").withCategory(ErrorCategory.APPLICATION).build());
155 File file = new File(path + "/VSP.zip");
156 file.createNewFile();
158 try (FileOutputStream fos = new FileOutputStream(file)) {
159 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
161 toscaFileOutputService.createOutputFile(translatorOutput.getToscaServiceModel(), null));
167 private String getErrorAsString(Map<String, List<ErrorMessage>> errorMessages) {
168 StringBuilder sb = new StringBuilder();
169 errorMessages.entrySet().forEach(
170 entry -> sb.append("File:").append(entry.getKey()).append(System.lineSeparator())
171 .append(getErrorList(entry.getValue())));
173 return sb.toString();
176 private String getErrorList(List<ErrorMessage> errors) {
177 StringBuilder sb = new StringBuilder();
179 error -> sb.append(error.getMessage()).append("[").append(error.getLevel()).append("]")
180 .append(System.lineSeparator()));
181 return sb.toString();