Reducing onboarding backend maven build time
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / test / java / org / openecomp / sdc / translator / services / heattotosca / impl / resourcetranslation / BaseFullTranslationTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
18
19 import static org.junit.Assert.assertEquals;
20
21 import org.apache.commons.collections4.MapUtils;
22 import org.junit.AfterClass;
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.datatypes.error.ErrorMessage;
36 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
37 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
38 import org.openecomp.sdc.translator.TestUtils;
39 import org.togglz.testing.TestFeatureManager;
40 import org.togglz.testing.TestFeatureManagerProvider;
41
42 import java.io.BufferedInputStream;
43 import java.io.File;
44 import java.io.FileInputStream;
45 import java.io.FileOutputStream;
46 import java.io.IOException;
47 import java.net.URL;
48 import java.util.HashMap;
49 import java.util.HashSet;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.Set;
53 import java.util.zip.ZipEntry;
54 import java.util.zip.ZipInputStream;
55
56
57 public class BaseFullTranslationTest {
58
59   public static final String IN_POSTFIX = "/in";
60   public static final String OUT_POSTFIX = "/out";
61
62   protected static TestFeatureManager manager;
63
64   @BeforeClass
65   public static void enableToggleableFeatures(){
66     manager = new TestFeatureManager(ToggleableFeature.class);
67     manager.enableAll();
68     TestFeatureManagerProvider.setFeatureManager(manager);
69   }
70
71
72   @AfterClass
73   public static void disableToggleableFeatures() {
74     manager.disableAll();
75     manager = null;
76     TestFeatureManagerProvider.setFeatureManager(null);
77   }
78
79   protected void testTranslationWithInit(String path) throws IOException {
80     File translatedZipFile = initTranslatorAndTranslate(path);
81     testTranslation(path, translatedZipFile);
82   }
83
84   protected File initTranslatorAndTranslate(String path) throws IOException {
85     HeatToToscaTranslator heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface();
86     return translateZipFile(path, heatToToscaTranslator);
87   }
88
89   protected void testTranslation(String basePath, File translatedZipFile) throws IOException {
90
91     URL url = BaseFullTranslationTest.class.getResource(basePath + OUT_POSTFIX);
92     Set<String> expectedResultFileNameSet = new HashSet<>();
93     Map<String, byte[]> expectedResultMap = new HashMap<>();
94
95     String path = url.getPath();
96     File pathFile = new File(path);
97     File[] files = pathFile.listFiles();
98     Assert.assertNotNull("manifest files is empty", files);
99     for (File expectedFile : files) {
100       expectedResultFileNameSet.add(expectedFile.getName());
101       try (FileInputStream input = new FileInputStream(expectedFile)) {
102         expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
103       }
104     }
105
106     try (FileInputStream fis = new FileInputStream(translatedZipFile);
107          ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
108       ZipEntry entry;
109       String name;
110       String expected;
111       String actual;
112
113       while ((entry = zis.getNextEntry()) != null) {
114
115         name = entry.getName()
116             .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
117         if (expectedResultFileNameSet.contains(name)) {
118           expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
119           actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
120           assertEquals("difference in file: " + name, expected, actual);
121
122           expectedResultFileNameSet.remove(name);
123         }
124       }
125       if (expectedResultFileNameSet.isEmpty()) {
126         expectedResultFileNameSet.forEach(System.out::println);
127       }
128     }
129     assertEquals(0, expectedResultFileNameSet.size());
130     translatedZipFile.delete();
131   }
132
133   private File translateZipFile(String basePath, HeatToToscaTranslator heatToToscaTranslator) throws IOException {
134     URL inputFilesUrl = this.getClass().getResource(basePath + IN_POSTFIX);
135     String path = inputFilesUrl.getPath();
136     TestUtils.addFilesToTranslator(heatToToscaTranslator, path);
137     TranslatorOutput translatorOutput = heatToToscaTranslator.translate();
138     Assert.assertNotNull(translatorOutput);
139     if (MapUtils.isNotEmpty(translatorOutput.getErrorMessages()) && MapUtils.isNotEmpty(
140         MessageContainerUtil
141             .getMessageByLevel(ErrorLevel.ERROR, translatorOutput.getErrorMessages()))) {
142       throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
143           "Error in validation " + getErrorAsString(translatorOutput.getErrorMessages()))
144           .withId("Validation Error").withCategory(ErrorCategory.APPLICATION).build());
145     }
146
147     File file = File.createTempFile("VSP", "zip");
148
149     try (FileOutputStream fos = new FileOutputStream(file)) {
150       ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
151       fos.write(
152           toscaFileOutputService.createOutputFile(translatorOutput.getToscaServiceModel(), null));
153     }
154
155     return file;
156   }
157
158   private String getErrorAsString(Map<String, List<ErrorMessage>> errorMessages) {
159     StringBuilder sb = new StringBuilder();
160     errorMessages.entrySet().forEach(
161         entry -> sb.append("File:").append(entry.getKey()).append(System.lineSeparator())
162             .append(getErrorList(entry.getValue())));
163
164     return sb.toString();
165   }
166
167   private String getErrorList(List<ErrorMessage> errors) {
168     StringBuilder sb = new StringBuilder();
169     errors.forEach(
170         error -> sb.append(error.getMessage()).append("[").append(error.getLevel()).append("]")
171             .append(System.lineSeparator()));
172     return sb.toString();
173   }
174
175 }