366bd82eabab81a732484d981389824d4298be11
[sdc.git] / openecomp-be / lib / openecomp-sdc-enrichment-lib / openecomp-sdc-enrichment-impl / src / test / java / org / openecomp / sdc / enrichment / impl / tosca / BaseToscaEnrichmentTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.enrichment.impl.tosca;
22
23 import org.openecomp.core.utilities.file.FileUtils;
24 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
25 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
26 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
27 import org.openecomp.sdc.tosca.services.ToscaUtil;
28 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
29 import org.openecomp.sdc.tosca.services.yamlutil.ToscaExtensionYamlUtil;
30
31 import java.io.*;
32 import java.net.URL;
33 import java.nio.file.NotDirectoryException;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.Map;
38 import java.util.Set;
39 import java.util.zip.ZipEntry;
40 import java.util.zip.ZipInputStream;
41
42 import static org.junit.Assert.assertEquals;
43
44 public class BaseToscaEnrichmentTest {
45
46     protected String outputFilesPath;
47
48     public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
49                                                            String globalServiceTemplatesPath,
50                                                            String entryDefinitionServiceTemplate)
51         throws IOException {
52         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
53         Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
54         if (entryDefinitionServiceTemplate == null) {
55             entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
56         }
57
58         loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
59         if (globalServiceTemplatesPath != null) {
60             loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
61         }
62
63         return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
64     }
65
66     private static void loadServiceTemplates(String serviceTemplatesPath,
67                                              ToscaExtensionYamlUtil toscaExtensionYamlUtil,
68                                              Map<String, ServiceTemplate> serviceTemplates)
69         throws IOException {
70         URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
71         if (urlFile != null) {
72             File pathFile = new File(urlFile.getFile());
73             Collection<File> files = org.apache.commons.io.FileUtils.listFiles(pathFile, null, true);
74             if (files != null) {
75                 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
76             } else {
77                 throw new NotDirectoryException(serviceTemplatesPath);
78             }
79         } else {
80             throw new NotDirectoryException(serviceTemplatesPath);
81         }
82     }
83
84     private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
85                                                 Collection<File> files,
86                                                 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
87         throws IOException {
88         for (File file : files) {
89             try (InputStream yamlFile = new FileInputStream(file)) {
90                 ServiceTemplate serviceTemplateFromYaml =
91                     toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
92                 serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplateFromYaml), serviceTemplateFromYaml);
93                 try {
94                     yamlFile.close();
95                 } catch (IOException ignore) {
96                 }
97             } catch (FileNotFoundException exception) {
98                 throw exception;
99             } catch (IOException exception) {
100                 throw exception;
101             }
102         }
103     }
104
105
106     /*public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
107                                                           String globalServiceTemplatesPath,
108                                                           String entryDefinitionServiceTemplate)
109         throws IOException {
110         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
111         Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
112         if (entryDefinitionServiceTemplate == null) {
113             entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
114         }
115
116         loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
117         if (globalServiceTemplatesPath != null) {
118             loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
119         }
120
121         return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
122     }
123
124     private static void loadServiceTemplates(String serviceTemplatesPath,
125                                              ToscaExtensionYamlUtil toscaExtensionYamlUtil,
126                                              Map<String, ServiceTemplate> serviceTemplates)
127         throws IOException {
128         URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
129         if (urlFile != null) {
130             File pathFile = new File(urlFile.getFile());
131             File[] files = pathFile.listFiles();
132             if (files != null) {
133                 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
134             } else {
135                 throw new NotDirectoryException(serviceTemplatesPath);
136             }
137         } else {
138             throw new NotDirectoryException(serviceTemplatesPath);
139         }
140     }
141
142     private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
143                                                 File[] files,
144                                                 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
145         throws IOException {
146         for (File file : files) {
147             try (InputStream yamlFile = new FileInputStream(file)) {
148                 ServiceTemplate serviceTemplateFromYaml =
149                     toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
150                 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
151                 try {
152                     yamlFile.close();
153                 } catch (IOException ignore) {
154                 }
155             } catch (FileNotFoundException e) {
156                 throw e;
157             } catch (IOException e) {
158                 throw e;
159             }
160         }
161     }*/
162
163     void compareActualAndExpectedModel(ToscaServiceModel toscaServiceModel) throws IOException {
164
165         ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
166         byte[] toscaActualFile = toscaFileOutputService.createOutputFile(toscaServiceModel, null);
167
168         URL url = BaseToscaEnrichmentTest.class.getResource(outputFilesPath);
169         Set<String> expectedResultFileNameSet = new HashSet<>();
170         Map<String, byte[]> expectedResultMap = new HashMap<>();
171         String path = url.getPath();
172         File pathFile = new File(path);
173         File[] files = pathFile.listFiles();
174         org.junit.Assert.assertNotNull("model is empty", files);
175         for (File expectedFile : files) {
176             expectedResultFileNameSet.add(expectedFile.getName());
177             try (FileInputStream input = new FileInputStream(expectedFile)) {
178                 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
179             }
180         }
181
182         try (InputStream fis = new ByteArrayInputStream(toscaActualFile);
183              ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
184             ZipEntry entry;
185             String name;
186             String expected;
187             String actual;
188
189             while ((entry = zis.getNextEntry()) != null) {
190                 name = entry.getName()
191                         .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
192                 if (expectedResultFileNameSet.contains(name)) {
193                     expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
194                     actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
195                     assertEquals("difference in file: " + name, expected, actual);
196
197                     expectedResultFileNameSet.remove(name);
198                 }
199             }
200             if (expectedResultFileNameSet.isEmpty()) {
201                 expectedResultFileNameSet.forEach(System.out::println);
202             }
203         }
204         assertEquals(0, expectedResultFileNameSet.size());
205     }
206 }