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