summaryrefslogtreecommitdiffstats
path: root/csarvalidation/src/test/java/org/onap
diff options
context:
space:
mode:
authorBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>2020-07-08 14:37:27 +0200
committerBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>2020-07-14 14:51:34 +0200
commitae6a1ca9e670a9ab2c7302af89f0dd17bf54ba83 (patch)
treeaaaf29803463b76456bc48e776d1f27426a764c1 /csarvalidation/src/test/java/org/onap
parentb93e6a61eb52c364c43190f40c33aa9045ad90d2 (diff)
Add validation of YAML documents, with use of schema
Issue-ID: VNFSDK-594 Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> Change-Id: I4afc573f564c642a349736e50b50c591b9b18650
Diffstat (limited to 'csarvalidation/src/test/java/org/onap')
-rw-r--r--csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java13
-rw-r--r--csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java9
-rw-r--r--csarvalidation/src/test/java/org/onap/validation/yaml/YamlValidatorTest.java85
-rw-r--r--csarvalidation/src/test/java/org/onap/validation/yaml/process/YamlValidationProcessTest.java101
4 files changed, 206 insertions, 2 deletions
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 1680a40..03999a3 100644
--- a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java
+++ b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoaderTest.java
@@ -20,11 +20,12 @@ package org.onap.validation.yaml;
import org.junit.Test;
import org.onap.validation.yaml.model.YamlDocument;
import org.onap.validation.yaml.model.YamlDocumentFactory;
+import org.yaml.snakeyaml.parser.ParserException;
-import java.net.URL;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class YamlLoaderTest {
@@ -35,7 +36,15 @@ public class YamlLoaderTest {
List<YamlDocument> documents = YamlLoadingUtils.loadValidMultiDocumentYamlFile();
// then
- assertThat(documents.size()).isEqualTo(4);
+ assertThat(documents).hasSize(4);
+ }
+
+ @Test
+ public void shouldThrowExceptionWhenLoadingDocumentsFromInvalidYamlFile() {
+ // when then
+ assertThatThrownBy(YamlLoadingUtils::tryToLoadMultiDocumentInvalidYamlFile
+ ).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 1e3618b..5827f66 100644
--- a/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java
+++ b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlLoadingUtils.java
@@ -28,11 +28,16 @@ public final class YamlLoadingUtils {
private YamlLoadingUtils() {}
+ public static final int VALID_YAML_DOCUMENT_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;
+
private 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";
public static List<YamlDocument> loadValidMultiDocumentYamlFile() throws YamlDocumentParsingException {
return new YamlLoader().loadMultiDocumentYamlFile(getUrlForGivenPath(PATH_TO_VALID_YAML));
@@ -54,6 +59,10 @@ public final class YamlLoadingUtils {
return new YamlLoader().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));
+ }
+
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
new file mode 100644
index 0000000..418ae55
--- /dev/null
+++ b/csarvalidation/src/test/java/org/onap/validation/yaml/YamlValidatorTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.exception.YamlProcessingException;
+import org.onap.validation.yaml.model.SchemaValidationError;
+import org.onap.validation.yaml.model.YamlDocument;
+import org.onap.validation.yaml.schema.YamlSchemaFactory;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.onap.validation.yaml.YamlLoadingUtils.VALID_YAML_DOCUMENT_INDEX;
+import static org.onap.validation.yaml.YamlLoadingUtils.YAML_DOCUMENT_WITH_MISSING_FIELD_INDEX;
+import static org.onap.validation.yaml.YamlLoadingUtils.YAML_DOCUMENT_WITH_MISSING_FIELD_AND_WRONG_VALUE_INDEX;
+
+public class YamlValidatorTest {
+
+ @Test
+ public void shouldCreateValidatorUsingSchemaLoadedFromYamlFileAndValidatedDocumentsFromThatFile()
+ throws YamlProcessingException {
+
+ // given
+ List<YamlDocument> documents = YamlLoadingUtils.loadValidMultiDocumentYamlFile();
+ YamlValidator validator = new YamlValidator(new YamlSchemaFactory().createTreeStructuredYamlSchema(documents.get(0)));
+ Map<Integer,List<SchemaValidationError>> validationErrors = new HashMap<>();
+
+ SchemaValidationError expectedValidationValueError =
+ new SchemaValidationError(
+ "/pmMetaData/pmFields/measResultType",
+ "Value is not in array of accepted values.\n"
+ + " value: integer\n"
+ + " accepted values: [float, uint32, uint64]"
+ );
+ SchemaValidationError expectedValidationKeyError =
+ new SchemaValidationError(
+ "/pmMetaData/pmFields/",
+ "Key not found: measChangeType"
+ );
+
+ // when
+ for (int documentIndex = 1 ; documentIndex < documents.size() ; documentIndex++) {
+ validationErrors.put(documentIndex, validator.validate(documents.get(documentIndex)));
+ }
+
+ // then
+ assertThat(validationErrors.size()).isEqualTo(3);
+ assertThat(validationErrors).containsKeys(1,2,3);
+ assertThat(validationErrors.get(YAML_DOCUMENT_WITH_MISSING_FIELD_AND_WRONG_VALUE_INDEX)).hasSize(2);
+ assertThat(validationErrors.get(YAML_DOCUMENT_WITH_MISSING_FIELD_AND_WRONG_VALUE_INDEX))
+ .usingFieldByFieldElementComparator()
+ .containsAll(
+ Lists.list(
+ expectedValidationValueError,
+ expectedValidationKeyError
+ ));
+ assertThat(validationErrors.get(YAML_DOCUMENT_WITH_MISSING_FIELD_INDEX)).hasSize(1);
+ assertThat(validationErrors.get(YAML_DOCUMENT_WITH_MISSING_FIELD_INDEX))
+ .usingFieldByFieldElementComparator()
+ .contains(
+ expectedValidationKeyError
+ );
+ assertThat(validationErrors.get(VALID_YAML_DOCUMENT_INDEX)).hasSize(0);
+ }
+
+}
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
new file mode 100644
index 0000000..1380a9d
--- /dev/null
+++ b/csarvalidation/src/test/java/org/onap/validation/yaml/process/YamlValidationProcessTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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.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.model.YamlDocument;
+import org.onap.validation.yaml.schema.YamlSchema;
+import org.onap.validation.yaml.schema.YamlSchemaFactory;
+
+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.VALID_YAML_DOCUMENT_INDEX;
+import static org.onap.validation.yaml.YamlLoadingUtils.YAML_DOCUMENT_WITH_MISSING_FIELD_INDEX;
+import static org.onap.validation.yaml.YamlLoadingUtils.YAML_DOCUMENT_WITH_MISSING_FIELD_AND_WRONG_VALUE_INDEX;
+
+
+public class YamlValidationProcessTest {
+
+ @Test
+ public void shouldReturnNoErrorWhenProcessingValidPmDictionaryYaml()
+ throws YamlProcessingException {
+ // given
+ List<YamlDocument> documents = YamlLoadingUtils.loadValidMultiDocumentYamlFile();
+ YamlSchema schema = new YamlSchemaFactory().createTreeStructuredYamlSchema(documents.get(0));
+ YamlDocument document = documents.get(VALID_YAML_DOCUMENT_INDEX);
+
+ // when
+ List<SchemaValidationError> errors = new YamlValidationProcess(schema,document).validate();
+
+ // then
+ assertThat(errors).isEmpty();
+ }
+
+ @Test
+ public void shouldReturnOneErrorWhenProcessingPmDictionaryYamlWithMissingField()
+ throws YamlProcessingException {
+ // 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
+ List<SchemaValidationError> errors = new YamlValidationProcess(schema,document).validate();
+
+ // then
+ assertThat(errors).hasSize(1);
+ }
+
+ @Test
+ public void shouldReturnTwoErrorsWhenProcessingPmDictionaryYamlWithMissingFieldAndIncorrectValue()
+ throws YamlProcessingException {
+ // 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
+ List<SchemaValidationError> errors = new YamlValidationProcess(schema,document).validate();
+
+ // then
+ assertThat(errors).hasSize(2);
+ }
+
+ @Test
+ public void shouldThrowExceptionWhenProcessingPmDictionaryIsNotValidYaml()
+ throws YamlProcessingException {
+ // 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 then
+ assertThatThrownBy(() ->
+ new YamlValidationProcess(schema,document).validate()
+ ).isInstanceOf(YamlProcessingException.class)
+ .hasMessageContaining(
+ String.format("Lazy loading failed, due to yaml parsing exception.")
+ );
+ }
+}