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