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