2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.onap.aai.schema;
23 //import org.junit.Test;
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;
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;
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;
49 import static org.junit.Assert.assertEquals;
50 import static org.junit.Assert.fail;
52 public class ValidateOXMTest {
55 public void testFindXmlPropContainingSpace() throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
56 boolean foundIssue = false;
57 List<File> fileList = getFiles();
59 StringBuilder msg = new StringBuilder();
60 for (File file : fileList) {
61 msg.append(file.getAbsolutePath().replaceAll(".*aai-schema", ""));
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);
68 for (int i = 0; i < nodeList.getLength(); i++) {
71 msg.append(nodeList.item(i).getParentNode().getParentNode().getAttributes().getNamedItem("name").getNodeValue());
80 System.out.println(msg.toString());
81 fail("Node type xml-property should have space.");
87 * Verifies that all of the node types in the oxm's have their uri templates.
88 * @throws XPathExpressionException
90 * @throws SAXException
91 * @throws ParserConfigurationException
94 public void allNodeTypesHaveAAIUriTemplate() throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
95 boolean foundIssue = false;
96 List<File> fileList = getFiles();
98 StringBuilder msg = new StringBuilder();
99 for (File file : fileList) {
100 msg.append(file.getAbsolutePath().replaceAll(".*aai-schema", ""));
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);
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")) {
119 System.out.println(msg.toString());
120 fail("Missing uriTemplate in oxm.");
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());
135 //TODO test that all oxm xml are valid xml
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");
144 return stringBuilder.toString();
147 public String printNode(Node node, Document document) throws IOException {
148 StringWriter stringWriter = new StringWriter();
149 return stringWriter.toString();
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);