Implement YAML Validator
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / SchemaFilesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.tosca;
22
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;
26
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;
39 import java.util.Set;
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;
47
48 class SchemaFilesTest {
49
50     @Test
51     void testValidateYamlNormativeFiles() {
52         String importToscaPath = "src/main/resources/import/tosca";
53         assertTrue(checkValidYamlInFileTree(importToscaPath));
54     }
55
56     @Test
57     void testRainyYamlNormativeFiles() {
58         String importToscaPathTest = "src/test/resources/yamlValidation";
59         assertFalse(checkValidYamlInFileTree(importToscaPathTest));
60     }
61
62     private boolean checkValidYamlInFileTree(final String fileTree) {
63         AtomicBoolean ret = new AtomicBoolean(true);
64         try (final Stream<Path> pathStream = Files.walk(Paths.get(fileTree))) {
65             pathStream
66                 .filter(path -> path.getFileName().toString().toLowerCase().endsWith(".yml"))
67                 .forEach(yamlFile -> {
68                     try {
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());
72                         ret.set(false);
73                     }
74                 });
75         } catch (final IOException e) {
76             System.out.println("Error in reading file from folder : " + fileTree);
77             return false;
78         }
79         return ret.get();
80     }
81
82     @Test
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();
87
88         try (final Stream<Path> pathStream = Files.walk(Paths.get("src/test/resources/yamlValidation"))) {
89             pathStream
90                 .filter(path -> path.getFileName().toString().toLowerCase().startsWith("data_types_no-valid-"))
91                 .forEach(path -> {
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());
101                     }
102                 });
103         } catch (final IOException e) {
104             fail(e.getMessage());
105         }
106     }
107     @Test
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();
112
113         try (final Stream<Path> pathStream = Files.walk(Paths.get("src/test/resources/yamlValidation"))) {
114             pathStream
115                 .filter(path -> path.getFileName().toString().toLowerCase().startsWith("data_types_valid-"))
116                 .forEach(path -> {
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());
124                     }
125                 });
126         } catch (final IOException e) {
127             fail(e.getMessage());
128         }
129     }
130
131 }