diff options
author | 2019-09-03 13:45:28 +0000 | |
---|---|---|
committer | 2019-09-03 15:39:43 +0000 | |
commit | 7de4f3e788ff2b71936ae43be7dfbdf48740f9be (patch) | |
tree | a7419656377e146ec1095d6eaf82dbbffd717b41 /openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl | |
parent | ccc1f364baea63733e93c6f4eb26ea97eeb77960 (diff) |
Implement PNFD Model driven conversion
Change-Id: I7427e82353cac17817457294d78953f867d2d6e8
Issue-ID: SDC-2509
Signed-off-by: andre.schmid <andre.schmid@est.tech>
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl')
4 files changed, 373 insertions, 199 deletions
diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaConverterImplTest.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaConverterImplTest.java new file mode 100644 index 0000000000..3e4e62a0d1 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaConverterImplTest.java @@ -0,0 +1,296 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.impl; + +import org.apache.commons.collections.CollectionUtils; +import org.junit.Assert; +import org.junit.Test; +import org.onap.sdc.tosca.datatypes.model.*; +import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil; +import org.onap.sdc.tosca.services.YamlUtil; +import org.openecomp.core.converter.ToscaConverter; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.file.NotDirectoryException; +import java.util.*; + +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 final ToscaConverter toscaConverter = new ToscaConverterImpl(); + private static final String VIRTUAL_LINK = "virtualLink"; + private static final String UNBOUNDED = "UNBOUNDED"; + private static final String BASE_DIR = "/mock/toscaConverter"; + + + @Test + public void testConvertMainSt() throws IOException { + String inputFilesPath = BASE_DIR + "/convertMainSt/in"; + String outputFilesPath = BASE_DIR + "/convertMainSt/out"; + + convertAndValidate(inputFilesPath, outputFilesPath); + } + + @Test + public void testNodesConversion() throws IOException { + String inputFilesPath = BASE_DIR + "/convertCsar/in"; + String outputFilesPath = BASE_DIR + "/convertCsar/out"; + + convertAndValidate(inputFilesPath, outputFilesPath); + } + + @Test + public void testParameterConversion() throws IOException { + String inputFilesPath = BASE_DIR + "/convertParameters/in"; + String outputFilesPath = BASE_DIR + "/convertParameters/out"; + + convertAndValidate(inputFilesPath, outputFilesPath); + } + + @Test + public void testConversionWithInt() throws IOException { + String inputFilesPath = BASE_DIR + "/conversionWithInt/in"; + String outputFilesPath = BASE_DIR + "/conversionWithInt/out"; + + convertAndValidate(inputFilesPath, outputFilesPath); + } + + @Test + public void testOccurrencesUpperString() { + Object[] occurrences = buildOccurrences("0", UNBOUNDED); + Assert.assertEquals(occurrences[0], 0); + Assert.assertEquals(occurrences[1], UNBOUNDED); + } + + @Test + public void testOccurrencesAsInts() { + Object[] occurrences = buildOccurrences("0", "1"); + Assert.assertEquals(occurrences[0], 0); + Assert.assertEquals(occurrences[1], 1); + } + + @Test + public void testOccurrencesAsStrings() { + String test = "TEST_A"; + Object[] occurrences = buildOccurrences(UNBOUNDED, test); + Assert.assertEquals(occurrences[0], UNBOUNDED); + Assert.assertEquals(occurrences[1], test); + } + + @Test + public void testOccurrencesLowerString() { + Object[] occurrences = buildOccurrences(UNBOUNDED, "100"); + Assert.assertEquals(occurrences[0], UNBOUNDED); + Assert.assertEquals(occurrences[1], 100); + } + + @Test + public void testOccurrencesEmpty() { + Object[] occurrences = buildOccurrences(); + Assert.assertEquals(occurrences.length, 0); + } + + @Test + public void testOccurrencesMany() { + String test = "TEST_B"; + Object[] occurrences = buildOccurrences("1", "2", test); + Assert.assertEquals(occurrences[0], 1); + Assert.assertEquals(occurrences[1], 2); + Assert.assertEquals(occurrences[2], test); + } + + @Test + public void testDefaultOccurrences() { + Object[] occurrences = buildOccurrences((List<String>) null); + Assert.assertEquals(1, occurrences[0]); + Assert.assertEquals(1, occurrences[1]); + } + + private Object[] buildOccurrences(String... bounds) { + return buildOccurrences(Arrays.asList(bounds)); + } + + private void convertAndValidate(String inputFilesPath, String outputFilesPath) + throws IOException { + FileContentHandler fileContentHandler = + createFileContentHandlerFromInput(inputFilesPath); + + ToscaServiceModel toscaServiceModel = toscaConverter.convert(fileContentHandler); + validateConvertorOutput(outputFilesPath, toscaServiceModel); + } + + private void validateConvertorOutput(String outputFilesPath, ToscaServiceModel toscaServiceModel) + throws IOException { + ServiceTemplate mainSt = toscaServiceModel.getServiceTemplates().get(mainStName); + Map<String, ServiceTemplate> expectedOutserviceTemplates = new HashMap<>(); + loadServiceTemplates(outputFilesPath, new ToscaExtensionYamlUtil(), + expectedOutserviceTemplates); + + checkSTResults(expectedOutserviceTemplates, null, mainSt); + } + + private Object[] buildOccurrences(List<String> bounds) { + NodeType nodeType = JsonUtil.json2Object("{derived_from=tosca.nodes.Root, description=MME_VFC, " + + "properties={vendor={type=string, default=ERICSSON}, " + + "csarVersion={type=string, default=v1.0}, csarProvider={type=string, default=ERICSSON}, " + + "id={type=string, default=vMME}, version={type=string, default=v1.0}, csarType={type=string, default=NFAR}}, " + + "requirements=[{virtualLink={" + + (bounds == null ? "" : "occurrences=[" + String.join(", ", bounds) + "], ") + + "capability=tosca.capabilities.network.Linkable}}]}", NodeType.class); + List<Map<String, RequirementDefinition>> requirements = nodeType.getRequirements(); + return requirements.get(0).get(VIRTUAL_LINK).getOccurrences(); + } + + 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<String, ServiceTemplate> 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<String, ServiceTemplate> 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<String, ServiceTemplate> 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); + } + } + } + + 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<String, NodeTemplate> nodeTemplates = + serviceTemplateFromYaml.getTopology_template().getNode_templates(); + for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) { + NodeTemplate nodeTemplate = entry.getValue(); + List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements(); + List<Map<String, RequirementAssignment>> concreteRequirementList = new ArrayList<>(); + if (requirements != null) { + ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements + .listIterator(); + while (reqListIterator.hasNext()){ + Map<String, RequirementAssignment> requirement = reqListIterator.next(); + Map<String, RequirementAssignment> concreteRequirement = new HashMap<>(); + for (Map.Entry<String, RequirementAssignment> 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); + } + } +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaConvertorDefinitionsTest.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaConvertorDefinitionsTest.java new file mode 100644 index 0000000000..b978b7db99 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaConvertorDefinitionsTest.java @@ -0,0 +1,77 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.impl; + +import org.apache.commons.io.IOUtils; +import org.junit.Ignore; +import org.junit.Test; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; + +import java.net.URL; +import java.util.Map; +import java.util.Set; + +import static org.junit.Assert.*; +import static org.openecomp.core.converter.datatypes.Constants.ONAP_INDEX; +import static org.openecomp.core.impl.GlobalSubstitutionServiceTemplate.*; +import static org.openecomp.sdc.common.utils.CommonUtil.validateAndUploadFileContent; +public class ToscaConvertorDefinitionsTest { + + + @Ignore + @Test + public void loadCsar() throws Exception { + URL resource = ToscaConvertorDefinitionsTest.class.getResource("/csar/vCSCF.csar"); + byte[] bytes = IOUtils.toByteArray(resource); + assertNotNull(bytes); + FileContentHandler contentMap = validateAndUploadFileContent(OnboardingTypesEnum.CSAR, bytes); + ToscaConverterImpl toscaConverter = new ToscaConverterImpl(); + ToscaServiceModel convert = toscaConverter.convert(contentMap); + Map<String, ServiceTemplate> serviceTemplates = convert.getServiceTemplates(); + assertTrue(serviceTemplates.containsKey(GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME)); + ServiceTemplate serviceTemplate = serviceTemplates.get(GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME); + + assertNotNull(serviceTemplate); + assertTrue(serviceTemplate instanceof GlobalSubstitutionServiceTemplate); + + assertNotNull(serviceTemplate.getMetadata()); + assertFalse(serviceTemplate.getMetadata().isEmpty()); + assertTrue(serviceTemplate.getMetadata().containsKey(TEMPLATE_NAME_PROPERTY)); + + assertNotNull(serviceTemplate.getImports()); + assertFalse(serviceTemplate.getImports().isEmpty()); + assertEquals(1 ,serviceTemplate.getImports().size()); + assertTrue(((Map) serviceTemplate.getImports().get(0)).containsKey(HEAT_INDEX)); + assertTrue(((Map) serviceTemplate.getImports().get(0)).containsKey(ONAP_INDEX)); + + assertEquals(DEFININTION_VERSION, serviceTemplate.getTosca_definitions_version()); + + + assertNotNull(serviceTemplate.getNode_types()); + assertEquals(1, serviceTemplate.getNode_types().size()); + Set<String> keys = serviceTemplate.getNode_types().keySet(); + assertTrue(keys.contains("tosca.nodes.nfv.ext.zte.VNF.vCSCF")); + } + + +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterPnfMultipleNodeTemplatesTest.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterPnfMultipleNodeTemplatesTest.java deleted file mode 100644 index 21394c9c47..0000000000 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterPnfMultipleNodeTemplatesTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * - - * * ============LICENSE_START======================================================= - * * Copyright (C) 2019 Nordix Foundation. - * * ================================================================================ - * * Licensed under the Apache License, Version 2.0 (the "License"); - * * you may not use this file except in compliance with the License. - * * You may obtain a copy of the License at - * * - * * http://www.apache.org/licenses/LICENSE-2.0 - * * - * * Unless required by applicable law or agreed to in writing, software - * * distributed under the License is distributed on an "AS IS" BASIS, - * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * * See the License for the specific language governing permissions and - * * limitations under the License. - * * - * * SPDX-License-Identifier: Apache-2.0 - * * ============LICENSE_END========================================================= - * - */ - -package org.openecomp.core.impl; - -import org.apache.commons.io.IOUtils; -import org.junit.Test; -import org.onap.sdc.tosca.datatypes.model.NodeTemplate; -import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; -import org.openecomp.core.converter.ServiceTemplateReaderService; -import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class ToscaSolConverterPnfMultipleNodeTemplatesTest { - - private static final String PNF_EXT_CP_1 = "pnfExtCp_1"; - private static final String PNF_EXT_CP_2 = "pnfExtCp_2"; - - @Test - public void testGivenDescriptorWithPnfAndTwoPnfExts_WhenConvertTopologyTemplate_ThenTwoExtCpsInOutput() throws IOException { - // Added this as separate test as data-driven tests compare strings and as order of nodeTemplates - // can be different in hashMap and hence test may fail - final byte[] descriptor = getFileResource("pnfDescriptor/other/pnfDescriptor_PnfAnd2ExtCps.yaml"); - ServiceTemplateReaderService serviceTemplateReaderService = new ServiceTemplateReaderServiceImpl(descriptor); - ServiceTemplate serviceTemplate = new ServiceTemplate(); - ToscaSolConverterPnf toscaSolConverter = new ToscaSolConverterPnf(); - toscaSolConverter.convertTopologyTemplate(serviceTemplate, serviceTemplateReaderService); - Map<String, NodeTemplate> nodeTemplates = serviceTemplate.getTopology_template().getNode_templates(); - assertEquals(2, nodeTemplates.size()); - - nodeTemplates.entrySet().stream() - .map(Map.Entry::getKey) - .forEach((key -> assertTrue(getErrorString(), hasCorrectName(key)))); - } - - private byte[] getFileResource(String filePath) throws IOException { - try (InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath)) { - return IOUtils.toByteArray(inputStream); - } - } - - private boolean hasCorrectName(String name) { - return PNF_EXT_CP_1.equals(name) || PNF_EXT_CP_2.equals(name); - } - - private String getErrorString() { - return "node template name should be either " + PNF_EXT_CP_1 + " or " + PNF_EXT_CP_2; - } -} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterPnfTest.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterPnfTest.java deleted file mode 100644 index 4d59d6bfe9..0000000000 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterPnfTest.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * - - * * ============LICENSE_START======================================================= - * * Copyright (C) 2019 Nordix Foundation. - * * ================================================================================ - * * Licensed under the Apache License, Version 2.0 (the "License"); - * * you may not use this file except in compliance with the License. - * * You may obtain a copy of the License at - * * - * * http://www.apache.org/licenses/LICENSE-2.0 - * * - * * Unless required by applicable law or agreed to in writing, software - * * distributed under the License is distributed on an "AS IS" BASIS, - * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * * See the License for the specific language governing permissions and - * * limitations under the License. - * * - * * SPDX-License-Identifier: Apache-2.0 - * * ============LICENSE_END========================================================= - * - */ - -package org.openecomp.core.impl; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.fail; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Collection; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import org.apache.commons.io.IOUtils; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; -import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil; -import org.onap.sdc.tosca.services.YamlUtil; -import org.openecomp.core.converter.ServiceTemplateReaderService; -import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl; - -@RunWith(Parameterized.class) -public class ToscaSolConverterPnfTest { - - private static final String INPUT_DIR = "pnfDescriptor/in/"; - private static final String OUTPUT_DIR = "pnfDescriptor/out/"; - private String inputFilename; - private YamlUtil yamlUtil = new YamlUtil(); - private ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - - public ToscaSolConverterPnfTest(String inputFilename) { - this.inputFilename = inputFilename; - } - - @Parameterized.Parameters(name = "{index}: {0}") - public static Collection<String> input() throws IOException { - try (Stream<Path> files = Files.list(getPathFromClasspath(INPUT_DIR))) { - return files.map(path -> path.getFileName().toString()) - .collect(Collectors.toList()); - } - } - - @Test - public void testTopologyTemplateConversions() { - final byte[] descriptor = getInputFileResource(inputFilename); - final ServiceTemplateReaderService serviceTemplateReaderService = - new ServiceTemplateReaderServiceImpl(descriptor); - final ServiceTemplate serviceTemplate = new ServiceTemplate(); - final ToscaSolConverterPnf toscaSolConverter = new ToscaSolConverterPnf(); - toscaSolConverter.convertTopologyTemplate(serviceTemplate, serviceTemplateReaderService); - - final String actualYaml = yamlUtil.objectToYaml(serviceTemplate); - final String expectedYaml = getExpectedResultFor(inputFilename); - assertThat("Converted PNF descriptor should be the same as the expected topology template", actualYaml, - equalTo(expectedYaml)); - } - - private String getExpectedResultFor(final String inputFilename) { - try (final InputStream inputStream = getOutputFileResourceCorrespondingTo(inputFilename)) { - final ServiceTemplate serviceTemplate = toscaExtensionYamlUtil.yamlToObject(inputStream, ServiceTemplate.class); - return yamlUtil.objectToYaml(serviceTemplate); - } catch (final IOException e) { - fail(String.format("Could not find file '%s'", inputFilename)); - } - - return null; - } - - private static Path getPathFromClasspath(final String location) { - return Paths.get(Thread.currentThread().getContextClassLoader().getResource(location).getPath()); - } - - private byte[] getInputFileResource(final String inputFilename) { - return getFileResource(INPUT_DIR + inputFilename); - } - - private InputStream getOutputFileResourceCorrespondingTo(final String inputFilename) { - final String outputFilename = getOutputFilenameFrom(inputFilename); - return getFileResourceAsInputStream(OUTPUT_DIR + outputFilename); - } - - private String getOutputFilenameFrom(final String inputFilename) { - return inputFilename.replace("pnfDescriptor", "topologyTemplate"); - } - - private byte[] getFileResource(final String filePath) { - try (InputStream inputStream = getFileResourceAsInputStream(filePath)) { - return IOUtils.toByteArray(inputStream); - } catch (final IOException e) { - fail(String.format("Could not find file '%s'", filePath)); - } - - return null; - } - - private InputStream getFileResourceAsInputStream(final String filePath) { - return Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); - } - -}
\ No newline at end of file |