8126d2967cc8e20a2bfd6e89bac302ba47dd6076
[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     if (!ToggleableFeature.FORWARDER_CAPABILITY.isActive()) {
68       manager.enable(ToggleableFeature.FORWARDER_CAPABILITY);
69     }
70     if (!ToggleableFeature.ANNOTATIONS.isActive()) {
71       manager.enable(ToggleableFeature.ANNOTATIONS);
72     }
73     if(!ToggleableFeature.VLAN_TAGGING.isActive()) {
74       manager.enable(ToggleableFeature.VLAN_TAGGING);
75     }
76   }
77
78
79   @AfterClass
80   public static void disableToggleableFeatures() {
81     manager.disable(ToggleableFeature.FORWARDER_CAPABILITY);
82     manager.disable(ToggleableFeature.ANNOTATIONS);
83     manager.disable(ToggleableFeature.VLAN_TAGGING);
84     manager = null;
85     TestFeatureManagerProvider.setFeatureManager(null);
86   }
87
88   protected void testTranslationWithInit(String path) throws IOException {
89     File translatedZipFile = initTranslatorAndTranslate(path);
90     testTranslation(path, translatedZipFile);
91   }
92
93   protected File initTranslatorAndTranslate(String path) throws IOException {
94     HeatToToscaTranslator heatToToscaTranslator = HeatToToscaTranslatorFactory.getInstance().createInterface();
95     return translateZipFile(path, heatToToscaTranslator);
96   }
97
98   protected void testTranslation(String basePath, File translatedZipFile) throws IOException {
99
100     URL url = BaseFullTranslationTest.class.getResource(basePath + OUT_POSTFIX);
101     Set<String> expectedResultFileNameSet = new HashSet<>();
102     Map<String, byte[]> expectedResultMap = new HashMap<>();
103
104     String path = url.getPath();
105     File pathFile = new File(path);
106     File[] files = pathFile.listFiles();
107     Assert.assertNotNull("manifest files is empty", files);
108     for (File expectedFile : files) {
109       expectedResultFileNameSet.add(expectedFile.getName());
110       try (FileInputStream input = new FileInputStream(expectedFile)) {
111         expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
112       }
113     }
114
115     try (FileInputStream fis = new FileInputStream(translatedZipFile);
116          ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
117       ZipEntry entry;
118       String name;
119       String expected;
120       String actual;
121
122       while ((entry = zis.getNextEntry()) != null) {
123
124         name = entry.getName()
125             .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
126         if (expectedResultFileNameSet.contains(name)) {
127           expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
128           actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
129           assertEquals("difference in file: " + name, expected, actual);
130
131           expectedResultFileNameSet.remove(name);
132         }
133       }
134       if (expectedResultFileNameSet.isEmpty()) {
135         expectedResultFileNameSet.forEach(System.out::println);
136       }
137     }
138     assertEquals(0, expectedResultFileNameSet.size());
139   }
140
141   private File translateZipFile(String basePath, HeatToToscaTranslator heatToToscaTranslator) throws IOException {
142     URL inputFilesUrl = this.getClass().getResource(basePath + IN_POSTFIX);
143     String path = inputFilesUrl.getPath();
144     TestUtils.addFilesToTranslator(heatToToscaTranslator, path);
145     TranslatorOutput translatorOutput = heatToToscaTranslator.translate();
146     Assert.assertNotNull(translatorOutput);
147     if (MapUtils.isNotEmpty(translatorOutput.getErrorMessages()) && MapUtils.isNotEmpty(
148         MessageContainerUtil
149             .getMessageByLevel(ErrorLevel.ERROR, translatorOutput.getErrorMessages()))) {
150       throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
151           "Error in validation " + getErrorAsString(translatorOutput.getErrorMessages()))
152           .withId("Validation Error").withCategory(ErrorCategory.APPLICATION).build());
153     }
154
155     File file = new File(path + "/VSP.zip");
156     file.createNewFile();
157
158     try (FileOutputStream fos = new FileOutputStream(file)) {
159       ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
160       fos.write(
161           toscaFileOutputService.createOutputFile(translatorOutput.getToscaServiceModel(), null));
162     }
163
164     return file;
165   }
166
167   private String getErrorAsString(Map<String, List<ErrorMessage>> errorMessages) {
168     StringBuilder sb = new StringBuilder();
169     errorMessages.entrySet().forEach(
170         entry -> sb.append("File:").append(entry.getKey()).append(System.lineSeparator())
171             .append(getErrorList(entry.getValue())));
172
173     return sb.toString();
174   }
175
176   private String getErrorList(List<ErrorMessage> errors) {
177     StringBuilder sb = new StringBuilder();
178     errors.forEach(
179         error -> sb.append(error.getMessage()).append("[").append(error.getLevel()).append("]")
180             .append(System.lineSeparator()));
181     return sb.toString();
182   }
183
184 }