From 4428fc40a81ff3234b918af87da74a27ccbfe650 Mon Sep 17 00:00:00 2001 From: shiria Date: Tue, 26 Mar 2019 10:08:43 +0200 Subject: Load tosca with no imports section Change-Id: I50df2a149bd8d27973bb66e41f80a3260b8a9b8b Issue-ID: SDC-2192 Signed-off-by: shiria --- .../services/impl/ToscaAnalyzerServiceImpl.java | 5 ++-- .../impl/ToscaAnalyzerServiceImplTest.java | 28 ++++++++++++++++++++++ .../analyzerService/importConvertTestNoImport.yml | 4 ++++ .../mock/analyzerService/invalidToscaImport.yml | 5 ++++ 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/importConvertTestNoImport.yml create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/invalidToscaImport.yml (limited to 'openecomp-be/lib/openecomp-tosca-lib/src') diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java index bb557f6076..053253a993 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java @@ -182,7 +182,6 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { } String convertServiceTemplateImport(ToscaExtensionYamlUtil toscaExtensionYamlUtil, byte[] fileContent) { - Map serviceTemplateMap = toscaExtensionYamlUtil.yamlToObject(new String(fileContent), Map.class); convertToscaImports(serviceTemplateMap, toscaExtensionYamlUtil); return toscaExtensionYamlUtil.objectToYaml(serviceTemplateMap); @@ -191,8 +190,8 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { private void convertToscaImports(Map serviceTemplateMap, ToscaExtensionYamlUtil toscaExtensionYamlUtil) { List> convertedImport = new ArrayList<>(); Object importObj = serviceTemplateMap.get(IMPORTS); - if (!(importObj instanceof List)) { - throw new SdcRuntimeException("Illegal Statement"); + if (Objects.nonNull(importObj) && !(importObj instanceof List)) { + throw new SdcRuntimeException(new IllegalStateException("Invalid TOSCA import section")); } List imports = (List) importObj; if (CollectionUtils.isEmpty(imports)) { diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java index d2c1ed8a17..a915b9b131 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java @@ -1199,6 +1199,34 @@ public class ToscaAnalyzerServiceImplTest { Assert.assertNotNull(serviceTemplate.getImports().get(7).get("relationshipsExt")); } + @Test + public void testConvertToscaImportForEmptyImport() throws Exception { + String inputResourceName = "/mock/analyzerService/importConvertTestNoImport.yml"; + byte[] uploadedFileData = IOUtils.toByteArray(this.getClass().getResource(inputResourceName)); + + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + ToscaAnalyzerServiceImpl toscaAnalyzerServiceImpl = new ToscaAnalyzerServiceImpl(); + String convertServiceTemplateImport = + toscaAnalyzerServiceImpl.convertServiceTemplateImport(toscaExtensionYamlUtil, uploadedFileData); + + Assert.assertNotNull(convertServiceTemplateImport); + ServiceTemplate serviceTemplate = + new YamlUtil().yamlToObject(convertServiceTemplateImport, ServiceTemplate.class); + Assert.assertNull(serviceTemplate.getImports()); + } + + @Test + public void testInvalidToscaImportSection() throws Exception { + thrown.expect(SdcRuntimeException.class); + thrown.expectMessage("Invalid TOSCA import section"); + String inputResourceName = "/mock/analyzerService/invalidToscaImport.yml"; + byte[] uploadedFileData = IOUtils.toByteArray(this.getClass().getResource(inputResourceName)); + + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + ToscaAnalyzerServiceImpl toscaAnalyzerServiceImpl = new ToscaAnalyzerServiceImpl(); + toscaAnalyzerServiceImpl.convertServiceTemplateImport(toscaExtensionYamlUtil, uploadedFileData); + } + @Test public void loadValidToscaYamlFileTest() throws Exception { String inputResourceName = "/mock/analyzerService/ServiceTemplateInterfaceInheritanceTest.yaml"; diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/importConvertTestNoImport.yml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/importConvertTestNoImport.yml new file mode 100644 index 0000000000..536e1367e5 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/importConvertTestNoImport.yml @@ -0,0 +1,4 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 +node_types: + tosca.nodes.Root: + description: The TOSCA Node Type all other TOSCA base Node Types derive from \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/invalidToscaImport.yml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/invalidToscaImport.yml new file mode 100644 index 0000000000..8a5cfaa9ac --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/invalidToscaImport.yml @@ -0,0 +1,5 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 +imports: abc +node_types: + tosca.nodes.Root: + description: The TOSCA Node Type all other TOSCA base Node Types derive from \ No newline at end of file -- cgit 1.2.3-korg