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