Toggle
[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 import static org.openecomp.sdc.translator.TestUtils.getErrorAsString;
21
22 import java.io.BufferedInputStream;
23 import java.io.ByteArrayInputStream;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.net.URL;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.Map;
31 import java.util.Set;
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;
50
51
52 public class BaseFullTranslationTest {
53
54     private static final String IN_POSTFIX = "/in";
55     private static final String OUT_POSTFIX = "/out";
56
57     protected static TestFeatureManager manager;
58
59     @BeforeClass
60     public static void enableToggleableFeatures() {
61         manager = new TestFeatureManager(ToggleableFeature.class);
62         manager.enableAll();
63         TestFeatureManagerProvider.setFeatureManager(manager);
64     }
65
66     protected void testTranslationWithInit(String path) throws IOException {
67         byte[] translatedZipFile = initTranslatorAndTranslate(path);
68         testTranslation(path, translatedZipFile);
69     }
70
71     protected byte[] initTranslatorAndTranslate(String path) throws IOException {
72         HeatToToscaTranslator heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface();
73         return translateZipFile(path, heatToToscaTranslator);
74     }
75
76     protected void testTranslation(String basePath, byte[] translatedZipFile) throws IOException {
77
78         URL url = BaseFullTranslationTest.class.getResource(basePath + OUT_POSTFIX);
79         Set<String> expectedResultFileNameSet = new HashSet<>();
80         Map<String, byte[]> expectedResultMap = new HashMap<>();
81
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));
90             }
91         }
92
93         try (ByteArrayInputStream fis = new ByteArrayInputStream(translatedZipFile);
94              BufferedInputStream bis = new BufferedInputStream(fis); ZipInputStream zis = new ZipInputStream(bis)) {
95             TestUtils.compareTranslatedOutput(expectedResultFileNameSet, expectedResultMap, zis);
96         }
97         assertEquals(0, expectedResultFileNameSet.size());
98     }
99
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)
111                                             .build());
112         }
113
114         return new ToscaFileOutputServiceCsarImpl().createOutputFile(translatorOutput.getToscaServiceModel(), null);
115     }
116
117 }