Rename packages from openecomp to onap.
[sdc.git] / openecomp-be / lib / openecomp-tosca-converter-lib / openecomp-tosca-converter-core / src / test / java / org / openecomp / core / converter / impl / ToscaConverterImplTest.java
1 package org.openecomp.core.converter.impl;
2
3 import org.apache.commons.collections.CollectionUtils;
4 import org.junit.Assert;
5 import org.junit.Test;
6 import org.openecomp.core.converter.ToscaConverter;
7 import org.openecomp.core.impl.ToscaConverterImpl;
8 import org.openecomp.core.utilities.file.FileContentHandler;
9 import org.openecomp.core.utilities.file.FileUtils;
10 import org.openecomp.core.utilities.json.JsonUtil;
11 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
12 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
13 import org.onap.sdc.tosca.datatypes.model.NodeType;
14 import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
15 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
16 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
17 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
18 import org.onap.sdc.tosca.services.YamlUtil;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.URL;
25 import java.nio.file.NotDirectoryException;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.ListIterator;
31 import java.util.Map;
32 import java.util.Objects;
33
34 import static org.junit.Assert.assertEquals;
35 import static org.openecomp.core.converter.datatypes.Constants.globalStName;
36 import static org.openecomp.core.converter.datatypes.Constants.mainStName;
37
38 public class ToscaConverterImplTest {
39
40   private static final ToscaConverter toscaConverter = new ToscaConverterImpl();
41   private static final String VIRTUAL_LINK = "virtualLink";
42   private static final String UNBOUNDED = "UNBOUNDED";
43   private static final String BASE_DIR = "/mock/toscaConverter";
44
45
46   @Test
47   public void testConvertMainSt() throws IOException {
48     String inputFilesPath = BASE_DIR + "/convertMainSt/in";
49     String outputFilesPath = BASE_DIR + "/convertMainSt/out";
50
51     convertAndValidate(inputFilesPath, outputFilesPath);
52   }
53
54   @Test
55   public void testNodesConversion() throws IOException {
56     String inputFilesPath = BASE_DIR + "/convertCsar/in";
57     String outputFilesPath = BASE_DIR + "/convertCsar/out";
58
59     convertAndValidate(inputFilesPath, outputFilesPath);
60   }
61
62   @Test
63   public void testParameterConversion() throws IOException {
64     String inputFilesPath = BASE_DIR + "/convertParameters/in";
65     String outputFilesPath = BASE_DIR + "/convertParameters/out";
66
67     convertAndValidate(inputFilesPath, outputFilesPath);
68   }
69
70   @Test
71   public void testConversionWithInt() throws IOException {
72     String inputFilesPath = BASE_DIR + "/conversionWithInt/in";
73     String outputFilesPath = BASE_DIR + "/conversionWithInt/out";
74
75     convertAndValidate(inputFilesPath, outputFilesPath);
76   }
77
78   @Test
79   public void testOccurrencesUpperString() {
80     Object[] occurrences = buildOccurrences("0", UNBOUNDED);
81     Assert.assertEquals(occurrences[0], 0);
82     Assert.assertEquals(occurrences[1], UNBOUNDED);
83   }
84
85   @Test
86   public void testOccurrencesAsInts() {
87     Object[] occurrences = buildOccurrences("0", "1");
88     Assert.assertEquals(occurrences[0], 0);
89     Assert.assertEquals(occurrences[1], 1);
90   }
91
92   @Test
93   public void testOccurrencesAsStrings() {
94     String test = "TEST_A";
95     Object[] occurrences = buildOccurrences(UNBOUNDED, test);
96     Assert.assertEquals(occurrences[0], UNBOUNDED);
97     Assert.assertEquals(occurrences[1], test);
98   }
99
100   @Test
101   public void testOccurrencesLowerString() {
102     Object[] occurrences = buildOccurrences(UNBOUNDED, "100");
103     Assert.assertEquals(occurrences[0], UNBOUNDED);
104     Assert.assertEquals(occurrences[1], 100);
105   }
106
107   @Test
108   public void testOccurrencesEmpty() {
109     Object[] occurrences = buildOccurrences();
110     Assert.assertEquals(occurrences.length, 0);
111   }
112
113   @Test
114   public void testOccurrencesMany() {
115     String test = "TEST_B";
116     Object[] occurrences = buildOccurrences("1", "2", test);
117     Assert.assertEquals(occurrences[0], 1);
118     Assert.assertEquals(occurrences[1], 2);
119     Assert.assertEquals(occurrences[2], test);
120   }
121
122   @Test
123   public void testDefaultOccurrences() {
124     Object[] occurrences = buildOccurrences((List<String>) null);
125     Assert.assertEquals(1, occurrences[0]);
126     Assert.assertEquals(1, occurrences[1]);
127   }
128
129   private Object[] buildOccurrences(String... bounds) {
130     return buildOccurrences(Arrays.asList(bounds));
131   }
132
133   private void convertAndValidate(String inputFilesPath, String outputFilesPath)
134       throws IOException {
135     FileContentHandler fileContentHandler =
136         createFileContentHandlerFromInput(inputFilesPath);
137
138     ToscaServiceModel toscaServiceModel = toscaConverter.convert(fileContentHandler);
139     validateConvertorOutput(outputFilesPath, toscaServiceModel);
140   }
141
142   private void validateConvertorOutput(String outputFilesPath, ToscaServiceModel toscaServiceModel)
143       throws IOException {
144     ServiceTemplate mainSt = toscaServiceModel.getServiceTemplates().get(mainStName);
145     Map<String, ServiceTemplate> expectedOutserviceTemplates = new HashMap<>();
146     loadServiceTemplates(outputFilesPath, new ToscaExtensionYamlUtil(),
147         expectedOutserviceTemplates);
148
149     checkSTResults(expectedOutserviceTemplates, null, mainSt);
150   }
151
152   private Object[] buildOccurrences(List<String> bounds) {
153     NodeType nodeType = JsonUtil.json2Object("{derived_from=tosca.nodes.Root, description=MME_VFC, " +
154             "properties={vendor={type=string, default=ERICSSON}, " +
155             "csarVersion={type=string, default=v1.0}, csarProvider={type=string, default=ERICSSON}, " +
156             "id={type=string, default=vMME}, version={type=string, default=v1.0}, csarType={type=string, default=NFAR}}, " +
157             "requirements=[{virtualLink={" +
158             (bounds == null ? "" : "occurrences=[" + String.join(", ", bounds) +   "], ") +
159             "capability=tosca.capabilities.network.Linkable}}]}", NodeType.class);
160     List<Map<String, RequirementDefinition>> requirements = nodeType.getRequirements();
161     return requirements.get(0).get(VIRTUAL_LINK).getOccurrences();
162   }
163
164   private FileContentHandler createFileContentHandlerFromInput(String inputFilesPath)
165       throws IOException {
166     URL inputFilesUrl = this.getClass().getResource(inputFilesPath);
167     String path = inputFilesUrl.getPath();
168     File directory = new File(path);
169     File[] listFiles = directory.listFiles();
170
171     FileContentHandler fileContentHandler = new FileContentHandler();
172     insertFilesIntoFileContentHandler(listFiles, fileContentHandler);
173     return fileContentHandler;
174   }
175
176   private void insertFilesIntoFileContentHandler(File[] listFiles,
177                                                  FileContentHandler fileContentHandler)
178       throws IOException {
179     byte[] fileContent;
180     if(CollectionUtils.isEmpty(fileContentHandler.getFileList())) {
181       fileContentHandler.setFiles(new HashMap<>());
182     }
183
184     for (File file : listFiles) {
185       if(!file.isDirectory()) {
186         try (FileInputStream fis = new FileInputStream(file)) {
187           fileContent = FileUtils.toByteArray(fis);
188           fileContentHandler.addFile(file.getPath(), fileContent);
189         }
190       }else{
191         File[] currFileList = file.listFiles();
192         insertFilesIntoFileContentHandler(currFileList, fileContentHandler);
193       }
194
195     }
196   }
197
198   private void checkSTResults(
199       Map<String, ServiceTemplate> expectedOutserviceTemplates,
200       ServiceTemplate gloablSubstitutionServiceTemplate, ServiceTemplate mainServiceTemplate) {
201     YamlUtil yamlUtil = new YamlUtil();
202     if (Objects.nonNull(gloablSubstitutionServiceTemplate)) {
203       assertEquals("difference global substitution service template: ",
204           yamlUtil.objectToYaml(expectedOutserviceTemplates.get(globalStName)),
205           yamlUtil.objectToYaml(gloablSubstitutionServiceTemplate));
206     }
207     if (Objects.nonNull(mainServiceTemplate)) {
208       assertEquals("difference main service template: ",
209           yamlUtil.objectToYaml(expectedOutserviceTemplates.get(mainStName)),
210           yamlUtil.objectToYaml(mainServiceTemplate));
211     }
212   }
213
214   public static void loadServiceTemplates(String serviceTemplatesPath,
215                                           ToscaExtensionYamlUtil toscaExtensionYamlUtil,
216                                           Map<String, ServiceTemplate> serviceTemplates)
217       throws IOException {
218     URL urlFile = ToscaConverterImplTest.class.getResource(serviceTemplatesPath);
219     if (urlFile != null) {
220       File pathFile = new File(urlFile.getFile());
221       File[] files = pathFile.listFiles();
222       if (files != null) {
223         addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
224       } else {
225         throw new NotDirectoryException(serviceTemplatesPath);
226       }
227     } else {
228       throw new NotDirectoryException(serviceTemplatesPath);
229     }
230   }
231
232   private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
233                                               File[] files,
234                                               ToscaExtensionYamlUtil toscaExtensionYamlUtil) throws IOException {
235
236     for (File file : files) {
237
238       try (InputStream yamlFile = new FileInputStream(file)) {
239         ServiceTemplate serviceTemplateFromYaml =
240             toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
241         createConcreteRequirementObjectsInServiceTemplate(serviceTemplateFromYaml, toscaExtensionYamlUtil);
242         serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
243       }
244     }
245   }
246
247   private static void createConcreteRequirementObjectsInServiceTemplate(ServiceTemplate
248                                                                             serviceTemplateFromYaml,
249                                                                         ToscaExtensionYamlUtil
250                                                                             toscaExtensionYamlUtil) {
251
252     if (serviceTemplateFromYaml == null
253         || serviceTemplateFromYaml.getTopology_template() == null
254         || serviceTemplateFromYaml.getTopology_template().getNode_templates() == null) {
255       return;
256     }
257
258     //Creating concrete objects
259     Map<String, NodeTemplate> nodeTemplates =
260         serviceTemplateFromYaml.getTopology_template().getNode_templates();
261     for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) {
262       NodeTemplate nodeTemplate = entry.getValue();
263       List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements();
264       List<Map<String, RequirementAssignment>> concreteRequirementList = new ArrayList<>();
265       if (requirements != null) {
266         ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements
267             .listIterator();
268         while (reqListIterator.hasNext()){
269           Map<String, RequirementAssignment> requirement = reqListIterator.next();
270           Map<String, RequirementAssignment> concreteRequirement = new HashMap<>();
271           for (Map.Entry<String, RequirementAssignment> reqEntry : requirement.entrySet()) {
272             RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil
273                 .yamlToObject(toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()),
274                     RequirementAssignment.class));
275             concreteRequirement.put(reqEntry.getKey(), requirementAssignment);
276             concreteRequirementList.add(concreteRequirement);
277             reqListIterator.remove();
278           }
279         }
280         requirements.clear();
281         requirements.addAll(concreteRequirementList);
282         nodeTemplate.setRequirements(requirements);
283       }
284       System.out.println();
285       //toscaExtensionYamlUtil.yamlToObject(nodeTemplate, NodeTemplate.class);
286     }
287   }
288 }