From 7be0ab69d0828950c1601566ae9857a104e7124d Mon Sep 17 00:00:00 2001 From: Bartosz Gardziejewski Date: Tue, 18 Aug 2020 13:31:29 +0200 Subject: Wrap exceptions connected with Yaml parsing in order to send them as validation error. Signed-off-by: Bartosz Gardziejewski Change-Id: Iae6c0948746bb903945de1796b6f42952d4072ba Issue-ID: VNFSDK-644 --- Changelog.md | 4 + .../cvc/csar/cc/sol004/VTPValidateCSARR816745.java | 3 +- .../VTPValidateCSARR816745IntegrationTest.java | 34 +++++ .../org/onap/validation/yaml/YamlLoaderTest.java | 20 +++ .../org/onap/validation/yaml/YamlLoadingUtils.java | 10 ++ ...with-invalid-pm-dictionary-invalid-mapping.csar | Bin 0 -> 8590 bytes ...lid-pm-dictionary-unknown-escape-character.csar | Bin 0 -> 8595 bytes .../yaml_schema/Simple_Invalid_Mapping_Value.yaml | 149 +++++++++++++++++++++ .../Simple_Unknown_Escape_Character.yaml | 149 +++++++++++++++++++++ 9 files changed, 368 insertions(+), 1 deletion(-) create mode 100644 csarvalidation/src/test/resources/pnf/r816745/csar-with-invalid-pm-dictionary-invalid-mapping.csar create mode 100644 csarvalidation/src/test/resources/pnf/r816745/csar-with-invalid-pm-dictionary-unknown-escape-character.csar create mode 100644 csarvalidation/src/test/resources/yaml_schema/Simple_Invalid_Mapping_Value.yaml create mode 100644 csarvalidation/src/test/resources/yaml_schema/Simple_Unknown_Escape_Character.yaml diff --git a/Changelog.md b/Changelog.md index a179ffc..a60747e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -61,3 +61,7 @@ All notable changes to this project will be documented in this file. ## [1.2.11] + +## Fixed +- Fixed rule R816745 that wasn't sending all exceptions connected with YAML parsing as validation error + - https://jira.onap.org/browse/VNFSDK-644 diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java index f1bbffc..a929418 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java @@ -24,6 +24,7 @@ import org.onap.validation.yaml.error.YamlDocumentValidationError; import org.onap.validation.yaml.exception.YamlProcessingException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.error.YAMLException; import java.nio.file.Path; import java.util.List; @@ -83,7 +84,7 @@ public class VTPValidateCSARR816745 extends VTPValidateCSARBase { List validationErrors = new YamlFileValidator().validateYamlFileWithSchema(rootPath+artifactPath); addAllErrorsReportedByVaidator(artifactPath, validationErrors); - } catch (YamlProcessingException e) { + } catch (YamlProcessingException | YAMLException e) { LOGGER.error("Failed to load PM_Dictionary file.", e); errors.add(new CSARPmDictionaryLoadingError( artifactPath, diff --git a/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745IntegrationTest.java b/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745IntegrationTest.java index ac8f964..f171b4d 100644 --- a/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745IntegrationTest.java +++ b/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745IntegrationTest.java @@ -39,6 +39,7 @@ public class VTPValidateCSARR816745IntegrationTest { private static final boolean IS_PNF = true; private static final String TEST_CSAR_DIRECTORY = "pnf/r816745/"; private static final int NUMBER_OF_EXPECTED_ERRORS = 4; + private static final String LETTER_S_WITH_ASCII_CODE = "s(115)"; private VTPValidateCSARR816745 testCase; @@ -127,6 +128,39 @@ public class VTPValidateCSARR816745IntegrationTest { assertThat(convertToMessagesList(errors)).contains("Fail to load PM_Dictionary With error: PM_Dictionary YAML file is empty"); } + @Test + public void shouldReturnListContainingOneErrorsWhenCsarContainsPmDictionaryWithInvalidKeyMapping() throws Exception { + // given + configureTestCase(testCase, TEST_CSAR_DIRECTORY + "csar-with-invalid-pm-dictionary-invalid-mapping.csar", "vtp-validate-csar-r816745.yaml", IS_PNF); + + // when + testCase.execute(); + + // then + List errors = testCase.getErrors(); + assertThat(errors.size()).isEqualTo(1); + assertThat(convertToMessagesList(errors).get(0)).contains( + "Fail to load PM_Dictionary With error: mapping values are not allowed here" + ); + } + + @Test + public void shouldReturnListContainingOneErrorsWhenCsarContainsPmDictionaryWithIncorrectEscapeCharacter() throws Exception { + // given + configureTestCase(testCase, TEST_CSAR_DIRECTORY + "csar-with-invalid-pm-dictionary-unknown-escape-character.csar", "vtp-validate-csar-r816745.yaml", IS_PNF); + + // when + testCase.execute(); + + // then + List errors = testCase.getErrors(); + assertThat(errors.size()).isEqualTo(1); + assertThat(convertToMessagesList(errors).get(0)).contains( + "Fail to load PM_Dictionary With error: while scanning a double-quoted scalar", + "found unknown escape character " + LETTER_S_WITH_ASCII_CODE + ); + } + @Test public void shouldReturnProperRequestNumber() { assertThat(testCase.getVnfReqsNo()).isEqualTo("R816745"); diff --git a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java index e8bc7c1..4c68d60 100644 --- a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java @@ -22,6 +22,7 @@ import org.onap.validation.yaml.exception.YamlProcessingException; import org.onap.validation.yaml.model.YamlDocument; import org.onap.validation.yaml.model.YamlDocumentFactory; import org.yaml.snakeyaml.parser.ParserException; +import org.yaml.snakeyaml.scanner.ScannerException; import java.util.List; @@ -32,6 +33,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; public class YamlLoaderTest { private static final int EXPECTED_NUMBER_OF_DOCUMENTS = 5; + private static final String LETTER_S_WITH_ASCII_CODE = "s(115)"; @Test public void shouldLoadAllDocumentsFromYamlFile() throws YamlDocumentFactory.YamlDocumentParsingException { @@ -76,4 +78,22 @@ public class YamlLoaderTest { .hasMessageContaining("expected the node content, but found ''"); } + + @Test + public void shouldThrowExceptionWhenLoadingInvalidYamlFileWithIncorrectKeyMapping() { + // when then + assertThatThrownBy(YamlLoadingUtils::tryToLoadInvalidYamlFileWithIncorrectKeyMapping + ).isInstanceOf(ScannerException.class) + .hasMessageContaining("mapping values are not allowed here"); + } + + + @Test + public void shouldThrowExceptionWhenLoadingInvalidYamlFileWithUnknownEscapeCharacter() { + // when then + assertThatThrownBy(YamlLoadingUtils::tryToLoadInvalidYamlFileWithUnknownEscapeCharacter + ).isInstanceOf(ScannerException.class) + .hasMessageContaining("found unknown escape character " + LETTER_S_WITH_ASCII_CODE); + } + } diff --git a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java index 9df15cd..8d03910 100644 --- a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java @@ -41,6 +41,8 @@ public final class YamlLoadingUtils { private static final String PATH_TO_SIMPLE_INVALID_SCHEMA = "yaml_schema/Simple_Invalid_Schema_Construction.yaml"; private static final String PATH_TO_SIMPLE_INVALID_SCHEMA_FOR_LAZY_LOADING = "yaml_schema/Simple_Invalid_Schema_LazyLoading.yaml"; static final String PATH_TO_MULTI_DOCUMENT_INVALID_YAML = "yaml_schema/Multi_Document_Invalid.yaml"; + private static final String PATH_TO_INVALID_YAML_WITH_INCORRECT_KEY_MAPPING = "yaml_schema/Simple_Invalid_Mapping_Value.yaml"; + private static final String PATH_TO_INVALID_YAML_WITH_UNKNOWN_ESCAPE_CHARACTER = "yaml_schema/Simple_Unknown_Escape_Character.yaml"; public static List loadValidMultiDocumentYamlFile() throws YamlDocumentParsingException { return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_YAML)); @@ -78,6 +80,14 @@ public final class YamlLoadingUtils { return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_MULTI_DOCUMENT_INVALID_YAML).getPath()); } + public static List tryToLoadInvalidYamlFileWithIncorrectKeyMapping() throws YamlDocumentParsingException { + return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_INVALID_YAML_WITH_INCORRECT_KEY_MAPPING)); + } + + public static List tryToLoadInvalidYamlFileWithUnknownEscapeCharacter() throws YamlDocumentParsingException { + return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_INVALID_YAML_WITH_UNKNOWN_ESCAPE_CHARACTER)); + } + private static URL getUrlForGivenPath(String path) { return YamlLoadingUtils.class.getClassLoader().getResource(path); } diff --git a/csarvalidation/src/test/resources/pnf/r816745/csar-with-invalid-pm-dictionary-invalid-mapping.csar b/csarvalidation/src/test/resources/pnf/r816745/csar-with-invalid-pm-dictionary-invalid-mapping.csar new file mode 100644 index 0000000..b69b79a Binary files /dev/null and b/csarvalidation/src/test/resources/pnf/r816745/csar-with-invalid-pm-dictionary-invalid-mapping.csar differ diff --git a/csarvalidation/src/test/resources/pnf/r816745/csar-with-invalid-pm-dictionary-unknown-escape-character.csar b/csarvalidation/src/test/resources/pnf/r816745/csar-with-invalid-pm-dictionary-unknown-escape-character.csar new file mode 100644 index 0000000..9ed6270 Binary files /dev/null and b/csarvalidation/src/test/resources/pnf/r816745/csar-with-invalid-pm-dictionary-unknown-escape-character.csar differ diff --git a/csarvalidation/src/test/resources/yaml_schema/Simple_Invalid_Mapping_Value.yaml b/csarvalidation/src/test/resources/yaml_schema/Simple_Invalid_Mapping_Value.yaml new file mode 100644 index 0000000..25c72cd --- /dev/null +++ b/csarvalidation/src/test/resources/yaml_schema/Simple_Invalid_Mapping_Value.yaml @@ -0,0 +1,149 @@ +--- +# PM Dictionary schema specifying and describing the meta information +# used to define perf3gpp measurements in the PM Dictionary +pmMetaData: { presence: required, structure: { + pmHeader: { + presence: required, + structure: { + nfType: { + presence: required, + comment: "NF type; should match the nfName-vendor string used in + the fileReady or perf3gpp eventName" + }, + pmDefSchemaVsn: { + presence: required, + value: 2.0, + comment: "PM Dictionary Schema Version from the VES Event + Registration specification" + }, + pmDefVsn: { + presence: required, + comment: "vendor-defined PM Dictionary version" + } + } + }, + pmFields: { + presence: required, + structure: { + iMeasInfoId: { + presence: required, + comment: "vendor-defined integer measurement group identifier" + }, + iMeasType: { + presence: required, + comment: "vendor-defined integer identifier for the measType; + must be combined with measInfoId to identify a + specific measurement." + }, + measChangeType: { + presence: required, + value: [added, modified, deleted], + comment: "indicates the type of change that occurred during + measLastChange" + }, + measCollectionMethod: { + presence: required, + value: [CC, SI, DER, Gauge, Average], + comment: "the measurement collection method; CC, SI, DER and + Gauge are as defined in 3GPP; average contains the + average value of the measurement during the + granularity period" + }, + measCondition: { + presence: required, + comment: "description of the condition causing the measurement" + }, + measDescription: { + presence: required, + comment: "description of the measurement information + and purpose" + }, + measFamily: { + presence: required, + comment: "abbreviation for a family of measurements, in + 3GPP format, or vendor defined" + }, + measInfoId: { + presence: required, + comment: "name for a group of related measurements in + 3GPP format or vendor defined" + }, + measLastChange: { + presence: required, + comment: "version of the PM Dictionary the last time this + measurement was added, modified or deleted" + }, + measObjClass: { + presence: required, + value: [NGBTS, NGCELL, IPNO, IPSEC, ETHIF], + comment: "measurement object class" + }, + measResultRange: { + presence: optional, + comment: "range of the measurement result; only necessary when + the range is smaller than the full range of the + data type" + }, + measResultType: { + presence: required, + value: [float, uint32, uint64], + comment: "data type of the measurement result" + }, + measResultUnits: { + presence: required, + value: [seconds, minutes, nanoseconds, microseconds, dB, + number, kilobytes, bytes, ethernetFrames, + packets, users], + comment: "units of measure for the measurement result" + }, + measType: { + presence: required, + comment: "measurement name in 3GPP or vendor-specific format; + vendor specific names are preceded with VS" + }, + measAdditionalFields: { + presence: required, + comment: "vendor-specific PM Dictionary fields", + structure: { + vendorField1: { + presence: required, + value: [X, Y, Z], + comment: "vendor field 1 description" + }, + vendorField2: { + presence: optional, + value: [A, B], + comment: "vendor field 2 description." + } + } + } + } + } +}} +... +# PM Dictionary perf3gpp measurements for the gnb-Nokia NF (bracket style yaml) +--- +pmMetaData: + pmHeader: + nfType: gnb-Nokia + pmDefSchemaVsn: 2.0 + pmDefVsn: 5G19_1906_002 + pmFields: + iMeasInfoId: 2204 + iMeasType: 1 + measCollectionMethod: CC + measCondition: This measurement is updated when X2AP: SgNB Modification Required message is sent to MeNB + with the SCG Change Indication set as PSCellChange. + measDescription: This counter indicates the number of intra gNB intra frequency PSCell change attempts. + measFamily: NINFC + measInfoId: "NR Intra Frequency PSCell Change" + measLastChange: 5G18A_1807_003 + measObjClass: NGCELL + measResultRange: 0-4096 + measResultType: integer + measResultUnits: number + measType: VS.NINFC.IntraFrPscelChAttempt + measAdditionalFields: + vendorField1: X + vendorField2: B +... diff --git a/csarvalidation/src/test/resources/yaml_schema/Simple_Unknown_Escape_Character.yaml b/csarvalidation/src/test/resources/yaml_schema/Simple_Unknown_Escape_Character.yaml new file mode 100644 index 0000000..2cac9e6 --- /dev/null +++ b/csarvalidation/src/test/resources/yaml_schema/Simple_Unknown_Escape_Character.yaml @@ -0,0 +1,149 @@ +--- +# PM Dictionary schema specifying and describing the meta information +# used to define perf3gpp measurements in the PM Dictionary +pmMetaData: { presence: required, structure: { + pmHeader: { + presence: required, + structure: { + nfType: { + presence: required, + comment: "NF type; should match the nfName-vendor string used in + the fileReady or perf3gpp eventName" + }, + pmDefSchemaVsn: { + presence: required, + value: 2.0, + comment: "PM Dictionary Schema Version from the VES Event + Registration specification" + }, + pmDefVsn: { + presence: required, + comment: "vendor-defined PM Dictionary version" + } + } + }, + pmFields: { + presence: required, + structure: { + iMeasInfoId: { + presence: required, + comment: "vendor-defined integer measurement group identifier" + }, + iMeasType: { + presence: required, + comment: "vendor-defined integer identifier for the measType; + must be combined with measInfoId to identify a + specific measurement." + }, + measChangeType: { + presence: required, + value: [added, modified, deleted], + comment: "indicates the type of change that occurred during + measLastChange" + }, + measCollectionMethod: { + presence: required, + value: [CC, SI, DER, Gauge, Average], + comment: "the measurement collection method; CC, SI, DER and + Gauge are as defined in 3GPP; average contains the + average value of the measurement during the + granularity period" + }, + measCondition: { + presence: required, + comment: "description of the condition causing the measurement" + }, + measDescription: { + presence: required, + comment: "description of the measurement information + and purpose" + }, + measFamily: { + presence: required, + comment: "abbreviation for a family of measurements, in + 3GPP format, or vendor defined" + }, + measInfoId: { + presence: required, + comment: "name for a group of related measurements in + 3GPP format or vendor defined" + }, + measLastChange: { + presence: required, + comment: "version of the PM Dictionary the last time this + measurement was added, modified or deleted" + }, + measObjClass: { + presence: required, + value: [NGBTS, NGCELL, IPNO, IPSEC, ETHIF], + comment: "measurement object class" + }, + measResultRange: { + presence: optional, + comment: "range of the measurement result; only necessary when + the range is smaller than the full range of the + data type" + }, + measResultType: { + presence: required, + value: [float, uint32, uint64], + comment: "data type of the measurement result" + }, + measResultUnits: { + presence: required, + value: [seconds, minutes, nanoseconds, microseconds, dB, + number, kilobytes, bytes, ethernetFrames, + packets, users], + comment: "units of measure for the measurement result" + }, + measType: { + presence: required, + comment: "measurement name in 3GPP or vendor-specific format; + vendor specific names are preceded with VS" + }, + measAdditionalFields: { + presence: required, + comment: "vendor-specific PM Dictionary fields", + structure: { + vendorField1: { + presence: required, + value: [X, Y, Z], + comment: "vendor field 1 description" + }, + vendorField2: { + presence: optional, + value: [A, B], + comment: "vendor field 2 description." + } + } + } + } + } +}} +... +# PM Dictionary perf3gpp measurements for the gnb-Nokia NF (bracket style yaml) +--- +pmMetaData: + pmHeader: + nfType: gnb-Nokia + pmDefSchemaVsn: 2.0 + pmDefVsn: 5G19_1906_002 + pmFields: + iMeasInfoId: 2204 + iMeasType: 1 + measCollectionMethod: CC + measCondition: "This measurement is updated when X2AP: SgNB Modification Required message is \sent\ to MeNB + with the SCG Change Indication set as PSCellChange." + measDescription: This counter indicates the number of intra gNB intra frequency PSCell change attempts. + measFamily: NINFC + measInfoId: "NR Intra Frequency PSCell Change" + measLastChange: 5G18A_1807_003 + measObjClass: NGCELL + measResultRange: 0-4096 + measResultType: integer + measResultUnits: number + measType: VS.NINFC.IntraFrPscelChAttempt + measAdditionalFields: + vendorField1: X + vendorField2: B +... -- cgit 1.2.3-korg