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