From 4ce8c75c2559bde35b5fca46ec2c94d6b5070b6f Mon Sep 17 00:00:00 2001 From: Bartosz Gardziejewski Date: Thu, 9 Jul 2020 14:02:23 +0200 Subject: use YAML schema validator in rule R816745 Issue-ID: VNFSDK-594 Signed-off-by: Bartosz Gardziejewski Change-Id: I70c6150662b69833d4e190a6adc1047840a78975 --- .../cvc/csar/cc/sol004/VTPValidateCSARR816745.java | 74 ++++++++++++++- .../onap/validation/yaml/YamlFileValidator.java | 72 +++++++++++++++ .../java/org/onap/validation/yaml/YamlLoader.java | 14 ++- .../org/onap/validation/yaml/YamlValidator.java | 2 +- .../yaml/error/SchemaValidationError.java | 36 ++++++++ .../yaml/error/YamlDocumentValidationError.java | 42 +++++++++ .../yaml/exception/YamlProcessingException.java | 4 + .../yaml/model/SchemaValidationError.java | 36 -------- .../yaml/process/YamlValidationProcess.java | 2 +- .../yaml/schema/node/YamlSchemaBranchNode.java | 3 - .../yaml/schema/node/YamlSchemaLeafNode.java | 2 +- .../cvc/csar/cc/sol004/IntegrationTestUtils.java | 4 + .../VTPValidateCSARR816745IntegrationTest.java | 101 ++++++++++++++++++++- .../validation/yaml/YamlFileValidatorTest.java | 95 +++++++++++++++++++ .../org/onap/validation/yaml/YamlLoaderTest.java | 18 ++++ .../org/onap/validation/yaml/YamlLoadingUtils.java | 13 ++- .../onap/validation/yaml/YamlValidatorTest.java | 2 +- .../yaml/process/YamlValidationProcessTest.java | 2 +- .../pnf/r816745/csar-with-empty-pm-dictionary.csar | Bin 0 -> 7156 bytes .../r816745/csar-with-invalid-pm-dictionary.csar | Bin 0 -> 8774 bytes .../pnf/r816745/csar-with-pm-dictionary.csar | Bin 7156 -> 0 bytes .../pnf/r816745/csar-with-valid-pm-dictionary.csar | Bin 0 -> 8728 bytes .../pnf/r816745/zip-with-invalid-pm-dictionary.zip | Bin 0 -> 6447 bytes .../pnf/r816745/zip-with-valid-pm-dictionary.zip | Bin 0 -> 6396 bytes 24 files changed, 464 insertions(+), 58 deletions(-) create mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java create mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java create mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/error/YamlDocumentValidationError.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/model/SchemaValidationError.java create mode 100644 csarvalidation/src/test/java/org/onap/validation/yaml/YamlFileValidatorTest.java create mode 100644 csarvalidation/src/test/resources/pnf/r816745/csar-with-empty-pm-dictionary.csar create mode 100644 csarvalidation/src/test/resources/pnf/r816745/csar-with-invalid-pm-dictionary.csar delete mode 100644 csarvalidation/src/test/resources/pnf/r816745/csar-with-pm-dictionary.csar create mode 100644 csarvalidation/src/test/resources/pnf/r816745/csar-with-valid-pm-dictionary.csar create mode 100644 csarvalidation/src/test/resources/pnf/r816745/zip-with-invalid-pm-dictionary.zip create mode 100644 csarvalidation/src/test/resources/pnf/r816745/zip-with-valid-pm-dictionary.zip (limited to 'csarvalidation/src') 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>> 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 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 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 validateYamlFileWithSchema(String pathToFile) + throws YamlProcessingException { + + List documents = new YamlLoader().loadMultiDocumentYamlFile(pathToFile); + if(!documents.isEmpty()) { + return validateDocuments(documents); + } else { + throw new YamlProcessingException("PM_Dictionary YAML file is empty"); + } + } + + private List validateDocuments(List documents) + throws YamlProcessingException { + + List yamlFileValidationErrors = new ArrayList<>(); + YamlSchema schema = extractSchema(documents); + YamlValidator validator = new YamlValidator(schema); + + for (int index = FIRST_DOCUMENT_INDEX; index < documents.size(); index++) { + List validationErrors = validator.validate(documents.get(index)); + yamlFileValidationErrors.addAll(transformErrors(index,validationErrors)); + } + + return yamlFileValidationErrors; + } + + private List transformErrors(int index, List validationErrors) { + return validationErrors + .stream() + .map(error->new YamlDocumentValidationError(index, error.getPath(), error.getMessage())) + .collect(Collectors.toList()); + } + + private YamlSchema extractSchema(List 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 loadMultiDocumentYamlFile(URL path) + List loadMultiDocumentYamlFile(URL path) throws YamlDocumentFactory.YamlDocumentParsingException { List documentsFromFile = new ArrayList<>(); try (InputStream yamlStream = path.openStream()) { @@ -48,4 +50,12 @@ public class YamlLoader { return documentsFromFile; } + List 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/error/SchemaValidationError.java b/csarvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java new file mode 100644 index 0000000..6ffe6d4 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java @@ -0,0 +1,36 @@ +/* + * 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 SchemaValidationError { + private final String path; + private final String message; + + public String getPath() { + return path; + } + + public String getMessage() { + return message; + } + + public SchemaValidationError(String path, String message) { + this.path = path; + this.message = message; + } +} 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/model/SchemaValidationError.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/SchemaValidationError.java deleted file mode 100644 index 45f1647..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/model/SchemaValidationError.java +++ /dev/null @@ -1,36 +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.model; - -public class SchemaValidationError { - private final String path; - private final String message; - - public String getPath() { - return path; - } - - public String getMessage() { - return message; - } - - public SchemaValidationError(String path, String message) { - this.path = path; - this.message = message; - } -} 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 convertToMessagesList(List errors) { return errors.stream().map(CSARArchive.CSARError::getMessage).collect(Collectors.toList()); } + + public static List convertToFilesList(List 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 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 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 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 errors) { + assertThat(errors.size()).isEqualTo(3); + + Condition containingSameFileForAllErrors = new HamcrestCondition<>( + containsString("Artifacts/Deployment/Measurements/PM_Dictionary.yml") + ); + assertThat(convertToFilesList(errors)).haveExactly(3, containingSameFileForAllErrors); + + Condition 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 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 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 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; @@ -39,6 +40,15 @@ public class YamlLoaderTest { assertThat(documents).hasSize(4); } + @Test + public void shouldLoadAllDocumentsFromYamlFileUsingPathInString() throws YamlProcessingException { + // when + List documents = YamlLoadingUtils.loadValidMultiDocumentYamlFileUsingStringPath(); + + // then + assertThat(documents).hasSize(4); + } + @Test public void shouldThrowExceptionWhenLoadingDocumentsFromInvalidYamlFile() { // when then @@ -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 loadValidMultiDocumentYamlFile() throws YamlDocumentParsingException { return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_YAML)); } + public static List 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 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-empty-pm-dictionary.csar b/csarvalidation/src/test/resources/pnf/r816745/csar-with-empty-pm-dictionary.csar new file mode 100644 index 0000000..d0dbdf0 Binary files /dev/null and b/csarvalidation/src/test/resources/pnf/r816745/csar-with-empty-pm-dictionary.csar differ 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 new file mode 100644 index 0000000..5f9d058 Binary files /dev/null and b/csarvalidation/src/test/resources/pnf/r816745/csar-with-invalid-pm-dictionary.csar differ diff --git a/csarvalidation/src/test/resources/pnf/r816745/csar-with-pm-dictionary.csar b/csarvalidation/src/test/resources/pnf/r816745/csar-with-pm-dictionary.csar deleted file mode 100644 index 861f602..0000000 Binary files a/csarvalidation/src/test/resources/pnf/r816745/csar-with-pm-dictionary.csar and /dev/null differ 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 new file mode 100644 index 0000000..2768e16 Binary files /dev/null and b/csarvalidation/src/test/resources/pnf/r816745/csar-with-valid-pm-dictionary.csar differ 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 new file mode 100644 index 0000000..3c46ef8 Binary files /dev/null and b/csarvalidation/src/test/resources/pnf/r816745/zip-with-invalid-pm-dictionary.zip differ 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 new file mode 100644 index 0000000..6c8970c Binary files /dev/null and b/csarvalidation/src/test/resources/pnf/r816745/zip-with-valid-pm-dictionary.zip differ -- cgit 1.2.3-korg