[sdc] docker file fix for cassandra
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / test / java / org / openecomp / sdc / translator / services / heattotosca / impl / BaseResourceTranslationTest.java
1 package org.openecomp.sdc.translator.services.heattotosca.impl;
2
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;
17
18 import java.io.*;
19 import java.net.URL;
20 import java.util.*;
21 import java.util.zip.ZipEntry;
22 import java.util.zip.ZipInputStream;
23
24 import static org.junit.Assert.assertEquals;
25
26 public class BaseResourceTranslationTest {
27
28   protected String inputFilesPath;
29   protected String outputFilesPath;
30   private HeatToToscaTranslator heatToToscaTranslator;
31   private File translatedZipFile;
32
33   private Map<String, byte[]> expectedResultMap = new HashMap<>();
34   private Set<String> expectedResultFileNameSet = new HashSet<>();
35
36   @Before
37   public void setUp() throws IOException {
38     initTranslatorAndTranslate();
39   }
40
41   protected void initTranslatorAndTranslate() throws IOException {
42     heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface();
43     translatedZipFile = translateZipFile();
44   }
45
46   protected void testTranslation() throws IOException {
47
48     URL url = BaseResourceTranslationTest.class.getResource(outputFilesPath);
49
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));
58       }
59     }
60
61     try (FileInputStream fis = new FileInputStream(translatedZipFile);
62          ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
63       ZipEntry entry;
64       String name;
65       String expected;
66       String actual;
67
68       while ((entry = zis.getNextEntry()) != null) {
69
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);
76
77           expectedResultFileNameSet.remove(name);
78         }
79       }
80       if (expectedResultFileNameSet.isEmpty()) {
81         expectedResultFileNameSet.forEach(System.out::println);
82       }
83     }
84     assertEquals(0, expectedResultFileNameSet.size());
85   }
86
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(
95         MessageContainerUtil
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());
100     }
101     File file = new File(path + "/" + zipFilename);
102     file.createNewFile();
103
104     try (FileOutputStream fos = new FileOutputStream(file)) {
105       ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
106       fos.write(
107           toscaFileOutputService.createOutputFile(translatorOutput.getToscaServiceModel(), null));
108     }
109
110     return file;
111   }
112
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())));
118
119     return sb.toString();
120   }
121
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();
128   }
129
130 }