diff options
author | vasraz <vasyl.razinkov@est.tech> | 2023-06-28 15:34:47 +0100 |
---|---|---|
committer | Vasyl Razinkov <vasyl.razinkov@est.tech> | 2023-06-29 10:31:23 +0000 |
commit | 2e4af1e0c0611851f450b2e215485064f6795958 (patch) | |
tree | 2c766e17841c24dc40faa98ff260234ea2cfa80a /catalog-be/src/test/java/org | |
parent | 6d4057b032a30daa8388f9f1a323eae1581eb91c (diff) |
Implement YAML Validator
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech>
Change-Id: I0365d4160984e4d68906959fb801ec7da5449b77
Issue-ID: SDC-4537
Diffstat (limited to 'catalog-be/src/test/java/org')
-rw-r--r-- | catalog-be/src/test/java/org/openecomp/sdc/be/tosca/SchemaFiles.java | 76 | ||||
-rw-r--r-- | catalog-be/src/test/java/org/openecomp/sdc/be/tosca/SchemaFilesTest.java | 131 |
2 files changed, 131 insertions, 76 deletions
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/SchemaFiles.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/SchemaFiles.java deleted file mode 100644 index 839095f1ca..0000000000 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/SchemaFiles.java +++ /dev/null @@ -1,76 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.sdc.be.tosca; - -import org.junit.Test; -import org.yaml.snakeyaml.Yaml; - -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.List; -import java.util.stream.Collectors; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class SchemaFiles { - - @Test - public void testValidateYamlNormativeFiles(){ - String importToscaPath = "src/main/resources/import/tosca"; - assertTrue(checkValidYamlInFileTree(importToscaPath)); - } - - @Test - public void testRainyYamlNormativeFiles(){ - String importToscaPathTest = "src/test/resources/yamlValidation"; - assertFalse(checkValidYamlInFileTree(importToscaPathTest)); - } - - private boolean checkValidYamlInFileTree(String fileTree) { - - try { - List<Path> fileTreeYamlList = Files.walk(Paths.get(fileTree)) - .filter(path -> path.getFileName().toString().toLowerCase().endsWith(".yml")) - .collect(Collectors.toList()); - - for (Path yamlFile : fileTreeYamlList) { - try { - FileInputStream inputStream = new FileInputStream(yamlFile.toAbsolutePath().toString()); - Yaml yaml = new Yaml(); - Object content = yaml.load(inputStream); - } catch (Exception e) { - System.out.println("Not valid yaml in file creation : " + yamlFile.toAbsolutePath().toString()); - return false; - } - } - } catch (IOException e) { - System.out.println("Error in reading file from folder : " + fileTree); - return false; - } - return true; - } - - -} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/SchemaFilesTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/SchemaFilesTest.java new file mode 100644 index 0000000000..ddcf649c22 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/SchemaFilesTest.java @@ -0,0 +1,131 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.networknt.schema.JsonSchemaFactory; +import com.networknt.schema.SpecVersion; +import com.networknt.schema.ValidationMessage; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.test.util.TestResourcesHandler; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.parser.ParserException; +import org.yaml.snakeyaml.scanner.ScannerException; + +class SchemaFilesTest { + + @Test + void testValidateYamlNormativeFiles() { + String importToscaPath = "src/main/resources/import/tosca"; + assertTrue(checkValidYamlInFileTree(importToscaPath)); + } + + @Test + void testRainyYamlNormativeFiles() { + String importToscaPathTest = "src/test/resources/yamlValidation"; + assertFalse(checkValidYamlInFileTree(importToscaPathTest)); + } + + private boolean checkValidYamlInFileTree(final String fileTree) { + AtomicBoolean ret = new AtomicBoolean(true); + try (final Stream<Path> pathStream = Files.walk(Paths.get(fileTree))) { + pathStream + .filter(path -> path.getFileName().toString().toLowerCase().endsWith(".yml")) + .forEach(yamlFile -> { + try { + new Yaml().load(new FileInputStream(yamlFile.toAbsolutePath().toString())); + } catch (final Exception e) { + System.out.println("Not valid yaml in file creation : " + yamlFile.toAbsolutePath()); + ret.set(false); + } + }); + } catch (final IOException e) { + System.out.println("Error in reading file from folder : " + fileTree); + return false; + } + return ret.get(); + } + + @Test + void yamlValidation_test_no_valid() { + final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + final JsonSchemaFactory factory = + JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012)).objectMapper(mapper).build(); + + try (final Stream<Path> pathStream = Files.walk(Paths.get("src/test/resources/yamlValidation"))) { + pathStream + .filter(path -> path.getFileName().toString().toLowerCase().startsWith("data_types_no-valid-")) + .forEach(path -> { + try (final InputStream schemaFile = TestResourcesHandler.getResourceAsStream("yamlValidation/noValid/" + "schema.json"); + final InputStream yamlFile = TestResourcesHandler.getResourceAsStream("yamlValidation/noValid/" + path.getFileName())) { + final Set<ValidationMessage> validationMessages = factory.getSchema(schemaFile).validate(mapper.readTree(yamlFile)); + validationMessages.forEach(System.out::println); + assertFalse(validationMessages.isEmpty()); + } catch (JsonParseException e) { + assertTrue(e.getCause() instanceof ParserException || e.getCause() instanceof ScannerException); + } catch (IOException e) { + fail(e.getMessage()); + } + }); + } catch (final IOException e) { + fail(e.getMessage()); + } + } + @Test + void yamlValidation_test_valid() { + final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + final JsonSchemaFactory factory = + JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012)).objectMapper(mapper).build(); + + try (final Stream<Path> pathStream = Files.walk(Paths.get("src/test/resources/yamlValidation"))) { + pathStream + .filter(path -> path.getFileName().toString().toLowerCase().startsWith("data_types_valid-")) + .forEach(path -> { + try (final InputStream schemaFile = TestResourcesHandler.getResourceAsStream("yamlValidation/noValid/" + "schema.json"); + final InputStream yamlFile = TestResourcesHandler.getResourceAsStream("yamlValidation/noValid/" + path.getFileName())) { + final Set<ValidationMessage> validationMessages = factory.getSchema(schemaFile).validate(mapper.readTree(yamlFile)); + validationMessages.forEach(System.out::println); + assertTrue(validationMessages.isEmpty()); + } catch (IOException e) { + fail(e.getMessage()); + } + }); + } catch (final IOException e) { + fail(e.getMessage()); + } + } + +} |