758271bf4682df98c679e06a03ff00fc834b2427
[sdc.git] /
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 org.apache.commons.collections4.MapUtils;
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.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
36 import org.openecomp.sdc.translator.TestUtils;
37 import org.togglz.testing.TestFeatureManager;
38 import org.togglz.testing.TestFeatureManagerProvider;
39
40 import java.io.BufferedInputStream;
41 import java.io.ByteArrayInputStream;
42 import java.io.File;
43 import java.io.FileInputStream;
44 import java.io.IOException;
45 import java.net.URL;
46 import java.util.HashMap;
47 import java.util.HashSet;
48 import java.util.Map;
49 import java.util.Set;
50 import java.util.zip.ZipInputStream;
51
52
53 public class BaseFullTranslationTest {
54
55     private static final String IN_POSTFIX = "/in";
56     private static final String OUT_POSTFIX = "/out";
57
58     protected static TestFeatureManager manager;
59
60     @BeforeClass
61     public static void enableToggleableFeatures() {
62         manager = new TestFeatureManager(ToggleableFeature.class);
63         manager.enableAll();
64         TestFeatureManagerProvider.setFeatureManager(manager);
65     }
66
67     protected void testTranslationWithInit(String path) throws IOException {
68         byte[] translatedZipFile = initTranslatorAndTranslate(path);
69         testTranslation(path, translatedZipFile);
70     }
71
72     protected byte[] initTranslatorAndTranslate(String path) throws IOException {
73         HeatToToscaTranslator heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface();
74         return translateZipFile(path, heatToToscaTranslator);
75     }
76
77     protected void testTranslation(String basePath, byte[] translatedZipFile) throws IOException {
78
79         URL url = BaseFullTranslationTest.class.getResource(basePath + OUT_POSTFIX);
80         Set<String> expectedResultFileNameSet = new HashSet<>();
81         Map<String, byte[]> expectedResultMap = new HashMap<>();
82
83         String path = url.getPath();
84         File pathFile = new File(path);
85         File[] files = pathFile.listFiles();
86         Assert.assertNotNull("manifest files is empty", files);
87         for (File expectedFile : files) {
88             expectedResultFileNameSet.add(expectedFile.getName());
89             try (FileInputStream input = new FileInputStream(expectedFile)) {
90                 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
91             }
92         }
93
94         try (ByteArrayInputStream fis = new ByteArrayInputStream(translatedZipFile);
95              BufferedInputStream bis = new BufferedInputStream(fis);
96              ZipInputStream zis = new ZipInputStream(bis)) {
97             TestUtils.compareTranslatedOutput(expectedResultFileNameSet, expectedResultMap, zis);
98         }
99         assertEquals(0, expectedResultFileNameSet.size());
100     }
101
102     private byte[] translateZipFile(String basePath, HeatToToscaTranslator heatToToscaTranslator) throws IOException {
103         URL inputFilesUrl = this.getClass().getResource(basePath + IN_POSTFIX);
104         String path = inputFilesUrl.getPath();
105         TestUtils.addFilesToTranslator(heatToToscaTranslator, path);
106         TranslatorOutput translatorOutput = heatToToscaTranslator.translate();
107         Assert.assertNotNull(translatorOutput);
108         if (MapUtils.isNotEmpty(translatorOutput.getErrorMessages()) && MapUtils.isNotEmpty(
109                 MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, translatorOutput.getErrorMessages()))) {
110             throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
111                     "Error in validation " + getErrorAsString(translatorOutput.getErrorMessages()))
112                                                                       .withId("Validation Error")
113                                                                       .withCategory(ErrorCategory.APPLICATION).build());
114         }
115
116         return new ToscaFileOutputServiceCsarImpl().createOutputFile(translatorOutput.getToscaServiceModel(), null);
117     }
118
119 }