1 package org.openecomp.sdc.translator.services.heattotosca.impl;
3 import org.openecomp.sdc.common.errors.CoreException;
4 import org.openecomp.sdc.common.errors.ErrorCategory;
5 import org.openecomp.sdc.common.errors.ErrorCode;
6 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
7 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
8 import org.openecomp.sdc.translator.TestUtils;
9 import org.openecomp.core.translator.api.HeatToToscaTranslator;
10 import org.openecomp.core.translator.datatypes.TranslatorOutput;
11 import org.openecomp.core.translator.factory.HeatToToscaTranslatorFactory;
12 import org.openecomp.core.utilities.file.FileUtils;
13 import org.openecomp.core.validation.types.MessageContainerUtil;
14 import org.apache.commons.collections4.MapUtils;
15 import org.junit.Assert;
16 import org.junit.Before;
21 import java.util.zip.ZipEntry;
22 import java.util.zip.ZipInputStream;
24 import static org.junit.Assert.assertEquals;
26 public class BaseResourceTranslationTest {
28 protected String inputFilesPath;
29 protected String outputFilesPath;
30 private HeatToToscaTranslator heatToToscaTranslator;
31 private File translatedZipFile;
33 private Map<String, byte[]> expectedResultMap = new HashMap<>();
34 private Set<String> expectedResultFileNameSet = new HashSet<>();
37 public void setUp() throws IOException {
38 initTranslatorAndTranslate();
41 protected void initTranslatorAndTranslate() throws IOException {
42 heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface();
43 translatedZipFile = translateZipFile();
46 protected void testTranslation() throws IOException {
48 URL url = BaseResourceTranslationTest.class.getResource(outputFilesPath);
50 String path = url.getPath();
51 File pathFile = new File(path);
52 File[] files = pathFile.listFiles();
53 Assert.assertNotNull("manifest files is empty", files);
54 for (File expectedFile : files) {
55 expectedResultFileNameSet.add(expectedFile.getName());
56 try (FileInputStream input = new FileInputStream(expectedFile)) {
57 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
61 try (FileInputStream fis = new FileInputStream(translatedZipFile);
62 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
68 while ((entry = zis.getNextEntry()) != null) {
70 name = entry.getName()
71 .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
72 if (expectedResultFileNameSet.contains(name)) {
73 expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
74 actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
75 assertEquals("difference in file: " + name, expected, actual);
77 expectedResultFileNameSet.remove(name);
80 if (expectedResultFileNameSet.isEmpty()) {
81 expectedResultFileNameSet.forEach(System.out::println);
84 assertEquals(0, expectedResultFileNameSet.size());
87 private File translateZipFile() throws IOException {
88 String zipFilename = "VSP.zip";
89 URL inputFilesUrl = this.getClass().getResource(inputFilesPath);
90 String path = inputFilesUrl.getPath();
91 TestUtils.addFilesToTranslator(heatToToscaTranslator, path);
92 TranslatorOutput translatorOutput = heatToToscaTranslator.translate();
93 Assert.assertNotNull(translatorOutput);
94 if (MapUtils.isNotEmpty(translatorOutput.getErrorMessages()) && MapUtils.isNotEmpty(
96 .getMessageByLevel(org.openecomp.sdc.datatypes.error.ErrorLevel.ERROR, translatorOutput.getErrorMessages()))) {
97 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
98 "Error in validation " + getErrorAsString(translatorOutput.getErrorMessages()))
99 .withId("Validation Error").withCategory(ErrorCategory.APPLICATION).build());
101 File file = new File(path + "/" + zipFilename);
102 file.createNewFile();
104 try (FileOutputStream fos = new FileOutputStream(file)) {
105 ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
107 toscaFileOutputService.createOutputFile(translatorOutput.getToscaServiceModel(), null));
113 private String getErrorAsString(Map<String, List<org.openecomp.sdc.datatypes.error.ErrorMessage>> errorMessages) {
114 StringBuilder sb = new StringBuilder();
115 errorMessages.entrySet().forEach(
116 entry -> sb.append("File:").append(entry.getKey()).append(System.lineSeparator())
117 .append(getErrorList(entry.getValue())));
119 return sb.toString();
122 private String getErrorList(List<org.openecomp.sdc.datatypes.error.ErrorMessage> errors) {
123 StringBuilder sb = new StringBuilder();
124 errors.stream().forEach(
125 error -> sb.append(error.getMessage()).append("[").append(error.getLevel()).append("]")
126 .append(System.lineSeparator()));
127 return sb.toString();