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