From c8a540b3c234449163f6fb1899807bba951113b4 Mon Sep 17 00:00:00 2001 From: shrek2000 Date: Mon, 11 Sep 2017 15:45:37 +0300 Subject: Create new VSP, onboard from TOSCA file - UI Change-Id: I018c6d07a4b9ec7e6b1507ab37e2550865423cfe Issue-ID: SDC-230 Signed-off-by: shrek2000 --- .../converter/impl/ToscaConverterImplTest.java | 193 +++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/ToscaConverterImplTest.java (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/ToscaConverterImplTest.java') diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/ToscaConverterImplTest.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/ToscaConverterImplTest.java new file mode 100644 index 0000000000..4abed3e316 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/ToscaConverterImplTest.java @@ -0,0 +1,193 @@ +package org.openecomp.core.converter.impl; + +import org.apache.commons.collections.CollectionUtils; +import org.junit.Test; +import org.openecomp.core.converter.ToscaConverter; +import org.openecomp.core.impl.ToscaConverterImpl; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; +import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment; +import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil; +import org.openecomp.sdc.tosca.services.YamlUtil; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.file.NotDirectoryException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.Objects; + +import static org.junit.Assert.assertEquals; +import static org.openecomp.core.converter.datatypes.Constants.globalStName; +import static org.openecomp.core.converter.datatypes.Constants.mainStName; + +public class ToscaConverterImplTest { + + private static ToscaConverter toscaConverter = new ToscaConverterImpl(); + private static String inputFilesPath; + private static String outputFilesPath; + private static Map expectedOutserviceTemplates; + + + @Test + public void testConvertMainSt() throws IOException { + inputFilesPath = "/mock/toscaConverter/convertMainSt/in"; + outputFilesPath = "/mock/toscaConverter/convertMainSt/out"; + + FileContentHandler fileContentHandler = + createFileContentHandlerFromInput(inputFilesPath); + + expectedOutserviceTemplates = new HashMap<>(); + loadServiceTemplates(outputFilesPath, new ToscaExtensionYamlUtil(), + expectedOutserviceTemplates); + + ToscaServiceModel toscaServiceModel = toscaConverter.convert(fileContentHandler); + ServiceTemplate mainSt = toscaServiceModel.getServiceTemplates().get(mainStName); + + checkSTResults(expectedOutserviceTemplates, null, mainSt); + } + + + + private FileContentHandler createFileContentHandlerFromInput(String inputFilesPath) + throws IOException { + URL inputFilesUrl = this.getClass().getResource(inputFilesPath); + String path = inputFilesUrl.getPath(); + File directory = new File(path); + File[] listFiles = directory.listFiles(); + + FileContentHandler fileContentHandler = new FileContentHandler(); + insertFilesIntoFileContentHandler(listFiles, fileContentHandler); + return fileContentHandler; + } + + private void insertFilesIntoFileContentHandler(File[] listFiles, + FileContentHandler fileContentHandler) + throws IOException { + byte[] fileContent; + if(CollectionUtils.isEmpty(fileContentHandler.getFileList())) { + fileContentHandler.setFiles(new HashMap<>()); + } + + for (File file : listFiles) { + if(!file.isDirectory()) { + try (FileInputStream fis = new FileInputStream(file)) { + fileContent = FileUtils.toByteArray(fis); + fileContentHandler.addFile(file.getPath(), fileContent); + } + }else{ + File[] currFileList = file.listFiles(); + insertFilesIntoFileContentHandler(currFileList, fileContentHandler); + } + + } + } + + private void checkSTResults( + Map expectedOutserviceTemplates, + ServiceTemplate gloablSubstitutionServiceTemplate, ServiceTemplate mainServiceTemplate) { + YamlUtil yamlUtil = new YamlUtil(); + if (Objects.nonNull(gloablSubstitutionServiceTemplate)) { + assertEquals("difference global substitution service template: ", + yamlUtil.objectToYaml(expectedOutserviceTemplates.get(globalStName)), + yamlUtil.objectToYaml(gloablSubstitutionServiceTemplate)); + } + if (Objects.nonNull(mainServiceTemplate)) { + assertEquals("difference main service template: ", + yamlUtil.objectToYaml(expectedOutserviceTemplates.get(mainStName)), + yamlUtil.objectToYaml(mainServiceTemplate)); + } + } + + public static void loadServiceTemplates(String serviceTemplatesPath, + ToscaExtensionYamlUtil toscaExtensionYamlUtil, + Map serviceTemplates) + throws IOException { + URL urlFile = ToscaConverterImplTest.class.getResource(serviceTemplatesPath); + if (urlFile != null) { + File pathFile = new File(urlFile.getFile()); + File[] files = pathFile.listFiles(); + if (files != null) { + addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil); + } else { + throw new NotDirectoryException(serviceTemplatesPath); + } + } else { + throw new NotDirectoryException(serviceTemplatesPath); + } + } + + private static void addServiceTemplateFiles(Map serviceTemplates, + File[] files, + ToscaExtensionYamlUtil toscaExtensionYamlUtil) + throws IOException { + for (File file : files) { + try (InputStream yamlFile = new FileInputStream(file)) { + ServiceTemplate serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + createConcreteRequirementObjectsInServiceTemplate(serviceTemplateFromYaml, toscaExtensionYamlUtil); + serviceTemplates.put(file.getName(), serviceTemplateFromYaml); + try { + yamlFile.close(); + } catch (IOException ignore) { + } + } catch (FileNotFoundException e) { + throw e; + } catch (IOException e) { + throw e; + } + } + } + + private static void createConcreteRequirementObjectsInServiceTemplate(ServiceTemplate + serviceTemplateFromYaml, + ToscaExtensionYamlUtil + toscaExtensionYamlUtil) { + + if (serviceTemplateFromYaml == null + || serviceTemplateFromYaml.getTopology_template() == null + || serviceTemplateFromYaml.getTopology_template().getNode_templates() == null) { + return; + } + + //Creating concrete objects + Map nodeTemplates = + serviceTemplateFromYaml.getTopology_template().getNode_templates(); + for (Map.Entry entry : nodeTemplates.entrySet()) { + NodeTemplate nodeTemplate = entry.getValue(); + List> requirements = nodeTemplate.getRequirements(); + List> concreteRequirementList = new ArrayList<>(); + if (requirements != null) { + ListIterator> reqListIterator = requirements + .listIterator(); + while (reqListIterator.hasNext()){ + Map requirement = reqListIterator.next(); + Map concreteRequirement = new HashMap<>(); + for (Map.Entry reqEntry : requirement.entrySet()) { + RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil + .yamlToObject(toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()), + RequirementAssignment.class)); + concreteRequirement.put(reqEntry.getKey(), requirementAssignment); + concreteRequirementList.add(concreteRequirement); + reqListIterator.remove(); + } + } + requirements.clear(); + requirements.addAll(concreteRequirementList); + nodeTemplate.setRequirements(requirements); + } + System.out.println(); + //toscaExtensionYamlUtil.yamlToObject(nodeTemplate, NodeTemplate.class); + } + } +} -- cgit 1.2.3-korg