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