re base code
[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  * Copyright © 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.enrichment.impl.tosca;
18
19 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
20 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
21 import org.openecomp.core.utilities.file.FileUtils;
22 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
23 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
24 import org.openecomp.sdc.tosca.services.ToscaUtil;
25 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
26
27 import java.io.*;
28 import java.net.URL;
29 import java.nio.file.NotDirectoryException;
30 import java.util.*;
31 import java.util.zip.ZipEntry;
32 import java.util.zip.ZipInputStream;
33
34 import static org.junit.Assert.assertEquals;
35
36 public class BaseToscaEnrichmentTest {
37
38     protected String outputFilesPath;
39
40     public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
41                                                            String globalServiceTemplatesPath,
42                                                            String entryDefinitionServiceTemplate)
43         throws IOException {
44         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
45         Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
46         if (entryDefinitionServiceTemplate == null) {
47             entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
48         }
49
50         loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
51         if (globalServiceTemplatesPath != null) {
52             loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
53         }
54
55         return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
56     }
57
58     private static void loadServiceTemplates(String serviceTemplatesPath,
59                                              ToscaExtensionYamlUtil toscaExtensionYamlUtil,
60                                              Map<String, ServiceTemplate> serviceTemplates)
61         throws IOException {
62         URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
63         if (urlFile != null) {
64             File pathFile = new File(urlFile.getFile());
65             Collection<File> files = org.apache.commons.io.FileUtils.listFiles(pathFile, null, true);
66             if (files != null) {
67                 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
68             } else {
69                 throw new NotDirectoryException(serviceTemplatesPath);
70             }
71         } else {
72             throw new NotDirectoryException(serviceTemplatesPath);
73         }
74     }
75
76     private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
77                                                 Collection<File> files,
78                                                 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
79         throws IOException {
80         for (File file : files) {
81             try (InputStream yamlFile = new FileInputStream(file)) {
82                 ServiceTemplate serviceTemplateFromYaml =
83                     toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
84                 serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplateFromYaml), serviceTemplateFromYaml);
85             }
86         }
87     }
88
89
90     /*public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
91                                                           String globalServiceTemplatesPath,
92                                                           String entryDefinitionServiceTemplate)
93         throws IOException {
94         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
95         Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
96         if (entryDefinitionServiceTemplate == null) {
97             entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
98         }
99
100         loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
101         if (globalServiceTemplatesPath != null) {
102             loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
103         }
104
105         return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
106     }
107
108     private static void loadServiceTemplates(String serviceTemplatesPath,
109                                              ToscaExtensionYamlUtil toscaExtensionYamlUtil,
110                                              Map<String, ServiceTemplate> serviceTemplates)
111         throws IOException {
112         URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath);
113         if (urlFile != null) {
114             File pathFile = new File(urlFile.getFile());
115             File[] files = pathFile.listFiles();
116             if (files != null) {
117                 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
118             } else {
119                 throw new NotDirectoryException(serviceTemplatesPath);
120             }
121         } else {
122             throw new NotDirectoryException(serviceTemplatesPath);
123         }
124     }
125
126     private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
127                                                 File[] files,
128                                                 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
129         throws IOException {
130         for (File file : files) {
131             try (InputStream yamlFile = new FileInputStream(file)) {
132                 ServiceTemplate serviceTemplateFromYaml =
133                     toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
134                 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
135                 try {
136                     yamlFile.close();
137                 } catch (IOException ignore) {
138                 }
139             } catch (FileNotFoundException e) {
140                 throw e;
141             } catch (IOException e) {
142                 throw e;
143             }
144         }
145     }*/
146
147     void compareActualAndExpectedModel(ToscaServiceModel toscaServiceModel) throws IOException {
148
149         ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
150         byte[] toscaActualFile = toscaFileOutputService.createOutputFile(toscaServiceModel, null);
151
152         URL url = BaseToscaEnrichmentTest.class.getResource(outputFilesPath);
153         Set<String> expectedResultFileNameSet = new HashSet<>();
154         Map<String, byte[]> expectedResultMap = new HashMap<>();
155         String path = url.getPath();
156         File pathFile = new File(path);
157         File[] files = pathFile.listFiles();
158         org.junit.Assert.assertNotNull("model is empty", files);
159         for (File expectedFile : files) {
160             expectedResultFileNameSet.add(expectedFile.getName());
161             try (FileInputStream input = new FileInputStream(expectedFile)) {
162                 expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
163             }
164         }
165
166         try (InputStream fis = new ByteArrayInputStream(toscaActualFile);
167              ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
168             ZipEntry entry;
169             String name;
170             String expected;
171             String actual;
172
173             while ((entry = zis.getNextEntry()) != null) {
174                 name = entry.getName()
175                         .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
176                 if (expectedResultFileNameSet.contains(name)) {
177                     expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
178                     actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
179                     assertEquals("difference in file: " + name, expected, actual);
180
181                     expectedResultFileNameSet.remove(name);
182                 }
183             }
184             if (expectedResultFileNameSet.isEmpty()) {
185                 expectedResultFileNameSet.forEach(System.out::println);
186             }
187         }
188         assertEquals(0, expectedResultFileNameSet.size());
189     }
190 }