diff options
Diffstat (limited to 'pmdictionaryvalidation')
14 files changed, 461 insertions, 296 deletions
diff --git a/pmdictionaryvalidation/README.md b/pmdictionaryvalidation/README.md new file mode 100644 index 0000000..2bc91a5 --- /dev/null +++ b/pmdictionaryvalidation/README.md @@ -0,0 +1,26 @@ +PMDictionary Validation +======================= +This module can be used as a library to validate pmdictionary against the schema (schema is the first document in the file). + +How to use PMDictionary validation library +------------------------------------------ +VNF-SDK validation library (pmdictionaryvalidation) should be used to validate the PM_Dictionary file. + Below dependency should be added to the required modules in your project. + + <dependency> + <groupId>org.onap.vnfsdk.validation</groupId> + <artifactId>validation-pmdictionary</artifactId> + <version>version</version> + </dependency> + +How to validate PMDictionary +-------------------------- +1.Validate PMDictionary from a path to the file. + + new YamlContentValidator().validate(pathToFile) + +2.Validate PMDictionary file from the byte array. + + new YamlContentValidator().validate(fileContentAsByteArray) + +Above methods return list of YamlDocumentValidationError(empty list for no errors) or throw YamlProcessingException/YAMLException when something goes wrong. diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlContentValidator.java index 2de4f48..5e1238b 100644 --- a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlContentValidator.java @@ -21,6 +21,7 @@ import org.onap.validation.yaml.error.SchemaValidationError; import org.onap.validation.yaml.error.YamlDocumentValidationError; import org.onap.validation.yaml.exception.YamlProcessingException; import org.onap.validation.yaml.model.YamlDocument; +import org.onap.validation.yaml.model.YamlDocumentFactory; import org.onap.validation.yaml.schema.YamlSchema; import org.onap.validation.yaml.schema.YamlSchemaFactory; @@ -28,23 +29,33 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; -public class YamlFileValidator { +public class YamlContentValidator { private static final int FIRST_DOCUMENT_INDEX = 1; + private static final YamlLoader YAML_LOADER = new YamlLoader(new YamlDocumentFactory()); - public List<YamlDocumentValidationError> validateYamlFileWithSchema(String pathToFile) - throws YamlProcessingException { + public List<YamlDocumentValidationError> validate(String pathToFile) + throws YamlProcessingException { + List<YamlDocument> documents = YAML_LOADER.loadMultiDocumentYamlFile(pathToFile); + return getYamlDocumentValidationErrors(documents); + } - List<YamlDocument> documents = new YamlLoader().loadMultiDocumentYamlFile(pathToFile); - if(!documents.isEmpty()) { - return validateDocuments(documents); - } else { + public List<YamlDocumentValidationError> validate(byte[] yamlWithSchema) + throws YamlProcessingException { + List<YamlDocument> documents = YAML_LOADER.loadMultiDocumentYaml(yamlWithSchema); + return getYamlDocumentValidationErrors(documents); + } + + private List<YamlDocumentValidationError> getYamlDocumentValidationErrors(List<YamlDocument> documents) throws YamlProcessingException { + if (documents.isEmpty()) { throw new YamlProcessingException("PM_Dictionary YAML file is empty"); + } else { + return validateDocuments(documents); } } private List<YamlDocumentValidationError> validateDocuments(List<YamlDocument> documents) - throws YamlProcessingException { + throws YamlProcessingException { List<YamlDocumentValidationError> yamlFileValidationErrors = new ArrayList<>(); YamlSchema schema = extractSchema(documents); @@ -52,7 +63,7 @@ public class YamlFileValidator { for (int index = FIRST_DOCUMENT_INDEX; index < documents.size(); index++) { List<SchemaValidationError> validationErrors = validator.validate(documents.get(index)); - yamlFileValidationErrors.addAll(transformErrors(index,validationErrors)); + yamlFileValidationErrors.addAll(transformErrors(index, validationErrors)); } return yamlFileValidationErrors; @@ -60,9 +71,9 @@ public class YamlFileValidator { private List<YamlDocumentValidationError> transformErrors(int index, List<SchemaValidationError> validationErrors) { return validationErrors - .stream() - .map(error->new YamlDocumentValidationError(index, error.getPath(), error.getMessage())) - .collect(Collectors.toList()); + .stream() + .map(error -> new YamlDocumentValidationError(index, error.getPath(), error.getMessage())) + .collect(Collectors.toList()); } private YamlSchema extractSchema(List<YamlDocument> documents) throws YamlProcessingException { diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java index 1a5eef9..c23da0a 100644 --- a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java @@ -24,6 +24,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; @@ -33,29 +34,49 @@ import java.util.List; class YamlLoader { - private static final Logger LOGGER = LoggerFactory.getLogger(YamlLoader.class); + private static final Logger LOGGER = LoggerFactory.getLogger(YamlLoader.class); + private final YamlDocumentFactory documentFactory; + + YamlLoader(YamlDocumentFactory documentFactory) { + this.documentFactory = documentFactory; + } + + List<YamlDocument> loadMultiDocumentYaml(byte[] yamlWithSchema) + throws YamlDocumentFactory.YamlDocumentParsingException { + List<YamlDocument> documents = new ArrayList<>(); + try (InputStream yamlStream = new ByteArrayInputStream(yamlWithSchema)) { + documents.addAll(loadMultiDocumentYaml(yamlStream)); + } catch (IOException e) { + LOGGER.error("Failed to load multi document YAML", e); + } + return documents; + } List<YamlDocument> loadMultiDocumentYamlFile(URL path) - throws YamlDocumentFactory.YamlDocumentParsingException { - List<YamlDocument> documentsFromFile = new ArrayList<>(); + throws YamlDocumentFactory.YamlDocumentParsingException { + List<YamlDocument> documents = new ArrayList<>(); try (InputStream yamlStream = path.openStream()) { - for (Object yamlDocument : new Yaml().loadAll(yamlStream)) { - documentsFromFile.add( - new YamlDocumentFactory().createYamlDocument(yamlDocument) - ); - } + documents.addAll(loadMultiDocumentYaml(yamlStream)); } catch (IOException e) { - LOGGER.error("Failed to load multi document YAML file",e); + LOGGER.error("Failed to load multi document YAML file", e); } - return documentsFromFile; + return documents; } List<YamlDocument> loadMultiDocumentYamlFile(String path) - throws YamlProcessingException { + throws YamlProcessingException { try { return loadMultiDocumentYamlFile(new URL("file://" + path)); } catch (MalformedURLException e) { throw new YamlProcessingException("Fail to read file under given path.", e); } } + + private List<YamlDocument> loadMultiDocumentYaml(InputStream yamlStream) throws YamlDocumentFactory.YamlDocumentParsingException { + List<YamlDocument> documents = new ArrayList<>(); + for (Object yamlDocument : new Yaml().loadAll(yamlStream)) { + documents.add(documentFactory.createYamlDocument(yamlDocument)); + } + return documents; + } } diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java index 0f5b480..a3e9636 100644 --- a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java @@ -24,18 +24,16 @@ import org.onap.validation.yaml.model.YamlDocumentFactory; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Optional; public class YamlSchemaBranchNode extends YamlSchemaNode { private final YamlDocument nextNodesInLazyForm; - private Optional<List<YamlSchemaNode>> nextNodes; + private List<YamlSchemaNode> nextNodes = Collections.emptyList(); YamlSchemaBranchNode(String name, String path, boolean required, String comment, YamlDocument nextNodesInLazyForm) { super(name, path, required, comment); this.nextNodesInLazyForm = nextNodesInLazyForm; - this.nextNodes = Optional.empty(); } @Override @@ -51,7 +49,7 @@ public class YamlSchemaBranchNode extends YamlSchemaNode { @Override public synchronized List<YamlSchemaNode> getNextNodes() throws YamlSchemaProcessingException { try { - return nextNodes.orElseGet(this::loadNextNodes); + return nextNodes.isEmpty() ? this.loadNextNodes() : nextNodes; } catch (YamlSchemaLazyLoadingException lazyLoadingException) { throw new YamlSchemaProcessingException(lazyLoadingException); } @@ -65,7 +63,7 @@ public class YamlSchemaBranchNode extends YamlSchemaNode { .createYamlDocument(nextNodesInLazyForm.getYaml().get(key)); loadedNextNodes.add(new YamlSchemaNodeFactory().createNode(key, getPath() + getName() + "/", substructure)); } - nextNodes = Optional.of(loadedNextNodes); + nextNodes = loadedNextNodes; return loadedNextNodes; } catch (YamlProcessingException e) { throw new YamlSchemaLazyLoadingException("Lazy loading failed, due to yaml parsing exception.",e); 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); |