diff options
author | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2020-07-09 14:02:23 +0200 |
---|---|---|
committer | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2020-07-14 14:52:49 +0200 |
commit | 4ce8c75c2559bde35b5fca46ec2c94d6b5070b6f (patch) | |
tree | 56f7440b88a28214a8b476dbf0205e9329e3936d | |
parent | ae6a1ca9e670a9ab2c7302af89f0dd17bf54ba83 (diff) |
use YAML schema validator in rule R816745
Issue-ID: VNFSDK-594
Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Change-Id: I70c6150662b69833d4e190a6adc1047840a78975
22 files changed, 429 insertions, 23 deletions
diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java index 47b963e..f1bbffc 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java @@ -19,20 +19,58 @@ package org.onap.cvc.csar.cc.sol004; import org.onap.cli.fw.schema.OnapCommandSchema; import org.onap.cvc.csar.CSARArchive; import org.onap.cvc.csar.cc.VTPValidateCSARBase; +import org.onap.validation.yaml.YamlFileValidator; +import org.onap.validation.yaml.error.YamlDocumentValidationError; +import org.onap.validation.yaml.exception.YamlProcessingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.nio.file.Path; import java.util.List; import java.util.Map; @OnapCommandSchema(schema = "vtp-validate-csar-r816745.yaml") public class VTPValidateCSARR816745 extends VTPValidateCSARBase { + private static final Logger LOGGER = LoggerFactory.getLogger(VTPValidateCSARR816745.class); + + private static class CSARPmDictionaryValidationError extends CSARArchive.CSARError { + + CSARPmDictionaryValidationError(int documentNumber, String file, String path, String message) { + super("0x1000"); + this.message = String.format( + "Invalid YAML document in PM_Dictionary file. %n" + + "In document number %s (excluding document with schema) error occur. %n" + + "Path: %s%n" + + "%s", + documentNumber, path, message + ); + this.file = file; + } + + } + + private static class CSARPmDictionaryLoadingError extends CSARArchive.CSARError { + + CSARPmDictionaryLoadingError(String file, String message) { + super("0x2000"); + this.message = String.format( + "Fail to load PM_Dictionary With error: %s", + message + ); + this.file = file; + } + + } + private static final String PM_DICTIONARY = "onap_pm_dictionary"; @Override - protected void validateCSAR(CSARArchive csar) throws Exception { + protected void validateCSAR(CSARArchive csar) { Map<String, Map<String, List<String>>> nonManoFields = csar.getManifest().getNonMano(); - if(nonManoFields.containsKey(PM_DICTIONARY)) { - validateYamlFile(getLocationOfPmDictionaryFile(nonManoFields)); + String rootPath = csar.getWorkspace().getPathToCsarFolder().map(Path::toString).orElse("/"); + if (nonManoFields.containsKey(PM_DICTIONARY)) { + validateYamlFile(rootPath+"/",getLocationOfPmDictionaryFile(nonManoFields)); } } @@ -40,8 +78,34 @@ public class VTPValidateCSARR816745 extends VTPValidateCSARBase { return nonManoFields.get(PM_DICTIONARY).get("source").get(0); } - private void validateYamlFile(String path) { - throw new UnsupportedOperationException("Under development"); + private void validateYamlFile(String rootPath, String artifactPath) { + try { + List<YamlDocumentValidationError> validationErrors = + new YamlFileValidator().validateYamlFileWithSchema(rootPath+artifactPath); + addAllErrorsReportedByVaidator(artifactPath, validationErrors); + } catch (YamlProcessingException e) { + LOGGER.error("Failed to load PM_Dictionary file.", e); + errors.add(new CSARPmDictionaryLoadingError( + artifactPath, + e.getMessage() + )); + } + + } + + private void addAllErrorsReportedByVaidator(String artifactPath, List<YamlDocumentValidationError> validationErrors) { + for(YamlDocumentValidationError validationError: validationErrors) { + addPmDictionaryValidationError(artifactPath, validationError); + } + } + + private void addPmDictionaryValidationError(String artifactPath, YamlDocumentValidationError validationError) { + errors.add(new CSARPmDictionaryValidationError( + validationError.getYamlDocumentNumber(), + artifactPath, + validationError.getPath(), + validationError.getMessage() + )); } @Override diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java new file mode 100644 index 0000000..2de4f48 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java @@ -0,0 +1,72 @@ +/* + * 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.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.schema.YamlSchema; +import org.onap.validation.yaml.schema.YamlSchemaFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class YamlFileValidator { + + private static final int FIRST_DOCUMENT_INDEX = 1; + + public List<YamlDocumentValidationError> validateYamlFileWithSchema(String pathToFile) + throws YamlProcessingException { + + List<YamlDocument> documents = new YamlLoader().loadMultiDocumentYamlFile(pathToFile); + if(!documents.isEmpty()) { + return validateDocuments(documents); + } else { + throw new YamlProcessingException("PM_Dictionary YAML file is empty"); + } + } + + private List<YamlDocumentValidationError> validateDocuments(List<YamlDocument> documents) + throws YamlProcessingException { + + List<YamlDocumentValidationError> yamlFileValidationErrors = new ArrayList<>(); + YamlSchema schema = extractSchema(documents); + YamlValidator validator = new YamlValidator(schema); + + for (int index = FIRST_DOCUMENT_INDEX; index < documents.size(); index++) { + List<SchemaValidationError> validationErrors = validator.validate(documents.get(index)); + yamlFileValidationErrors.addAll(transformErrors(index,validationErrors)); + } + + return yamlFileValidationErrors; + } + + private List<YamlDocumentValidationError> transformErrors(int index, List<SchemaValidationError> validationErrors) { + return validationErrors + .stream() + .map(error->new YamlDocumentValidationError(index, error.getPath(), error.getMessage())) + .collect(Collectors.toList()); + } + + private YamlSchema extractSchema(List<YamlDocument> documents) throws YamlProcessingException { + return new YamlSchemaFactory().createTreeStructuredYamlSchema(documents.get(0)); + } + +} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java index 804f1ea..1a5eef9 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java @@ -17,6 +17,7 @@ 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 org.slf4j.Logger; @@ -25,15 +26,16 @@ import org.yaml.snakeyaml.Yaml; import java.io.IOException; import java.io.InputStream; +import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; -public class YamlLoader { +class YamlLoader { private static final Logger LOGGER = LoggerFactory.getLogger(YamlLoader.class); - public List<YamlDocument> loadMultiDocumentYamlFile(URL path) + List<YamlDocument> loadMultiDocumentYamlFile(URL path) throws YamlDocumentFactory.YamlDocumentParsingException { List<YamlDocument> documentsFromFile = new ArrayList<>(); try (InputStream yamlStream = path.openStream()) { @@ -48,4 +50,12 @@ public class YamlLoader { return documentsFromFile; } + List<YamlDocument> loadMultiDocumentYamlFile(String path) + throws YamlProcessingException { + try { + return loadMultiDocumentYamlFile(new URL("file://" + path)); + } catch (MalformedURLException e) { + throw new YamlProcessingException("Fail to read file under given path.", e); + } + } } diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java index 17ebd1f..9430df4 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java @@ -18,7 +18,7 @@ package org.onap.validation.yaml; import org.onap.validation.yaml.exception.YamlProcessingException; -import org.onap.validation.yaml.model.SchemaValidationError; +import org.onap.validation.yaml.error.SchemaValidationError; import org.onap.validation.yaml.model.YamlDocument; import org.onap.validation.yaml.process.YamlValidationProcess; import org.onap.validation.yaml.schema.YamlSchema; diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/SchemaValidationError.java b/csarvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java index 45f1647..6ffe6d4 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/model/SchemaValidationError.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java @@ -15,7 +15,7 @@ * */ -package org.onap.validation.yaml.model; +package org.onap.validation.yaml.error; public class SchemaValidationError { private final String path; diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/error/YamlDocumentValidationError.java b/csarvalidation/src/main/java/org/onap/validation/yaml/error/YamlDocumentValidationError.java new file mode 100644 index 0000000..f04708f --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/error/YamlDocumentValidationError.java @@ -0,0 +1,42 @@ +/* + * 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.error; + +public class YamlDocumentValidationError { + private final int yamlDocumentNumber; + private final String path; + private final String message; + + public YamlDocumentValidationError(int yamlDocumentNumber, String path, String message) { + this.yamlDocumentNumber = yamlDocumentNumber; + this.path = path; + this.message = message; + } + + public int getYamlDocumentNumber() { + return yamlDocumentNumber; + } + + public String getPath() { + return path; + } + + public String getMessage() { + return message; + } +} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java b/csarvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java index edfdbb8..99c2437 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java @@ -23,6 +23,10 @@ public class YamlProcessingException extends Exception { super(message, throwable); } + public YamlProcessingException(String message) { + super(message); + } + public YamlProcessingException(Throwable throwable) { super(throwable); } diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java b/csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java index f55b729..e3fadb6 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java @@ -18,7 +18,7 @@ package org.onap.validation.yaml.process; import org.onap.validation.yaml.exception.YamlProcessingException; -import org.onap.validation.yaml.model.SchemaValidationError; +import org.onap.validation.yaml.error.SchemaValidationError; import org.onap.validation.yaml.model.YamlDocument; import org.onap.validation.yaml.schema.YamlSchema; import org.onap.validation.yaml.schema.node.YamlSchemaNode; diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java index 8ff3569..0f5b480 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java @@ -26,9 +26,6 @@ import java.util.Collections; import java.util.List; import java.util.Optional; -import static org.onap.validation.yaml.model.YamlDocumentFactory.YamlDocumentParsingException; -import static org.onap.validation.yaml.model.YamlParameterListFactory.YamlParameterListParsingException; - public class YamlSchemaBranchNode extends YamlSchemaNode { private final YamlDocument nextNodesInLazyForm; diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java index 73470d4..c98f41e 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java @@ -24,7 +24,7 @@ import java.util.List; public class YamlSchemaLeafNode extends YamlSchemaNode { - private YamlParametersList acceptedValues; + private final YamlParametersList acceptedValues; YamlSchemaLeafNode(String name, String path, boolean required, String comment, YamlParametersList acceptedValues) { diff --git a/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/IntegrationTestUtils.java b/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/IntegrationTestUtils.java index 6f183d7..6d90b23 100644 --- a/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/IntegrationTestUtils.java +++ b/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/IntegrationTestUtils.java @@ -87,4 +87,8 @@ public class IntegrationTestUtils { public static List<String> convertToMessagesList(List<CSARArchive.CSARError> errors) { return errors.stream().map(CSARArchive.CSARError::getMessage).collect(Collectors.toList()); } + + public static List<String> convertToFilesList(List<CSARArchive.CSARError> errors) { + return errors.stream().map(CSARArchive.CSARError::getFile).collect(Collectors.toList()); + } } 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 efe6917..70a370e 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 @@ -17,17 +17,22 @@ package org.onap.cvc.csar.cc.sol004; +import org.assertj.core.api.Condition; +import org.assertj.core.api.HamcrestCondition; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.onap.cli.fw.error.OnapCommandExecutionFailed; import org.onap.cvc.csar.CSARArchive; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.CoreMatchers.containsString; import static org.onap.cvc.csar.cc.sol004.IntegrationTestUtils.configureTestCase; +import static org.onap.cvc.csar.cc.sol004.IntegrationTestUtils.convertToMessagesList; +import static org.onap.cvc.csar.cc.sol004.IntegrationTestUtils.convertToFilesList; public class VTPValidateCSARR816745IntegrationTest { @@ -58,16 +63,67 @@ public class VTPValidateCSARR816745IntegrationTest { } @Test - public void shouldThrowUnsupportedOperationExceptionWhenCsarContainsPmDictionary() throws Exception { + public void shouldReturnNoErrorsWhenCsarContainsValidPmDictionary() throws Exception { // given - configureTestCase(testCase, TEST_CSAR_DIRECTORY + "csar-with-pm-dictionary.csar", "vtp-validate-csar-r816745.yaml", IS_PNF); + configureTestCase(testCase, TEST_CSAR_DIRECTORY + "csar-with-valid-pm-dictionary.csar", "vtp-validate-csar-r816745.yaml", IS_PNF); + + // when + testCase.execute(); + + // then + List<CSARArchive.CSARError> errors = testCase.getErrors(); + assertThat(errors.size()).isEqualTo(0); + } + + @Test + public void shouldReturnNoErrorsWhenZipContainsCsarWithValidPmDictionary() throws Exception { + // given + configureTestCase(testCase, TEST_CSAR_DIRECTORY + "zip-with-valid-pm-dictionary.zip", "vtp-validate-csar-r816745.yaml", IS_PNF); + + // when + testCase.execute(); + + // then + List<CSARArchive.CSARError> errors = testCase.getErrors(); + assertThat(errors.size()).isEqualTo(0); + } + + @Test + public void shouldReturnListOfErrorsWhenCsarContainsInvalidPmDictionary() throws Exception { + // given + configureTestCase(testCase, TEST_CSAR_DIRECTORY + "csar-with-invalid-pm-dictionary.csar", "vtp-validate-csar-r816745.yaml", IS_PNF); + + // when + testCase.execute(); // then - exceptionRule.expect(OnapCommandExecutionFailed.class); - exceptionRule.expectMessage("Under development"); + assertThatReturnedErrorsAreCorrect(testCase.getErrors()); + } + + @Test + public void shouldReturnListOfErrorsWhenZipContainsCsarWithInvalidPmDictionary() throws Exception { + // given + configureTestCase(testCase, TEST_CSAR_DIRECTORY + "zip-with-invalid-pm-dictionary.zip", "vtp-validate-csar-r816745.yaml", IS_PNF); // when testCase.execute(); + + // then + assertThatReturnedErrorsAreCorrect(testCase.getErrors()); + } + + @Test + public void shouldAddPmDictionaryLoadingErrorWhenGivenInvalidPath() throws Exception { + // given + configureTestCase(testCase, TEST_CSAR_DIRECTORY + "csar-with-empty-pm-dictionary.csar", "vtp-validate-csar-r816745.yaml", IS_PNF); + + // when then + testCase.execute(); + + // then + List<CSARArchive.CSARError> errors = testCase.getErrors(); + assertThat(errors.size()).isEqualTo(1); + assertThat(convertToMessagesList(errors)).contains("Fail to load PM_Dictionary With error: PM_Dictionary YAML file is empty"); } @Test @@ -75,4 +131,39 @@ public class VTPValidateCSARR816745IntegrationTest { assertThat(testCase.getVnfReqsNo()).isEqualTo("R816745"); } + private void assertThatReturnedErrorsAreCorrect(List<CSARArchive.CSARError> errors) { + assertThat(errors.size()).isEqualTo(3); + + Condition<String> containingSameFileForAllErrors = new HamcrestCondition<>( + containsString("Artifacts/Deployment/Measurements/PM_Dictionary.yml") + ); + assertThat(convertToFilesList(errors)).haveExactly(3, containingSameFileForAllErrors); + + Condition<String> containingErrorForMissingValueInFirstDocument = new HamcrestCondition<>(allOf( + containsString("Invalid YAML document in PM_Dictionary file."), + containsString("In document number 1"), + containsString("Path: /pmMetaData/pmFields/"), + containsString("Key not found: measChangeType") + )); + assertThat(convertToMessagesList(errors)).haveExactly(1, containingErrorForMissingValueInFirstDocument); + + Condition<String> containingErrorForWrongValueInFirstDocument = new HamcrestCondition<>(allOf( + 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]") + )); + assertThat(convertToMessagesList(errors)).haveExactly(1, containingErrorForWrongValueInFirstDocument); + + Condition<String> containingErrorForMissingValueInSecondDocument = new HamcrestCondition<>(allOf( + containsString("Invalid YAML document in PM_Dictionary file."), + containsString("In document number 2"), + containsString("Path: /pmMetaData/pmFields/"), + containsString("Key not found: measChangeType") + )); + assertThat(convertToMessagesList(errors)).haveExactly(1, containingErrorForMissingValueInSecondDocument); + } + } diff --git a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlFileValidatorTest.java b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlFileValidatorTest.java new file mode 100644 index 0000000..6ef78ae --- /dev/null +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlFileValidatorTest.java @@ -0,0 +1,95 @@ +/* + * 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.assertj.core.util.Lists; +import org.junit.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.assertThatThrownBy; + +public class YamlFileValidatorTest { + + @Test + public void shouldReturnNoErrorsWhenGivenPathToValidPmDictionaryFile() throws YamlProcessingException { + // given + String path = getFullPathForGivenResources(YamlLoadingUtils.PATH_TO_VALID_YAML); + + // when + List<YamlDocumentValidationError> validationErrors = + new YamlFileValidator().validateYamlFileWithSchema(path); + + // then + assertThat(validationErrors).isNotNull(); + assertThat(validationErrors).hasSize(3); + 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 , + "/pmMetaData/pmFields/", + "Key not found: measChangeType"), + new YamlDocumentValidationError(2 , + "/pmMetaData/pmFields/", + "Key not found: measChangeType") + ) + ); + } + + @Test + public void shouldThrowErrorWhenGivenPathToInvalidPmDictionaryFile() { + // given + String path = getFullPathForGivenResources(YamlLoadingUtils.PATH_TO_MULTI_DOCUMENT_INVALID_YAML); + + // when then + assertThatThrownBy(() -> + new YamlFileValidator().validateYamlFileWithSchema(path) + ).isInstanceOf(ParserException.class) + .hasMessageContaining( + "expected the node content, but found DocumentEnd" + ); + } + + @Test + public void shouldThrowErrorWhenGivenInvalidPath() { + // given + String path ="invalid/path/to/pm_dictionary"; + + // when then + assertThatThrownBy(() -> + new YamlFileValidator().validateYamlFileWithSchema(path) + ).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/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java index 03999a3..e7efc2b 100644 --- a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java @@ -18,6 +18,7 @@ package org.onap.validation.yaml; import org.junit.Test; +import org.onap.validation.yaml.exception.YamlProcessingException; import org.onap.validation.yaml.model.YamlDocument; import org.onap.validation.yaml.model.YamlDocumentFactory; import org.yaml.snakeyaml.parser.ParserException; @@ -40,6 +41,15 @@ public class YamlLoaderTest { } @Test + public void shouldLoadAllDocumentsFromYamlFileUsingPathInString() throws YamlProcessingException { + // when + List<YamlDocument> documents = YamlLoadingUtils.loadValidMultiDocumentYamlFileUsingStringPath(); + + // then + assertThat(documents).hasSize(4); + } + + @Test public void shouldThrowExceptionWhenLoadingDocumentsFromInvalidYamlFile() { // when then assertThatThrownBy(YamlLoadingUtils::tryToLoadMultiDocumentInvalidYamlFile @@ -47,4 +57,12 @@ public class YamlLoaderTest { .hasMessageContaining("expected the node content, but found DocumentEnd"); } + @Test + public void shouldThrowExceptionWhenLoadingDocumentsFromInvalidYamlFileUsingPathInString() { + // when then + assertThatThrownBy(YamlLoadingUtils::tryToLoadMultiDocumentInvalidYamlFileUsingStringPath + ).isInstanceOf(ParserException.class) + .hasMessageContaining("expected the node content, but found DocumentEnd"); + } + } 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 5827f66..5e09e0a 100644 --- a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java @@ -17,6 +17,7 @@ package org.onap.validation.yaml; +import org.onap.validation.yaml.exception.YamlProcessingException; import org.onap.validation.yaml.model.YamlDocument; import java.net.URL; @@ -32,17 +33,21 @@ public final class YamlLoadingUtils { 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; - private static final String PATH_TO_VALID_YAML = "yaml_schema/PM_Dictionary.yaml"; + static final String PATH_TO_VALID_YAML = "yaml_schema/PM_Dictionary.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"; - private static final String PATH_TO_MULTI_DOCUMENT_INVALID_YAML = "yaml_schema/Multi_Document_Invalid.yaml"; + static final String PATH_TO_MULTI_DOCUMENT_INVALID_YAML = "yaml_schema/Multi_Document_Invalid.yaml"; public static List<YamlDocument> loadValidMultiDocumentYamlFile() throws YamlDocumentParsingException { return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_YAML)); } + public static List<YamlDocument> loadValidMultiDocumentYamlFileUsingStringPath() throws YamlProcessingException { + return new YamlLoader().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); } @@ -63,6 +68,10 @@ public final class YamlLoadingUtils { return new YamlLoader().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()); + } + private static URL getUrlForGivenPath(String path) { return YamlLoadingUtils.class.getClassLoader().getResource(path); } 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 418ae55..4927d32 100644 --- a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlValidatorTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlValidatorTest.java @@ -20,7 +20,7 @@ package org.onap.validation.yaml; import org.assertj.core.util.Lists; import org.junit.Test; import org.onap.validation.yaml.exception.YamlProcessingException; -import org.onap.validation.yaml.model.SchemaValidationError; +import org.onap.validation.yaml.error.SchemaValidationError; import org.onap.validation.yaml.model.YamlDocument; import org.onap.validation.yaml.schema.YamlSchemaFactory; diff --git a/csarvalidation/src/test/java/org/onap/validation/yaml/process/YamlValidationProcessTest.java b/csarvalidation/src/test/java/org/onap/validation/yaml/process/YamlValidationProcessTest.java index 1380a9d..12fe9ec 100644 --- a/csarvalidation/src/test/java/org/onap/validation/yaml/process/YamlValidationProcessTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/yaml/process/YamlValidationProcessTest.java @@ -20,7 +20,7 @@ package org.onap.validation.yaml.process; import org.junit.Test; import org.onap.validation.yaml.YamlLoadingUtils; import org.onap.validation.yaml.exception.YamlProcessingException; -import org.onap.validation.yaml.model.SchemaValidationError; +import org.onap.validation.yaml.error.SchemaValidationError; import org.onap.validation.yaml.model.YamlDocument; import org.onap.validation.yaml.schema.YamlSchema; import org.onap.validation.yaml.schema.YamlSchemaFactory; diff --git a/csarvalidation/src/test/resources/pnf/r816745/csar-with-pm-dictionary.csar b/csarvalidation/src/test/resources/pnf/r816745/csar-with-empty-pm-dictionary.csar Binary files differindex 861f602..d0dbdf0 100644 --- a/csarvalidation/src/test/resources/pnf/r816745/csar-with-pm-dictionary.csar +++ b/csarvalidation/src/test/resources/pnf/r816745/csar-with-empty-pm-dictionary.csar diff --git a/csarvalidation/src/test/resources/pnf/r816745/csar-with-invalid-pm-dictionary.csar b/csarvalidation/src/test/resources/pnf/r816745/csar-with-invalid-pm-dictionary.csar Binary files differnew file mode 100644 index 0000000..5f9d058 --- /dev/null +++ b/csarvalidation/src/test/resources/pnf/r816745/csar-with-invalid-pm-dictionary.csar diff --git a/csarvalidation/src/test/resources/pnf/r816745/csar-with-valid-pm-dictionary.csar b/csarvalidation/src/test/resources/pnf/r816745/csar-with-valid-pm-dictionary.csar Binary files differnew file mode 100644 index 0000000..2768e16 --- /dev/null +++ b/csarvalidation/src/test/resources/pnf/r816745/csar-with-valid-pm-dictionary.csar diff --git a/csarvalidation/src/test/resources/pnf/r816745/zip-with-invalid-pm-dictionary.zip b/csarvalidation/src/test/resources/pnf/r816745/zip-with-invalid-pm-dictionary.zip Binary files differnew file mode 100644 index 0000000..3c46ef8 --- /dev/null +++ b/csarvalidation/src/test/resources/pnf/r816745/zip-with-invalid-pm-dictionary.zip diff --git a/csarvalidation/src/test/resources/pnf/r816745/zip-with-valid-pm-dictionary.zip b/csarvalidation/src/test/resources/pnf/r816745/zip-with-valid-pm-dictionary.zip Binary files differnew file mode 100644 index 0000000..6c8970c --- /dev/null +++ b/csarvalidation/src/test/resources/pnf/r816745/zip-with-valid-pm-dictionary.zip |