1 package org.openecomp.core.converter.impl;
3 import org.apache.commons.collections.CollectionUtils;
4 import org.junit.Assert;
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;
17 import java.io.FileInputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
21 import java.nio.file.NotDirectoryException;
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;
28 public class ToscaConverterImplTest {
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";
37 public void testConvertMainSt() throws IOException {
38 String inputFilesPath = BASE_DIR + "/convertMainSt/in";
39 String outputFilesPath = BASE_DIR + "/convertMainSt/out";
41 convertAndValidate(inputFilesPath, outputFilesPath);
45 public void testNodesConversion() throws IOException {
46 String inputFilesPath = BASE_DIR + "/convertCsar/in";
47 String outputFilesPath = BASE_DIR + "/convertCsar/out";
49 convertAndValidate(inputFilesPath, outputFilesPath);
53 public void testParameterConversion() throws IOException {
54 String inputFilesPath = BASE_DIR + "/convertParameters/in";
55 String outputFilesPath = BASE_DIR + "/convertParameters/out";
57 convertAndValidate(inputFilesPath, outputFilesPath);
61 public void testConversionWithInt() throws IOException {
62 String inputFilesPath = BASE_DIR + "/conversionWithInt/in";
63 String outputFilesPath = BASE_DIR + "/conversionWithInt/out";
65 convertAndValidate(inputFilesPath, outputFilesPath);
69 public void testOccurrencesUpperString() {
70 Object[] occurrences = buildOccurrences("0", UNBOUNDED);
71 Assert.assertEquals(occurrences[0], 0);
72 Assert.assertEquals(occurrences[1], UNBOUNDED);
76 public void testOccurrencesAsInts() {
77 Object[] occurrences = buildOccurrences("0", "1");
78 Assert.assertEquals(occurrences[0], 0);
79 Assert.assertEquals(occurrences[1], 1);
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);
91 public void testOccurrencesLowerString() {
92 Object[] occurrences = buildOccurrences(UNBOUNDED, "100");
93 Assert.assertEquals(occurrences[0], UNBOUNDED);
94 Assert.assertEquals(occurrences[1], 100);
98 public void testOccurrencesEmpty() {
99 Object[] occurrences = buildOccurrences();
100 Assert.assertEquals(occurrences.length, 0);
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);
113 public void testDefaultOccurrences() {
114 Object[] occurrences = buildOccurrences((List<String>) null);
115 Assert.assertEquals(1, occurrences[0]);
116 Assert.assertEquals(1, occurrences[1]);
119 private Object[] buildOccurrences(String... bounds) {
120 return buildOccurrences(Arrays.asList(bounds));
123 private void convertAndValidate(String inputFilesPath, String outputFilesPath)
125 FileContentHandler fileContentHandler =
126 createFileContentHandlerFromInput(inputFilesPath);
128 ToscaServiceModel toscaServiceModel = toscaConverter.convert(fileContentHandler);
129 validateConvertorOutput(outputFilesPath, toscaServiceModel);
132 private void validateConvertorOutput(String outputFilesPath, ToscaServiceModel toscaServiceModel)
134 ServiceTemplate mainSt = toscaServiceModel.getServiceTemplates().get(mainStName);
135 Map<String, ServiceTemplate> expectedOutserviceTemplates = new HashMap<>();
136 loadServiceTemplates(outputFilesPath, new ToscaExtensionYamlUtil(),
137 expectedOutserviceTemplates);
139 checkSTResults(expectedOutserviceTemplates, null, mainSt);
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();
154 private FileContentHandler createFileContentHandlerFromInput(String inputFilesPath)
156 URL inputFilesUrl = this.getClass().getResource(inputFilesPath);
157 String path = inputFilesUrl.getPath();
158 File directory = new File(path);
159 File[] listFiles = directory.listFiles();
161 FileContentHandler fileContentHandler = new FileContentHandler();
162 insertFilesIntoFileContentHandler(listFiles, fileContentHandler);
163 return fileContentHandler;
166 private void insertFilesIntoFileContentHandler(File[] listFiles,
167 FileContentHandler fileContentHandler)
170 if(CollectionUtils.isEmpty(fileContentHandler.getFileList())) {
171 fileContentHandler.setFiles(new HashMap<>());
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);
181 File[] currFileList = file.listFiles();
182 insertFilesIntoFileContentHandler(currFileList, fileContentHandler);
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));
197 if (Objects.nonNull(mainServiceTemplate)) {
198 assertEquals("difference main service template: ",
199 yamlUtil.objectToYaml(expectedOutserviceTemplates.get(mainStName)),
200 yamlUtil.objectToYaml(mainServiceTemplate));
204 public static void loadServiceTemplates(String serviceTemplatesPath,
205 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
206 Map<String, ServiceTemplate> serviceTemplates)
208 URL urlFile = ToscaConverterImplTest.class.getResource(serviceTemplatesPath);
209 if (urlFile != null) {
210 File pathFile = new File(urlFile.getFile());
211 File[] files = pathFile.listFiles();
213 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
215 throw new NotDirectoryException(serviceTemplatesPath);
218 throw new NotDirectoryException(serviceTemplatesPath);
222 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
224 ToscaExtensionYamlUtil toscaExtensionYamlUtil) throws IOException {
226 for (File file : files) {
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);
237 private static void createConcreteRequirementObjectsInServiceTemplate(ServiceTemplate
238 serviceTemplateFromYaml,
239 ToscaExtensionYamlUtil
240 toscaExtensionYamlUtil) {
242 if (serviceTemplateFromYaml == null
243 || serviceTemplateFromYaml.getTopology_template() == null
244 || serviceTemplateFromYaml.getTopology_template().getNode_templates() == null) {
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
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();
270 requirements.clear();
271 requirements.addAll(concreteRequirementList);
272 nodeTemplate.setRequirements(requirements);
274 System.out.println();
275 //toscaExtensionYamlUtil.yamlToObject(nodeTemplate, NodeTemplate.class);