2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.tosca;
23 import static org.junit.jupiter.api.Assertions.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25 import static org.junit.jupiter.api.Assertions.fail;
27 import com.fasterxml.jackson.core.JsonParseException;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
30 import com.networknt.schema.JsonSchemaFactory;
31 import com.networknt.schema.SpecVersion;
32 import com.networknt.schema.ValidationMessage;
33 import java.io.FileInputStream;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
40 import java.util.concurrent.atomic.AtomicBoolean;
41 import java.util.stream.Stream;
42 import org.junit.jupiter.api.Test;
43 import org.openecomp.sdc.be.test.util.TestResourcesHandler;
44 import org.yaml.snakeyaml.Yaml;
45 import org.yaml.snakeyaml.parser.ParserException;
46 import org.yaml.snakeyaml.scanner.ScannerException;
48 class SchemaFilesTest {
51 void testValidateYamlNormativeFiles() {
52 String importToscaPath = "src/main/resources/import/tosca";
53 assertTrue(checkValidYamlInFileTree(importToscaPath));
57 void testRainyYamlNormativeFiles() {
58 String importToscaPathTest = "src/test/resources/yamlValidation";
59 assertFalse(checkValidYamlInFileTree(importToscaPathTest));
62 private boolean checkValidYamlInFileTree(final String fileTree) {
63 AtomicBoolean ret = new AtomicBoolean(true);
64 try (final Stream<Path> pathStream = Files.walk(Paths.get(fileTree))) {
66 .filter(path -> path.getFileName().toString().toLowerCase().endsWith(".yml"))
67 .forEach(yamlFile -> {
69 new Yaml().load(new FileInputStream(yamlFile.toAbsolutePath().toString()));
70 } catch (final Exception e) {
71 System.out.println("Not valid yaml in file creation : " + yamlFile.toAbsolutePath());
75 } catch (final IOException e) {
76 System.out.println("Error in reading file from folder : " + fileTree);
83 void yamlValidation_test_no_valid() {
84 final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
85 final JsonSchemaFactory factory =
86 JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012)).objectMapper(mapper).build();
88 try (final Stream<Path> pathStream = Files.walk(Paths.get("src/test/resources/yamlValidation"))) {
90 .filter(path -> path.getFileName().toString().toLowerCase().startsWith("data_types_no-valid-"))
92 try (final InputStream schemaFile = TestResourcesHandler.getResourceAsStream("yamlValidation/noValid/" + "schema.json");
93 final InputStream yamlFile = TestResourcesHandler.getResourceAsStream("yamlValidation/noValid/" + path.getFileName())) {
94 final Set<ValidationMessage> validationMessages = factory.getSchema(schemaFile).validate(mapper.readTree(yamlFile));
95 validationMessages.forEach(System.out::println);
96 assertFalse(validationMessages.isEmpty());
97 } catch (JsonParseException e) {
98 assertTrue(e.getCause() instanceof ParserException || e.getCause() instanceof ScannerException);
99 } catch (IOException e) {
100 fail(e.getMessage());
103 } catch (final IOException e) {
104 fail(e.getMessage());
108 void yamlValidation_test_valid() {
109 final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
110 final JsonSchemaFactory factory =
111 JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012)).objectMapper(mapper).build();
113 try (final Stream<Path> pathStream = Files.walk(Paths.get("src/test/resources/yamlValidation"))) {
115 .filter(path -> path.getFileName().toString().toLowerCase().startsWith("data_types_valid-"))
117 try (final InputStream schemaFile = TestResourcesHandler.getResourceAsStream("yamlValidation/noValid/" + "schema.json");
118 final InputStream yamlFile = TestResourcesHandler.getResourceAsStream("yamlValidation/noValid/" + path.getFileName())) {
119 final Set<ValidationMessage> validationMessages = factory.getSchema(schemaFile).validate(mapper.readTree(yamlFile));
120 validationMessages.forEach(System.out::println);
121 assertTrue(validationMessages.isEmpty());
122 } catch (IOException e) {
123 fail(e.getMessage());
126 } catch (final IOException e) {
127 fail(e.getMessage());