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