ffd6107e0820d1acd7dd4947d24bae2bdab3996a
[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;
18
19 import org.openecomp.core.enrichment.factory.EnrichmentManagerFactory;
20 import org.openecomp.core.utilities.file.FileUtils;
21 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
22 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
23 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
24 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
25 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
26 import org.testng.Assert;
27 import org.testng.annotations.Test;
28
29 import java.io.BufferedInputStream;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.net.URL;
36 import java.nio.file.NotDirectoryException;
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
47 public class EnrichmentManagerImplTest {
48
49   private 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 = EnrichmentManagerImplTest.class.getResource(serviceTemplatesPath);
72     if (urlFile != null) {
73       File pathFile = new File(urlFile.getFile());
74       File[] files = pathFile.listFiles();
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                                               File[] files,
87                                               ToscaExtensionYamlUtil toscaExtensionYamlUtil)
88       throws IOException {
89     for (File file : files) {
90       if (!file.getName().equals("CSR.zip") && !file.isDirectory()) {
91         try (InputStream yamlFile = new FileInputStream(file)) {
92           ServiceTemplate serviceTemplateFromYaml =
93               toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
94           serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
95         }
96       }
97     }
98   }
99
100   @Test
101   public void testEnrichmentManagerImpl() throws Exception {
102     Assert.assertTrue(
103         EnrichmentManagerFactory.getInstance().createInterface() instanceof EnrichmentManagerImpl);
104   }
105
106   private File getToscaModelAsFile(ToscaServiceModel toscaServiceModel) throws IOException {
107
108     URL inputFilesUrl = EnrichmentManagerImplTest.class.getResource("/mock/enrich/input");
109     String path = inputFilesUrl.getPath();
110
111
112     File file = new File(path + "/" + "CSR.zip");
113     file.createNewFile();
114
115     try (FileOutputStream fos = new FileOutputStream(file))
116
117     {
118       ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl();
119       fos.write(toscaFileOutputService.createOutputFile(toscaServiceModel, null));
120     }
121
122     return file;
123   }
124
125   protected void compareActualAndExpected(File actualFile, String expectedOutputPath)
126       throws IOException {
127
128     URL url = EnrichmentManagerImplTest.class.getResource(expectedOutputPath);
129     Set<String> expectedResultFileNameSet = new HashSet<>();
130     Map<String, byte[]> expectedResultMap = new HashMap<>();
131     String path = url.getPath();
132     File pathFile = new File(path);
133     File[] files = pathFile.listFiles();
134     org.junit.Assert.assertNotNull("model is empty", files);
135     for (File expectedFile : files) {
136       expectedResultFileNameSet.add(expectedFile.getName());
137       try (FileInputStream input = new FileInputStream(expectedFile)) {
138         expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input));
139       }
140     }
141
142     try (FileInputStream fis = new FileInputStream(actualFile);
143          ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
144       ZipEntry entry;
145       String name;
146       String expected;
147       String actual;
148
149       while ((entry = zis.getNextEntry()) != null) {
150
151         name = entry.getName()
152             .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length());
153         if (expectedResultFileNameSet.contains(name)) {
154           expected = new String(expectedResultMap.get(name)).trim().replace("\r", "");
155           actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", "");
156           assertEquals("difference in file: " + name, expected, actual);
157
158           expectedResultFileNameSet.remove(name);
159         }
160       }
161       if (expectedResultFileNameSet.isEmpty()) {
162         expectedResultFileNameSet.forEach(System.out::println);
163       }
164     }
165     assertEquals(0, expectedResultFileNameSet.size());
166   }
167
168
169 }