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