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 /csarvalidation/src/test/java | |
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
Diffstat (limited to 'csarvalidation/src/test/java')
7 files changed, 226 insertions, 9 deletions
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; |