From 3dcd5675ad634f5bd3dd859a0362c259de271a06 Mon Sep 17 00:00:00 2001 From: Bartosz Gardziejewski Date: Tue, 21 Jul 2020 11:35:49 +0200 Subject: Add list of values validation using schema in PM Dictionary. Issue-ID: VNFSDK-594 Signed-off-by: Bartosz Gardziejewski Change-Id: I1e59bdfb697adb6097636212fa5be8c749f084e6 --- .../VTPValidateCSARR816745IntegrationTest.java | 22 ++++++-- .../validation/yaml/YamlFileValidatorTest.java | 47 +++++++++++++----- .../org/onap/validation/yaml/YamlLoaderTest.java | 15 +++++- .../org/onap/validation/yaml/YamlLoadingUtils.java | 8 ++- .../onap/validation/yaml/YamlValidatorTest.java | 58 ++++++++++++++++++---- .../yaml/model/YamlDocumentFactoryTest.java | 3 +- .../yaml/model/YamlParameterListFactoryTest.java | 9 ++-- 7 files changed, 124 insertions(+), 38 deletions(-) (limited to 'csarvalidation/src/test/java/org') 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 70a370e..5ef4e99 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 @@ -38,6 +38,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 VTPValidateCSARR816745 testCase; @@ -132,12 +133,13 @@ public class VTPValidateCSARR816745IntegrationTest { } private void assertThatReturnedErrorsAreCorrect(List errors) { - assertThat(errors.size()).isEqualTo(3); + assertThat(errors.size()).isEqualTo(NUMBER_OF_EXPECTED_ERRORS); Condition containingSameFileForAllErrors = new HamcrestCondition<>( containsString("Artifacts/Deployment/Measurements/PM_Dictionary.yml") ); - assertThat(convertToFilesList(errors)).haveExactly(3, containingSameFileForAllErrors); + assertThat(convertToFilesList(errors)) + .haveExactly(NUMBER_OF_EXPECTED_ERRORS, containingSameFileForAllErrors); Condition containingErrorForMissingValueInFirstDocument = new HamcrestCondition<>(allOf( containsString("Invalid YAML document in PM_Dictionary file."), @@ -151,9 +153,9 @@ public class VTPValidateCSARR816745IntegrationTest { containsString("Invalid YAML document in PM_Dictionary file."), containsString("In document number 1"), containsString("Path: /pmMetaData/pmFields/measResultType"), - containsString("Value is not in array of accepted values."), - containsString("value: integer"), - containsString("accepted values: [float, uint32, uint64]") + containsString("Value(s) is/are not in array of accepted values."), + containsString("value(s): integer"), + containsString("accepted value(s): [float, uint32, uint64]") )); assertThat(convertToMessagesList(errors)).haveExactly(1, containingErrorForWrongValueInFirstDocument); @@ -164,6 +166,16 @@ public class VTPValidateCSARR816745IntegrationTest { containsString("Key not found: measChangeType") )); assertThat(convertToMessagesList(errors)).haveExactly(1, containingErrorForMissingValueInSecondDocument); + + Condition containingErrorForWrongValueInArrayInThirdDocument = new HamcrestCondition<>(allOf( + containsString("Invalid YAML document in PM_Dictionary file."), + containsString("In document number 3"), + containsString("Path: /pmMetaData/pmFields/measAdditionalFields/vendorField1"), + containsString("Value(s) is/are not in array of accepted values."), + containsString("value(s): [Z, A]"), + containsString("accepted value(s): [X, Y, Z]") + )); + assertThat(convertToMessagesList(errors)).haveExactly(1, containingErrorForWrongValueInArrayInThirdDocument); } } diff --git a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlFileValidatorTest.java b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlFileValidatorTest.java index 6ef78ae..d29b41a 100644 --- a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlFileValidatorTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlFileValidatorTest.java @@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; public class YamlFileValidatorTest { @Test - public void shouldReturnNoErrorsWhenGivenPathToValidPmDictionaryFile() throws YamlProcessingException { + public void shouldReturnCorrectErrorsWhenGivenPathToValidPmDictionaryFile() throws YamlProcessingException { // given String path = getFullPathForGivenResources(YamlLoadingUtils.PATH_TO_VALID_YAML); @@ -40,25 +40,48 @@ public class YamlFileValidatorTest { new YamlFileValidator().validateYamlFileWithSchema(path); // then + assertValidationReturnedExpectedErrors(validationErrors); + + } + + @Test + public void shouldReturnCorrecErrorsWhenGivenPathToValidJsonStylePmDictionaryFile() throws YamlProcessingException { + // given + String path = getFullPathForGivenResources(YamlLoadingUtils.PATH_TO_VALID_JSON_STYLE_YAML); + + // when + List validationErrors = + new YamlFileValidator().validateYamlFileWithSchema(path); + + // then + assertValidationReturnedExpectedErrors(validationErrors); + } + + + private void assertValidationReturnedExpectedErrors(List validationErrors) { assertThat(validationErrors).isNotNull(); - assertThat(validationErrors).hasSize(3); + assertThat(validationErrors).hasSize(4); assertThat(validationErrors).usingRecursiveFieldByFieldElementComparator().containsAll( Lists.list( - new YamlDocumentValidationError(1 , - "/pmMetaData/pmFields/measResultType" , - "Value is not in array of accepted values.\n" + - " value: integer\n" + - " accepted values: [float, uint32, uint64]"), - new YamlDocumentValidationError(1 , + new YamlDocumentValidationError(1, + "/pmMetaData/pmFields/measResultType", + "Value(s) is/are not in array of accepted values.\n" + + " value(s): integer\n" + + " accepted value(s): [float, uint32, uint64]"), + new YamlDocumentValidationError(1, "/pmMetaData/pmFields/", - "Key not found: measChangeType"), - new YamlDocumentValidationError(2 , + "Key not found: measChangeType"), + new YamlDocumentValidationError(2, "/pmMetaData/pmFields/", - "Key not found: measChangeType") + "Key not found: measChangeType"), + new YamlDocumentValidationError(3, + "/pmMetaData/pmFields/measAdditionalFields/vendorField1", + "Value(s) is/are not in array of accepted values.\n" + + " value(s): [Z, A]\n" + + " accepted value(s): [X, Y, Z]") ) ); } - @Test public void shouldThrowErrorWhenGivenPathToInvalidPmDictionaryFile() { // given 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 e7efc2b..9e7af5d 100644 --- a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java @@ -31,13 +31,24 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; public class YamlLoaderTest { + private static final int EXPECTED_NUMBER_OF_DOCUMENTS = 5; + @Test public void shouldLoadAllDocumentsFromYamlFile() throws YamlDocumentFactory.YamlDocumentParsingException { // when List documents = YamlLoadingUtils.loadValidMultiDocumentYamlFile(); // then - assertThat(documents).hasSize(4); + assertThat(documents).hasSize(EXPECTED_NUMBER_OF_DOCUMENTS); + } + + @Test + public void shouldLoadAllDocumentsFromJsonStyleYamlFile() throws YamlDocumentFactory.YamlDocumentParsingException { + // when + List documents = YamlLoadingUtils.loadValidJsonStyleMultiDocumentYamlFile(); + + // then + assertThat(documents).hasSize(EXPECTED_NUMBER_OF_DOCUMENTS); } @Test @@ -46,7 +57,7 @@ public class YamlLoaderTest { List documents = YamlLoadingUtils.loadValidMultiDocumentYamlFileUsingStringPath(); // then - assertThat(documents).hasSize(4); + assertThat(documents).hasSize(EXPECTED_NUMBER_OF_DOCUMENTS); } @Test 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 5e09e0a..9df15cd 100644 --- a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java @@ -29,11 +29,13 @@ public final class YamlLoadingUtils { private YamlLoadingUtils() {} - public static final int VALID_YAML_DOCUMENT_INDEX = 3; + public static final int VALID_YAML_DOCUMENT_INDEX = 4; + public static final int YAML_DOCUMENT_WITH_WRONG_VALUE_IN_ARRAY_INDEX = 3; public static final int YAML_DOCUMENT_WITH_MISSING_FIELD_INDEX = 2; public static final int YAML_DOCUMENT_WITH_MISSING_FIELD_AND_WRONG_VALUE_INDEX = 1; static final String PATH_TO_VALID_YAML = "yaml_schema/PM_Dictionary.yaml"; + static final String PATH_TO_VALID_JSON_STYLE_YAML = "yaml_schema/PM_Dictionary_JSON_Style.yaml"; private static final String PATH_TO_SIMPLE_VALID_SCHEMA = "yaml_schema/Simple_Valid_Schema.yaml"; private static final String PATH_TO_SIMPLE_VALID_SCHEMA_MULTI_ROOT = "yaml_schema/Simple_Valid_Schema_Multi_Root.yaml"; private static final String PATH_TO_SIMPLE_INVALID_SCHEMA = "yaml_schema/Simple_Invalid_Schema_Construction.yaml"; @@ -44,6 +46,10 @@ public final class YamlLoadingUtils { return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_YAML)); } + public static List loadValidJsonStyleMultiDocumentYamlFile() throws YamlDocumentParsingException { + return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_JSON_STYLE_YAML)); + } + public static List loadValidMultiDocumentYamlFileUsingStringPath() throws YamlProcessingException { return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_YAML).getPath()); } diff --git a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlValidatorTest.java b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlValidatorTest.java index 4927d32..efe9d69 100644 --- a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlValidatorTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlValidatorTest.java @@ -32,9 +32,29 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.onap.validation.yaml.YamlLoadingUtils.VALID_YAML_DOCUMENT_INDEX; import static org.onap.validation.yaml.YamlLoadingUtils.YAML_DOCUMENT_WITH_MISSING_FIELD_INDEX; import static org.onap.validation.yaml.YamlLoadingUtils.YAML_DOCUMENT_WITH_MISSING_FIELD_AND_WRONG_VALUE_INDEX; +import static org.onap.validation.yaml.YamlLoadingUtils.YAML_DOCUMENT_WITH_WRONG_VALUE_IN_ARRAY_INDEX; public class YamlValidatorTest { + + @Test + public void shouldCreateValidatorUsingSchemaLoadedFromYamlFileAndValidatedJsonStyleDocumentsFromThatFile() + throws YamlProcessingException { + + // given + List documents = YamlLoadingUtils.loadValidJsonStyleMultiDocumentYamlFile(); + YamlValidator validator = new YamlValidator(new YamlSchemaFactory().createTreeStructuredYamlSchema(documents.get(0))); + Map> validationErrors = new HashMap<>(); + + // when + for (int documentIndex = 1 ; documentIndex < documents.size() ; documentIndex++) { + validationErrors.put(documentIndex, validator.validate(documents.get(documentIndex))); + } + + // then + assertValidatorReturnedCorrectErrors(validationErrors); + } + @Test public void shouldCreateValidatorUsingSchemaLoadedFromYamlFileAndValidatedDocumentsFromThatFile() throws YamlProcessingException { @@ -44,26 +64,38 @@ public class YamlValidatorTest { YamlValidator validator = new YamlValidator(new YamlSchemaFactory().createTreeStructuredYamlSchema(documents.get(0))); Map> validationErrors = new HashMap<>(); + // when + for (int documentIndex = 1 ; documentIndex < documents.size() ; documentIndex++) { + validationErrors.put(documentIndex, validator.validate(documents.get(documentIndex))); + } + + // then + assertValidatorReturnedCorrectErrors(validationErrors); + } + + private void assertValidatorReturnedCorrectErrors(Map> validationErrors) { + SchemaValidationError expectedValidationValueError = new SchemaValidationError( "/pmMetaData/pmFields/measResultType", - "Value is not in array of accepted values.\n" - + " value: integer\n" - + " accepted values: [float, uint32, uint64]" + "Value(s) is/are not in array of accepted values.\n" + + " value(s): integer\n" + + " accepted value(s): [float, uint32, uint64]" ); SchemaValidationError expectedValidationKeyError = new SchemaValidationError( "/pmMetaData/pmFields/", "Key not found: measChangeType" ); + SchemaValidationError expectedValidationValuesInArrayError = + new SchemaValidationError( + "/pmMetaData/pmFields/measAdditionalFields/vendorField1", + "Value(s) is/are not in array of accepted values.\n" + + " value(s): [Z, A]\n" + + " accepted value(s): [X, Y, Z]" + ); - // when - for (int documentIndex = 1 ; documentIndex < documents.size() ; documentIndex++) { - validationErrors.put(documentIndex, validator.validate(documents.get(documentIndex))); - } - - // then - assertThat(validationErrors.size()).isEqualTo(3); + assertThat(validationErrors.size()).isEqualTo(4); assertThat(validationErrors).containsKeys(1,2,3); assertThat(validationErrors.get(YAML_DOCUMENT_WITH_MISSING_FIELD_AND_WRONG_VALUE_INDEX)).hasSize(2); assertThat(validationErrors.get(YAML_DOCUMENT_WITH_MISSING_FIELD_AND_WRONG_VALUE_INDEX)) @@ -79,6 +111,12 @@ public class YamlValidatorTest { .contains( expectedValidationKeyError ); + assertThat(validationErrors.get(YAML_DOCUMENT_WITH_WRONG_VALUE_IN_ARRAY_INDEX)).hasSize(1); + assertThat(validationErrors.get(YAML_DOCUMENT_WITH_WRONG_VALUE_IN_ARRAY_INDEX)) + .usingFieldByFieldElementComparator() + .contains( + expectedValidationValuesInArrayError + ); assertThat(validationErrors.get(VALID_YAML_DOCUMENT_INDEX)).hasSize(0); } diff --git a/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java b/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java index 469b39f..5641a56 100644 --- a/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java @@ -29,7 +29,6 @@ import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.onap.validation.yaml.model.YamlDocumentFactory.YamlDocumentParsingException; -import static org.onap.validation.yaml.model.YamlParameterListFactory.YamlParameterListParsingException; public class YamlDocumentFactoryTest { @@ -91,7 +90,7 @@ public class YamlDocumentFactoryTest { @Test public void shouldTurnMapOfUnknownKeyTypeToMapWithStringKeysAndBeAbleToExtractParametersList() - throws YamlDocumentParsingException, YamlParameterListParsingException { + throws YamlDocumentParsingException { // given Map inputMap = new HashMap<>(); List parametersList = new LinkedList<>(); diff --git a/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlParameterListFactoryTest.java b/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlParameterListFactoryTest.java index 1f370cb..34e61c5 100644 --- a/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlParameterListFactoryTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/model/YamlParameterListFactoryTest.java @@ -37,8 +37,7 @@ public class YamlParameterListFactoryTest { } @Test - public void shouldCreateParametersListContainingStringsFromListContainingSimpleTypes() - throws YamlParameterListFactory.YamlParameterListParsingException { + public void shouldCreateParametersListContainingStringsFromListContainingSimpleTypes() { // given List testList = Lists.list("test1",3,23.45,'a',"test2"); @@ -52,8 +51,7 @@ public class YamlParameterListFactoryTest { } @Test - public void shouldCreateParametersListContainingStringsFromListContainingVariousTypes() - throws YamlParameterListFactory.YamlParameterListParsingException { + public void shouldCreateParametersListContainingStringsFromListContainingVariousTypes() { // given List testList = Lists.list("test1",3,Lists.list(2,3,4),"test2"); @@ -67,8 +65,7 @@ public class YamlParameterListFactoryTest { } @Test - public void shouldCreateListWithOneStringWhenGivenObjectIsNotList() - throws YamlParameterListFactory.YamlParameterListParsingException { + public void shouldCreateListWithOneStringWhenGivenObjectIsNotList() { // given Object testObject = "test"; -- cgit 1.2.3-korg