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