diff options
Diffstat (limited to 'pmdictionaryvalidation/src/test/java/org')
10 files changed, 377 insertions, 268 deletions
diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlContentValidatorTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlContentValidatorTest.java new file mode 100644 index 0000000..b6c2548 --- /dev/null +++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlContentValidatorTest.java @@ -0,0 +1,161 @@ +/* + * Copyright 2020 Nokia + * + * 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. + * + */ +package org.onap.validation.yaml; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.onap.validation.yaml.error.YamlDocumentValidationError; +import org.onap.validation.yaml.exception.YamlProcessingException; +import org.yaml.snakeyaml.parser.ParserException; + +import java.io.IOException; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.onap.validation.yaml.YamlLoadingUtils.PATH_TO_MULTI_DOCUMENT_INVALID_YAML; +import static org.onap.validation.yaml.YamlLoadingUtils.PATH_TO_VALID_JSON_STYLE_YAML; +import static org.onap.validation.yaml.YamlLoadingUtils.PATH_TO_VALID_YAML; +import static org.onap.validation.yaml.YamlLoadingUtils.readFile; + +class YamlContentValidatorTest { + @Nested + class FromStringPathValidator { + @Test + void shouldReturnCorrectErrorsWhenGivenPathToValidPmDictionaryFile() throws YamlProcessingException { + // given + String path = getFullPathForGivenResources(PATH_TO_VALID_YAML); + + // when + List<YamlDocumentValidationError> validationErrors = new YamlContentValidator().validate(path); + + // then + assertValidationReturnedExpectedErrors(validationErrors); + } + + @Test + void shouldReturnCorrectErrorsWhenGivenPathToValidJsonStylePmDictionaryFile() throws YamlProcessingException { + // given + String path = getFullPathForGivenResources(PATH_TO_VALID_JSON_STYLE_YAML); + + // when + List<YamlDocumentValidationError> validationErrors = new YamlContentValidator().validate(path); + + // then + assertValidationReturnedExpectedErrors(validationErrors); + } + + @Test + void shouldThrowErrorWhenGivenPathToInvalidPmDictionaryFile() { + // given + String path = getFullPathForGivenResources(PATH_TO_MULTI_DOCUMENT_INVALID_YAML); + + //when then + assertThatThrownBy(() -> new YamlContentValidator().validate(path)) + .isInstanceOf(ParserException.class) + .hasMessageContaining("expected the node content, but found '<document end>'"); + } + + @Test + void shouldThrowErrorWhenGivenInvalidPath() { + // given + String path = "invalid/path/to/pm_dictionary"; + + //when then + assertThatThrownBy(() -> new YamlContentValidator().validate(path)) + .isInstanceOf(YamlProcessingException.class) + .hasMessageContaining("PM_Dictionary YAML file is empty"); + } + } + + @Nested + class FromByteArrayValidator { + @Test + void shouldReturnCorrectErrorsWhenGivenPmDictionaryFileWithErrors() throws YamlProcessingException, IOException { + // given + byte[] yaml = readFile(PATH_TO_VALID_YAML); + + // when + List<YamlDocumentValidationError> validationErrors = new YamlContentValidator().validate(yaml); + + // then + assertValidationReturnedExpectedErrors(validationErrors); + } + + @Test + void shouldReturnCorrectErrorsWhenGivenValidJsonStylePmDictionary() throws YamlProcessingException, IOException { + // given + byte[] yaml = readFile(PATH_TO_VALID_JSON_STYLE_YAML); + + // when + List<YamlDocumentValidationError> validationErrors = new YamlContentValidator().validate(yaml); + + // then + assertValidationReturnedExpectedErrors(validationErrors); + } + + @Test + void shouldThrowErrorWhenGivenInvalidPmDictionary() throws IOException { + // given + byte[] yaml = readFile(PATH_TO_MULTI_DOCUMENT_INVALID_YAML); + + //when then + assertThatThrownBy(() -> new YamlContentValidator().validate(yaml)) + .isInstanceOf(ParserException.class) + .hasMessageContaining("expected the node content, but found '<document end>'"); + } + + @Test + void shouldThrowErrorWhenGivenEmptyPmDictionary() { + //when then + assertThatThrownBy(() -> new YamlContentValidator().validate(new byte[0])) + .isInstanceOf(YamlProcessingException.class) + .hasMessageContaining("PM_Dictionary YAML file is empty"); + } + } + + private void assertValidationReturnedExpectedErrors(List<YamlDocumentValidationError> validationErrors) { + assertThat(validationErrors) + .isNotNull() + .hasSize(4) + .usingRecursiveFieldByFieldElementComparator() + .containsAll( + List.of( + 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, + "/pmMetaData/pmFields/", + "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]") + ) + ); + } + + private String getFullPathForGivenResources(String pathToValidYaml) { + return this.getClass().getClassLoader().getResource(pathToValidYaml).getPath(); + } +}
\ No newline at end of file diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlFileValidatorTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlFileValidatorTest.java deleted file mode 100644 index 5eb5dd5..0000000 --- a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlFileValidatorTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2020 Nokia - * - * 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. - * - */ - -package org.onap.validation.yaml; - -import org.junit.jupiter.api.Test; -import org.onap.validation.yaml.error.YamlDocumentValidationError; -import org.onap.validation.yaml.exception.YamlProcessingException; -import org.yaml.snakeyaml.parser.ParserException; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.catchThrowable; - -class YamlFileValidatorTest { - - @Test - void shouldReturnCorrectErrorsWhenGivenPathToValidPmDictionaryFile() throws YamlProcessingException { - //given - String path = getFullPathForGivenResources(YamlLoadingUtils.PATH_TO_VALID_YAML); - - //when - List<YamlDocumentValidationError> validationErrors = new YamlFileValidator().validateYamlFileWithSchema(path); - - //then - assertValidationReturnedExpectedErrors(validationErrors); - } - - @Test - void shouldReturnCorrectErrorsWhenGivenPathToValidJsonStylePmDictionaryFile() throws YamlProcessingException { - //given - String path = getFullPathForGivenResources(YamlLoadingUtils.PATH_TO_VALID_JSON_STYLE_YAML); - - //when - List<YamlDocumentValidationError> validationErrors = new YamlFileValidator().validateYamlFileWithSchema(path); - - //then - assertValidationReturnedExpectedErrors(validationErrors); - } - - - private void assertValidationReturnedExpectedErrors(List<YamlDocumentValidationError> validationErrors) { - assertThat(validationErrors) - .isNotNull() - .hasSize(4) - .usingRecursiveFieldByFieldElementComparator() - .containsAll( - List.of( - 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, - "/pmMetaData/pmFields/", - "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 - void shouldThrowErrorWhenGivenPathToInvalidPmDictionaryFile() { - //given - String path = getFullPathForGivenResources(YamlLoadingUtils.PATH_TO_MULTI_DOCUMENT_INVALID_YAML); - - //when - Throwable ex = catchThrowable(() -> new YamlFileValidator().validateYamlFileWithSchema(path)); - - //then - assertThat(ex) - .isInstanceOf(ParserException.class) - .hasMessageContaining("expected the node content, but found '<document end>'"); - } - - @Test - void shouldThrowErrorWhenGivenInvalidPath() { - //given - String path = "invalid/path/to/pm_dictionary"; - - //when - Throwable ex = catchThrowable(() -> new YamlFileValidator().validateYamlFileWithSchema(path)); - - //then - assertThat(ex) - .isInstanceOf(YamlProcessingException.class) - .hasMessageContaining("PM_Dictionary YAML file is empty"); - } - - private String getFullPathForGivenResources(String pathToValidYaml) { - return this.getClass().getClassLoader().getResource(pathToValidYaml).getPath(); - } -} diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java index 46cc5de..3b26541 100644 --- a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java +++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java @@ -14,9 +14,9 @@ * limitations under the License. * */ - package org.onap.validation.yaml; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.onap.validation.yaml.exception.YamlProcessingException; import org.onap.validation.yaml.model.YamlDocument; @@ -24,73 +24,125 @@ import org.onap.validation.yaml.model.YamlDocumentFactory; import org.yaml.snakeyaml.parser.ParserException; import org.yaml.snakeyaml.scanner.ScannerException; +import java.io.IOException; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; - class YamlLoaderTest { - private static final int EXPECTED_NUMBER_OF_DOCUMENTS = 5; private static final String LETTER_S_WITH_ASCII_CODE = "s(115)"; - @Test - void shouldLoadAllDocumentsFromYamlFile() throws YamlDocumentFactory.YamlDocumentParsingException { - //when - List<YamlDocument> documents = YamlLoadingUtils.loadValidMultiDocumentYamlFile(); - - //then - assertThat(documents).hasSize(EXPECTED_NUMBER_OF_DOCUMENTS); - } - - @Test - void shouldLoadAllDocumentsFromJsonStyleYamlFile() throws YamlDocumentFactory.YamlDocumentParsingException { - //when - List<YamlDocument> documents = YamlLoadingUtils.loadValidJsonStyleMultiDocumentYamlFile(); - - //then - assertThat(documents).hasSize(EXPECTED_NUMBER_OF_DOCUMENTS); - } - - @Test - void shouldLoadAllDocumentsFromYamlFileUsingPathInString() throws YamlProcessingException { - //when - List<YamlDocument> documents = YamlLoadingUtils.loadValidMultiDocumentYamlFileUsingStringPath(); - - //then - assertThat(documents).hasSize(EXPECTED_NUMBER_OF_DOCUMENTS); - } - - @Test - void shouldThrowExceptionWhenLoadingDocumentsFromInvalidYamlFile() { - //when /then - assertThatThrownBy(YamlLoadingUtils::tryToLoadMultiDocumentInvalidYamlFile) - .isInstanceOf(ParserException.class) - .hasMessageContaining("expected the node content, but found '<document end>'"); - } - - @Test - void shouldThrowExceptionWhenLoadingDocumentsFromInvalidYamlFileUsingPathInString() { - //when /then - assertThatThrownBy(YamlLoadingUtils::tryToLoadMultiDocumentInvalidYamlFileUsingStringPath) - .isInstanceOf(ParserException.class) - .hasMessageContaining("expected the node content, but found '<document end>'"); + @Nested + class FromUrlLoader { + @Test + void shouldLoadAllDocumentsFromYamlFile() throws YamlDocumentFactory.YamlDocumentParsingException { + // when + List<YamlDocument> documents = YamlLoadingUtils.loadValidMultiDocumentYamlFile(); + + // then + assertThat(documents).hasSize(EXPECTED_NUMBER_OF_DOCUMENTS); + } + + @Test + void shouldLoadAllDocumentsFromJsonStyleYamlFile() throws YamlDocumentFactory.YamlDocumentParsingException { + // when + List<YamlDocument> documents = YamlLoadingUtils.loadValidJsonStyleMultiDocumentYamlFile(); + + // then + assertThat(documents).hasSize(EXPECTED_NUMBER_OF_DOCUMENTS); + } + + @Test + void shouldThrowExceptionWhenLoadingDocumentsFromInvalidYamlFile() { + // when then + assertThatThrownBy(YamlLoadingUtils::tryToLoadMultiDocumentInvalidYamlFile) + .isInstanceOf(ParserException.class) + .hasMessageContaining("expected the node content, but found '<document end>'"); + } + + @Test + void shouldThrowExceptionWhenLoadingInvalidYamlFileWithIncorrectKeyMapping() { + // when then + assertThatThrownBy(YamlLoadingUtils::tryToLoadInvalidYamlFileWithIncorrectKeyMapping) + .isInstanceOf(ScannerException.class) + .hasMessageContaining("mapping values are not allowed here"); + } + + @Test + void shouldThrowExceptionWhenLoadingInvalidYamlFileWithUnknownEscapeCharacter() { + // when then + assertThatThrownBy(YamlLoadingUtils::tryToLoadInvalidYamlFileWithUnknownEscapeCharacter) + .isInstanceOf(ScannerException.class) + .hasMessageContaining("found unknown escape character " + LETTER_S_WITH_ASCII_CODE); + } } - @Test - void shouldThrowExceptionWhenLoadingInvalidYamlFileWithIncorrectKeyMapping() { - //when /then - assertThatThrownBy(YamlLoadingUtils::tryToLoadInvalidYamlFileWithIncorrectKeyMapping) - .isInstanceOf(ScannerException.class) - .hasMessageContaining("mapping values are not allowed here"); + @Nested + class FromStringPathLoader { + @Test + void shouldLoadAllDocumentsFromYamlFileUsingPathInString() throws YamlProcessingException { + // when + List<YamlDocument> documents = YamlLoadingUtils.loadValidMultiDocumentYamlFileUsingStringPath(); + + // then + assertThat(documents).hasSize(EXPECTED_NUMBER_OF_DOCUMENTS); + } + + @Test + void shouldThrowExceptionWhenLoadingDocumentsFromInvalidYamlFileUsingPathInString() { + // when then + assertThatThrownBy(YamlLoadingUtils::tryToLoadMultiDocumentInvalidYamlFileUsingStringPath) + .isInstanceOf(ParserException.class) + .hasMessageContaining("expected the node content, but found '<document end>'"); + } } - @Test - void shouldThrowExceptionWhenLoadingInvalidYamlFileWithUnknownEscapeCharacter() { - //when /then - assertThatThrownBy(YamlLoadingUtils::tryToLoadInvalidYamlFileWithUnknownEscapeCharacter) - .isInstanceOf(ScannerException.class) - .hasMessageContaining("found unknown escape character " + LETTER_S_WITH_ASCII_CODE); + @Nested + class FromByteArrayLoader { + private final YamlLoader YAML_LOADER = new YamlLoader(new YamlDocumentFactory()); + + @Test + void shouldLoadAllDocumentsFromYamlFile() throws YamlDocumentFactory.YamlDocumentParsingException, IOException { + // when + List<YamlDocument> documents = YAML_LOADER.loadMultiDocumentYaml(YamlLoadingUtils.readFile(YamlLoadingUtils.PATH_TO_VALID_YAML)); + + // then + assertThat(documents).hasSize(EXPECTED_NUMBER_OF_DOCUMENTS); + } + + @Test + void shouldLoadAllDocumentsFromJsonStyleYamlFile() throws YamlDocumentFactory.YamlDocumentParsingException, IOException { + // when + List<YamlDocument> documents = YAML_LOADER.loadMultiDocumentYaml(YamlLoadingUtils.readFile(YamlLoadingUtils.PATH_TO_VALID_JSON_STYLE_YAML)); + + // then + assertThat(documents).hasSize(EXPECTED_NUMBER_OF_DOCUMENTS); + } + + @Test + void shouldThrowExceptionWhenLoadingDocumentsFromInvalidYamlFile() { + // when then + assertThatThrownBy(() -> YAML_LOADER.loadMultiDocumentYaml(YamlLoadingUtils.readFile(YamlLoadingUtils.PATH_TO_MULTI_DOCUMENT_INVALID_YAML))) + .isInstanceOf(ParserException.class) + .hasMessageContaining("expected the node content, but found '<document end>'"); + } + + @Test + void shouldThrowExceptionWhenLoadingInvalidYamlFileWithIncorrectKeyMapping() { + // when then + assertThatThrownBy(() -> YAML_LOADER.loadMultiDocumentYaml(YamlLoadingUtils.readFile(YamlLoadingUtils.PATH_TO_INVALID_YAML_WITH_INCORRECT_KEY_MAPPING))) + .isInstanceOf(ScannerException.class) + .hasMessageContaining("mapping values are not allowed here"); + } + + @Test + void shouldThrowExceptionWhenLoadingInvalidYamlFileWithUnknownEscapeCharacter() { + // when then + assertThatThrownBy(() -> YAML_LOADER.loadMultiDocumentYaml(YamlLoadingUtils.readFile(YamlLoadingUtils.PATH_TO_INVALID_YAML_WITH_UNKNOWN_ESCAPE_CHARACTER))) + .isInstanceOf(ScannerException.class) + .hasMessageContaining("found unknown escape character " + LETTER_S_WITH_ASCII_CODE); + } } } diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java index b65029f..b16d3ea 100644 --- a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java +++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Nokia + *Copyright 2020 Nokia * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,78 +14,88 @@ * limitations under the License. * */ - package org.onap.validation.yaml; import org.onap.validation.yaml.exception.YamlProcessingException; import org.onap.validation.yaml.model.YamlDocument; +import org.onap.validation.yaml.model.YamlDocumentFactory; +import java.io.IOException; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import static org.onap.validation.yaml.model.YamlDocumentFactory.YamlDocumentParsingException; public final class YamlLoadingUtils { - private YamlLoadingUtils() { } + public static final YamlLoader YAML_LOADER = new YamlLoader(new YamlDocumentFactory()); + + private YamlLoadingUtils() { + } 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"; - 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 final String PATH_TO_VALID_YAML = "yaml_schema/PM_Dictionary.yaml"; + public static final String PATH_TO_VALID_JSON_STYLE_YAML = "yaml_schema/PM_Dictionary_JSON_Style.yaml"; + public static final String PATH_TO_SIMPLE_VALID_SCHEMA = "yaml_schema/Simple_Valid_Schema.yaml"; + public static final String PATH_TO_SIMPLE_VALID_SCHEMA_MULTI_ROOT = "yaml_schema/Simple_Valid_Schema_Multi_Root.yaml"; + public static final String PATH_TO_SIMPLE_INVALID_SCHEMA = "yaml_schema/Simple_Invalid_Schema_Construction.yaml"; + public static final String PATH_TO_SIMPLE_INVALID_SCHEMA_FOR_LAZY_LOADING = "yaml_schema/Simple_Invalid_Schema_LazyLoading.yaml"; + public static final String PATH_TO_MULTI_DOCUMENT_INVALID_YAML = "yaml_schema/Multi_Document_Invalid.yaml"; + public static final String PATH_TO_INVALID_YAML_WITH_INCORRECT_KEY_MAPPING = "yaml_schema/Simple_Invalid_Mapping_Value.yaml"; + public static final String PATH_TO_INVALID_YAML_WITH_UNKNOWN_ESCAPE_CHARACTER = "yaml_schema/Simple_Unknown_Escape_Character.yaml"; public static List<YamlDocument> loadValidMultiDocumentYamlFile() throws YamlDocumentParsingException { - return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_YAML)); + return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_YAML)); } public static List<YamlDocument> loadValidJsonStyleMultiDocumentYamlFile() throws YamlDocumentParsingException { - return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_JSON_STYLE_YAML)); + return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_JSON_STYLE_YAML)); } public static List<YamlDocument> loadValidMultiDocumentYamlFileUsingStringPath() throws YamlProcessingException { - return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_YAML).getPath()); + return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_YAML).getPath()); } public static YamlDocument loadSimpleValidYamlSchemaFile() throws YamlDocumentParsingException { - return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_SIMPLE_VALID_SCHEMA)).get(0); + return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_SIMPLE_VALID_SCHEMA)).get(0); } public static YamlDocument loadSimpleInvalidYamlSchemaFile() throws YamlDocumentParsingException { - return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_SIMPLE_INVALID_SCHEMA)).get(0); + return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_SIMPLE_INVALID_SCHEMA)).get(0); } public static YamlDocument loadSimpleInvalidYamlSchemaForLazyLoadingFile() throws YamlDocumentParsingException { - return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_SIMPLE_INVALID_SCHEMA_FOR_LAZY_LOADING)).get(0); + return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_SIMPLE_INVALID_SCHEMA_FOR_LAZY_LOADING)).get(0); } public static YamlDocument loadSimpleValidYamlSchemaWithMultiRootFile() throws YamlDocumentParsingException { - return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_SIMPLE_VALID_SCHEMA_MULTI_ROOT)).get(0); + return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_SIMPLE_VALID_SCHEMA_MULTI_ROOT)).get(0); } public static List<YamlDocument> tryToLoadMultiDocumentInvalidYamlFile() throws YamlDocumentParsingException { - return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_MULTI_DOCUMENT_INVALID_YAML)); + return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_MULTI_DOCUMENT_INVALID_YAML)); } public static List<YamlDocument> tryToLoadMultiDocumentInvalidYamlFileUsingStringPath() throws YamlProcessingException { - return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_MULTI_DOCUMENT_INVALID_YAML).getPath()); + return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_MULTI_DOCUMENT_INVALID_YAML).getPath()); } public static List<YamlDocument> tryToLoadInvalidYamlFileWithIncorrectKeyMapping() throws YamlDocumentParsingException { - return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_INVALID_YAML_WITH_INCORRECT_KEY_MAPPING)); + return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_INVALID_YAML_WITH_INCORRECT_KEY_MAPPING)); } public static List<YamlDocument> tryToLoadInvalidYamlFileWithUnknownEscapeCharacter() throws YamlDocumentParsingException { - return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_INVALID_YAML_WITH_UNKNOWN_ESCAPE_CHARACTER)); + return YAML_LOADER.loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_INVALID_YAML_WITH_UNKNOWN_ESCAPE_CHARACTER)); + } + + public static byte[] readFile(String path) throws IOException { + String file = getUrlForGivenPath(path).getFile(); + return Files.readAllBytes(Path.of(file)); } private static URL getUrlForGivenPath(String path) { diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlValidatorTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlValidatorTest.java index 9d289c0..3d993f8 100644 --- a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlValidatorTest.java +++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/YamlValidatorTest.java @@ -38,34 +38,34 @@ class YamlValidatorTest { @Test void shouldCreateValidatorUsingSchemaLoadedFromYamlFileAndValidatedJsonStyleDocumentsFromThatFile() throws YamlProcessingException { - //given + // given List<YamlDocument> documents = YamlLoadingUtils.loadValidJsonStyleMultiDocumentYamlFile(); YamlValidator validator = new YamlValidator(new YamlSchemaFactory().createTreeStructuredYamlSchema(documents.get(0))); Map<Integer, List<SchemaValidationError>> validationErrors = new HashMap<>(); - //when + // when for (int documentIndex = 1; documentIndex < documents.size(); documentIndex++) { validationErrors.put(documentIndex, validator.validate(documents.get(documentIndex))); } - //then + // then assertValidatorReturnedCorrectErrors(validationErrors); } @Test void shouldCreateValidatorUsingSchemaLoadedFromYamlFileAndValidatedDocumentsFromThatFile() throws YamlProcessingException { - //given + // given List<YamlDocument> documents = YamlLoadingUtils.loadValidMultiDocumentYamlFile(); YamlValidator validator = new YamlValidator(new YamlSchemaFactory().createTreeStructuredYamlSchema(documents.get(0))); Map<Integer, List<SchemaValidationError>> validationErrors = new HashMap<>(); - //when + // when for (int documentIndex = 1; documentIndex < documents.size(); documentIndex++) { validationErrors.put(documentIndex, validator.validate(documents.get(documentIndex))); } - //then + // then assertValidatorReturnedCorrectErrors(validationErrors); } diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java index 70219b3..d7d1153 100644 --- a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java +++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/model/YamlDocumentFactoryTest.java @@ -33,7 +33,7 @@ class YamlDocumentFactoryTest { @Test void shouldTurnMapOfUnknownKeyTypeToMapWithStringKeysAndBeAbleToReturnStringifyValues() throws YamlDocumentParsingException { - //given + // given List<String> testList = List.of("element1", "element11"); Map<Object, Object> testEmptyMap = Collections.emptyMap(); Map<Object, Object> inputMap = Map.of( @@ -42,17 +42,17 @@ class YamlDocumentFactoryTest { "test2", "element3", 2.67, testEmptyMap); - //when + // when YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); - //then + // then assertYamlDocument(document, inputMap); } @Test void shouldTurnMapOfUnknownKeyTypeToMapWithStringKeysAndBeAbleToExtractSubStructure() throws YamlDocumentParsingException { - //given + // given Map<Object, Object> subStructureMap = Map.of( "subTest1", "subElement1", "subTest2", "subElement2"); @@ -60,55 +60,55 @@ class YamlDocumentFactoryTest { "test", "element1", "structure", subStructureMap); - //when + // when YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); - //then + // then assertYamlDocument(document, inputMap); } @Test void shouldTurnMapOfUnknownKeyTypeToMapWithStringKeysAndBeAbleToExtractParametersList() throws YamlDocumentParsingException { - //given + // given List<String> parametersList = List.of("parameter1", "parameter2"); Map<Object, Object> inputMap = Map.of( "test", "element1", "parameters", parametersList); - //when + // when YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); - //then + // then assertYamlDocument(document, inputMap); } @Test void shouldThrowExceptionIfGetSubStructureIsCalledOnList() throws YamlDocumentParsingException { - //given + // given List<String> testList = List.of("element1", "element2"); Map<Object, Object> inputMap = Collections.singletonMap("test", testList); YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); - //when + // when Throwable ex = catchThrowable(() -> document.getSubStructure("test")); - //then + // then assertYamlDocumentParsingException(ex, testList); } @Test void shouldThrowExceptionIfGetSubStructureIsCalledOnString() throws YamlDocumentParsingException { - //given + // given Map<Object, Object> inputMap = Collections.singletonMap("test", "testElement"); YamlDocument document = new YamlDocumentFactory().createYamlDocument(inputMap); - //when + // when Throwable ex = catchThrowable(() -> document.getSubStructure("test")); - //then + // then assertYamlDocumentParsingException(ex, "testElement"); } diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/model/YamlParameterListFactoryTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/model/YamlParameterListFactoryTest.java index 70d0235..ab6f882 100644 --- a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/model/YamlParameterListFactoryTest.java +++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/model/YamlParameterListFactoryTest.java @@ -28,47 +28,47 @@ class YamlParameterListFactoryTest { @Test void shouldCreateEmptyParametersList() { - //when + // when YamlParametersList parametersList = new YamlParameterListFactory().createEmptyYamlParameterList(); - //then + // then assertThat(parametersList).isNotNull(); assertThat(parametersList.getParameters()).isEmpty(); } @Test void shouldCreateParametersListContainingStringsFromListContainingSimpleTypes() { - //given + // given List<Object> testList = List.of("test1", 3, 23.45, 'a', "test2"); - //when + // when YamlParametersList parametersList = new YamlParameterListFactory().createYamlParameterList(testList); - //then + // then assertYamlParametersList(parametersList, testList); } @Test void shouldCreateParametersListContainingStringsFromListContainingVariousTypes() { - //given + // given List<Object> testList = List.of("test1", 3, List.of(2, 3, 4), "test2"); - //when + // when YamlParametersList parametersList = new YamlParameterListFactory().createYamlParameterList(testList); - //then + // then assertYamlParametersList(parametersList, testList); } @Test void shouldCreateListWithOneStringWhenGivenObjectIsNotList() { - //given + // given Object testObject = "test"; - //when + // when YamlParametersList parametersList = new YamlParameterListFactory().createYamlParameterList(testObject); - //then + // then assertYamlParametersList(parametersList, Collections.singletonList(testObject)); } diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/process/YamlValidationProcessTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/process/YamlValidationProcessTest.java index 79cc105..5c9d8e5 100644 --- a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/process/YamlValidationProcessTest.java +++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/process/YamlValidationProcessTest.java @@ -38,61 +38,61 @@ class YamlValidationProcessTest { @Test void shouldReturnNoErrorWhenProcessingValidPmDictionaryYaml() throws YamlProcessingException { - //given + // given List<YamlDocument> documents = YamlLoadingUtils.loadValidMultiDocumentYamlFile(); YamlSchema schema = new YamlSchemaFactory().createTreeStructuredYamlSchema(documents.get(0)); YamlDocument document = documents.get(VALID_YAML_DOCUMENT_INDEX); - //when + // when List<SchemaValidationError> errors = new YamlValidationProcess(schema, document).validate(); - //then + // then assertThat(errors).isEmpty(); } @Test void shouldReturnOneErrorWhenProcessingPmDictionaryYamlWithMissingField() throws YamlProcessingException { - //given + // given List<YamlDocument> documents = YamlLoadingUtils.loadValidMultiDocumentYamlFile(); YamlSchema schema = new YamlSchemaFactory().createTreeStructuredYamlSchema(documents.get(0)); YamlDocument document = documents.get(YAML_DOCUMENT_WITH_MISSING_FIELD_INDEX); - //when + // when List<SchemaValidationError> errors = new YamlValidationProcess(schema, document).validate(); - //then + // then assertThat(errors).hasSize(1); } @Test void shouldReturnTwoErrorsWhenProcessingPmDictionaryYamlWithMissingFieldAndIncorrectValue() throws YamlProcessingException { - //given + // given List<YamlDocument> documents = YamlLoadingUtils.loadValidMultiDocumentYamlFile(); YamlSchema schema = new YamlSchemaFactory().createTreeStructuredYamlSchema(documents.get(0)); YamlDocument document = documents.get(YAML_DOCUMENT_WITH_MISSING_FIELD_AND_WRONG_VALUE_INDEX); - //when + // when List<SchemaValidationError> errors = new YamlValidationProcess(schema, document).validate(); - //then + // then assertThat(errors).hasSize(2); } @Test void shouldThrowExceptionWhenProcessingPmDictionaryIsNotValidYaml() throws YamlProcessingException { - //given + // given List<YamlDocument> documents = YamlLoadingUtils.loadValidMultiDocumentYamlFile(); YamlDocument schemaInYaml = YamlLoadingUtils.loadSimpleInvalidYamlSchemaForLazyLoadingFile(); YamlSchema schema = new YamlSchemaFactory().createTreeStructuredYamlSchema(schemaInYaml); YamlDocument document = documents.get(VALID_YAML_DOCUMENT_INDEX); - //when + // when Throwable ex = catchThrowable(() -> new YamlValidationProcess(schema, document).validate()); - //then + // then assertThat(ex) .isInstanceOf(YamlProcessingException.class) .hasMessageContaining("Lazy loading failed, due to yaml parsing exception."); diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/schema/YamlSchemaFactoryTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/schema/YamlSchemaFactoryTest.java index 6757556..efc304c 100644 --- a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/schema/YamlSchemaFactoryTest.java +++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/schema/YamlSchemaFactoryTest.java @@ -37,13 +37,13 @@ class YamlSchemaFactoryTest { @Test void shouldCreateYamlSchemaFromYamlDocumentWithMultipleRoots() throws YamlProcessingException { - //given + // given YamlDocument documents = YamlLoadingUtils.loadSimpleValidYamlSchemaWithMultiRootFile(); - //when + // when YamlSchema schema = new YamlSchemaFactory().createTreeStructuredYamlSchema(documents); - //then + // then assertThat(schema).isNotNull(); assertThat(schema.getRootNodes()) .extracting(YamlSchemaNode::getName) @@ -54,13 +54,13 @@ class YamlSchemaFactoryTest { @Test void shouldCreateYamlSchemaFromYamlDocument() throws YamlProcessingException { - //given + // given YamlDocument documents = YamlLoadingUtils.loadSimpleValidYamlSchemaFile(); - //when + // when YamlSchema schema = new YamlSchemaFactory().createTreeStructuredYamlSchema(documents); - //then + // then assertThat(schema).isNotNull(); assertThat(schema.getRootNodes()).hasSize(1); YamlSchemaNode pmMetaData = schema.getRootNodes().get(0); @@ -101,10 +101,10 @@ class YamlSchemaFactoryTest { @Test void shouldThrowYamlParsingExceptionWhenLoadedSchemaIsInvalid() throws YamlDocumentParsingException { - //given + // given YamlDocument documents = YamlLoadingUtils.loadSimpleInvalidYamlSchemaFile(); - //when /then + //when then assertThatThrownBy(() -> new YamlSchemaFactory().createTreeStructuredYamlSchema(documents)) .isInstanceOf(YamlDocumentParsingException.class) .hasMessageContaining(String.format( diff --git a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/schema/node/YamlSchemaNodeFactoryTest.java b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/schema/node/YamlSchemaNodeFactoryTest.java index 0bdd9d0..646b8a2 100644 --- a/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/schema/node/YamlSchemaNodeFactoryTest.java +++ b/pmdictionaryvalidation/src/test/java/org/onap/validation/yaml/schema/node/YamlSchemaNodeFactoryTest.java @@ -38,16 +38,16 @@ public class YamlSchemaNodeFactoryTest { @Test void shouldThrowExceptionDuringLazyLoadingWhenLoadedSchemaHaveInvalidSubStructure() throws YamlProcessingException { - //given + // given String nodeName = "pmMetaData"; YamlDocument document = YamlLoadingUtils.loadSimpleInvalidYamlSchemaForLazyLoadingFile(); YamlSchemaNode node = new YamlSchemaNodeFactory() .createNode(nodeName, ROOT_PATH, document.getSubStructure(nodeName)); - //when + // when Throwable ex = catchThrowable(node::getNextNodes); - //then + // then assertThat(ex) .isInstanceOf(YamlSchemaNode.YamlSchemaProcessingException.class) .hasMessageContaining("Lazy loading failed, due to yaml parsing exception."); @@ -56,49 +56,50 @@ public class YamlSchemaNodeFactoryTest { @Test void shouldCreateLeafNodeIfGivenYamlDocumentHaveNoSubStructure() throws YamlProcessingException { - //given + // given String nodeName = "leaf_test"; String comment = "test leaf node"; List<String> acceptedValues = List.of("val1", "val2"); - Map<Object, Object> nodeInYamlFormat = new HashMap<>(); - nodeInYamlFormat.put(YamlSchemaNodeFactory.PRESENCE_KEY, YamlSchemaNodeFactory.PRESENCE_REQUIRED_KEY); - nodeInYamlFormat.put(YamlSchemaNodeFactory.COMMENT_KEY, comment); - nodeInYamlFormat.put(YamlSchemaNodeFactory.VALUE_KET, acceptedValues); + Map<Object, Object> nodeInYamlFormat = Map.of( + YamlSchemaNodeFactory.PRESENCE_KEY, YamlSchemaNodeFactory.PRESENCE_REQUIRED_KEY, + YamlSchemaNodeFactory.COMMENT_KEY, comment, + YamlSchemaNodeFactory.VALUE_KET, acceptedValues); YamlDocument document = new YamlDocumentFactory().createYamlDocument(nodeInYamlFormat); - //when + // when YamlSchemaNode yamlSchemaNode = new YamlSchemaNodeFactory().createNode(nodeName, ROOT_PATH, document); - //then + // then assertThatLeafNodeIsValid( yamlSchemaNode, nodeName, ROOT_PATH, true, comment, - acceptedValues.toArray(new String[acceptedValues.size()]) + acceptedValues.toArray(new String[0]) ); } @Test void shouldCreateBranchNodeIfGivenYamlDocumentHaveSubStructure() throws YamlProcessingException { - //given + // given String nodeName = "branch_test"; String comment = "test branch node"; - Map<Object, Object> subStructure = new HashMap<>(); String subNode1Name = "branch_test_node1"; String subNode2Name = "branch_test_node2"; - subStructure.put(subNode1Name, new HashMap<>()); - subStructure.put(subNode2Name, new HashMap<>()); - Map<Object, Object> nodeInYamlFormat = new HashMap<>(); - nodeInYamlFormat.put(YamlSchemaNodeFactory.PRESENCE_KEY, YamlSchemaNodeFactory.PRESENCE_REQUIRED_KEY); - nodeInYamlFormat.put(YamlSchemaNodeFactory.COMMENT_KEY, comment); - nodeInYamlFormat.put(YamlSchemaNodeFactory.STRUCTURE_KEY, subStructure); + Map<Object, Object> subStructure = Map.of( + subNode1Name, new HashMap<>(), + subNode2Name, new HashMap<>()); + + Map<Object, Object> nodeInYamlFormat = Map.of( + YamlSchemaNodeFactory.PRESENCE_KEY, YamlSchemaNodeFactory.PRESENCE_REQUIRED_KEY, + YamlSchemaNodeFactory.COMMENT_KEY, comment, + YamlSchemaNodeFactory.STRUCTURE_KEY, subStructure); YamlDocument document = new YamlDocumentFactory().createYamlDocument(nodeInYamlFormat); - //when + // when YamlSchemaNode yamlSchemaNode = new YamlSchemaNodeFactory().createNode(nodeName, ROOT_PATH, document); - //then + // then assertThatBranchNodeIsValid(yamlSchemaNode, nodeName, ROOT_PATH, true, comment, 2); List<YamlSchemaNode> subNodes = yamlSchemaNode.getNextNodes(); assertThat(subNodes).hasSize(2); |