Move the aai-schema, annotations and
[aai/schema-service.git] / aai-schema / src / test / java / org / onap / aai / schema / ValidateOXMTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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 package org.onap.aai.schema;
21
22
23 //import org.junit.Test;
24
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.io.filefilter.DirectoryFileFilter;
27 import org.apache.commons.io.filefilter.RegexFileFilter;
28 import org.junit.Test;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Node;
31 import org.w3c.dom.NodeList;
32 import org.xml.sax.SAXException;
33
34 import javax.xml.parsers.DocumentBuilder;
35 import javax.xml.parsers.DocumentBuilderFactory;
36 import javax.xml.parsers.ParserConfigurationException;
37 import javax.xml.xpath.XPath;
38 import javax.xml.xpath.XPathConstants;
39 import javax.xml.xpath.XPathExpressionException;
40 import javax.xml.xpath.XPathFactory;
41 import java.io.*;
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44 import java.util.ArrayList;
45 import java.util.Collection;
46 import java.util.List;
47 import java.util.stream.Collectors;
48
49 import static org.junit.Assert.assertEquals;
50 import static org.junit.Assert.fail;
51
52 public class ValidateOXMTest {
53
54         @Test
55         public void testFindXmlPropContainingSpace() throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
56                 boolean foundIssue = false;
57                 List<File> fileList = getFiles();
58
59                 StringBuilder msg = new StringBuilder();
60                 for (File file : fileList) {
61                         msg.append(file.getAbsolutePath().replaceAll(".*aai-schema", ""));
62                         msg.append("\n");
63                         Document xmlDocument = getDocument(file);
64                         XPath xPath = XPathFactory.newInstance().newXPath();
65                         String expression = "/xml-bindings/java-types/java-type/xml-properties/xml-property[@name!='description' and contains(@value,' ')]";
66                         NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
67
68                         for (int i = 0; i < nodeList.getLength(); i++) {
69                                 foundIssue = true;
70                                 msg.append("\t");
71                                 msg.append(nodeList.item(i).getParentNode().getParentNode().getAttributes().getNamedItem("name").getNodeValue());
72                                 msg.append("\n");
73                                 msg.append("\t");
74                                 msg.append("\n");
75                         }
76
77                 }
78
79                 if (foundIssue) {
80                         System.out.println(msg.toString());
81                         fail("Node type xml-property should have space.");
82                 }
83
84         }
85
86         /**
87          * Verifies that all of the node types in the oxm's have their uri templates.
88          * @throws XPathExpressionException
89          * @throws IOException
90          * @throws SAXException
91          * @throws ParserConfigurationException
92          */
93         @Test
94         public void allNodeTypesHaveAAIUriTemplate() throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
95                 boolean foundIssue = false;
96                 List<File> fileList = getFiles();
97
98                 StringBuilder msg = new StringBuilder();
99                 for (File file : fileList) {
100                         msg.append(file.getAbsolutePath().replaceAll(".*aai-schema", ""));
101                         msg.append("\n");
102                         Document xmlDocument = getDocument(file);
103                         XPath xPath = XPathFactory.newInstance().newXPath();
104                         String expression = "/xml-bindings/java-types/java-type[count(xml-properties/xml-property[@name='container']) > 0 and count(xml-properties/xml-property[@name='uriTemplate']) = 0 ]";
105                         NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
106
107                         for (int i = 0; i < nodeList.getLength(); i++) {
108                                 String name = nodeList.item(i).getAttributes().getNamedItem("name").getNodeValue();
109                                 if (name.equals("InstanceFilter") || name.equals("InventoryResponseItems") || name.equals("InventoryResponseItem")) {
110                                         continue;
111                                 }
112                                 foundIssue = true;
113                                 msg.append("\t");
114                                 msg.append(name);
115                                 msg.append("\n");
116                         }
117                 }
118                 if (foundIssue) {
119                         System.out.println(msg.toString());
120                         fail("Missing uriTemplate in oxm.");
121                 }
122
123         }
124
125         private List<File> getFiles() {
126                 Path currentRelativePath = Paths.get("../aai-schema/src/main/resources/").toAbsolutePath();
127                 return FileUtils.listFiles(
128                                 currentRelativePath.toFile(),
129                                 new RegexFileFilter(".*\\.xml"),
130                                 DirectoryFileFilter.DIRECTORY)
131                                 .stream().filter(file -> file.getAbsolutePath().contains("oxm"))
132                                 .collect(Collectors.toList());
133         }
134
135         //TODO test that all oxm xml are valid xml
136
137
138
139         public String printNodeList(NodeList nodeList, Document doc) throws IOException {
140                 StringBuilder stringBuilder = new StringBuilder();
141                 for (int i = 0; i < nodeList.getLength(); i++) {
142                         stringBuilder.append(printNode(nodeList.item(i), doc)).append("\n");
143                 }
144                 return stringBuilder.toString();
145         }
146
147         public String printNode(Node node, Document document) throws IOException {
148                 StringWriter stringWriter = new StringWriter();
149                 return stringWriter.toString();
150
151         }
152
153         private Document getDocument(File file) throws ParserConfigurationException, SAXException, IOException {
154                 InputStream fileIS = new FileInputStream(file);
155                 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
156                 DocumentBuilder builder = builderFactory.newDocumentBuilder();
157                 return builder.parse(fileIS);
158         }
159
160 }