Final commit to master merge from
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / SchemaFiles.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.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.List;
32 import java.util.stream.Collectors;
33
34 import org.junit.Test;
35 import org.yaml.snakeyaml.Yaml;
36
37 public class SchemaFiles {
38         
39         @Test
40         public void testValidateYamlNormativeFiles(){
41                 String importToscaPath = "src/main/resources/import/tosca";
42                 assertTrue(checkValidYamlInFileTree(importToscaPath));
43         }
44         
45         @Test
46         public void testRainyYamlNormativeFiles(){
47                 String importToscaPathTest = "src/test/resources/yamlValidation";
48                 assertFalse(checkValidYamlInFileTree(importToscaPathTest));
49         }
50         
51         private boolean checkValidYamlInFileTree(String fileTree)  {
52                 
53                 try {
54                         List<Path> fileTreeYamlList = Files.walk(Paths.get(fileTree))
55                           .filter(path -> path.getFileName().toString().toLowerCase().endsWith(".yml"))
56                           .collect(Collectors.toList());
57                         
58                         for (Path yamlFile : fileTreeYamlList) {
59                                 try {
60                                         FileInputStream inputStream = new FileInputStream(yamlFile.toAbsolutePath().toString());
61                                 Yaml yaml = new Yaml();
62                                 Object content = yaml.load(inputStream);
63                                 } catch (Exception e) { 
64                                         System.out.println("Not valid yaml in file creation : " + yamlFile.toAbsolutePath().toString());
65                                         return false;
66                                 }
67                         }
68                 } catch (IOException e) {
69                         System.out.println("Error in reading file from folder : " + fileTree);
70                         return false;
71                 }
72                 return true;
73         }
74         
75         
76 }