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