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.apache.commons.io.FileUtils;
24 import org.apache.commons.io.filefilter.DirectoryFileFilter;
25 import org.apache.commons.io.filefilter.FileFileFilter;
26 import org.apache.commons.io.filefilter.FileFilterUtils;
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.Files;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
46 import java.util.stream.Collectors;
48 import static org.junit.Assert.fail;
50 public class ValidateOXMTest {
53 public void testFindXmlPropContainingSpace() throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
54 boolean foundIssue = false;
55 List<File> fileList = getLatestFiles();
57 StringBuilder msg = new StringBuilder();
58 for (File file : fileList) {
59 msg.append(file.getAbsolutePath().replaceAll(".*aai-schema", ""));
61 Document xmlDocument = getDocument(file);
62 XPath xPath = XPathFactory.newInstance().newXPath();
63 String expression = "/xml-bindings/java-types/java-type/xml-properties/xml-property[@name!='description' and contains(@value,' ')]";
64 NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
66 for (int i = 0; i < nodeList.getLength(); i++) {
69 msg.append(nodeList.item(i).getParentNode().getParentNode().getAttributes().getNamedItem("name").getNodeValue());
78 System.out.println(msg.toString());
79 fail("Node type xml-property should have space.");
85 * Verifies that all of the node types in the oxm's have their uri templates.
86 * @throws XPathExpressionException
88 * @throws SAXException
89 * @throws ParserConfigurationException
92 public void allNodeTypesHaveAAIUriTemplate() throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
93 boolean foundIssue = false;
94 List<File> fileList = getFiles();
96 StringBuilder msg = new StringBuilder();
97 for (File file : fileList) {
98 msg.append(file.getAbsolutePath().replaceAll(".*aai-schema", ""));
100 Document xmlDocument = getDocument(file);
101 XPath xPath = XPathFactory.newInstance().newXPath();
102 String expression = "/xml-bindings/java-types/java-type[" +
104 "count(xml-properties/xml-property[@name='container']) > 0 " +
105 "or count(xml-properties/xml-property[@name='dependentOn']) > 0" +
107 "and count(xml-properties/xml-property[@name='uriTemplate']) = 0 " +
109 NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
111 for (int i = 0; i < nodeList.getLength(); i++) {
112 String name = nodeList.item(i).getAttributes().getNamedItem("name").getNodeValue();
113 if (name.equals("InstanceFilter") || name.equals("InventoryResponseItems") || name.equals("InventoryResponseItem")) {
123 System.out.println(msg.toString());
124 fail("Missing uriTemplate in oxm.");
131 * Verifies that all specified properties are indexed
132 * Currently set to check that "model-invariant-id","model-version-id" which are aliased are indexed
133 * @throws XPathExpressionException
134 * @throws IOException
135 * @throws SAXException
136 * @throws ParserConfigurationException
139 public void aliasedIndexedPropsAreInIndexedListWithPropName() throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
141 final List<String> props = Arrays.asList("model-invariant-id","model-version-id");
143 boolean foundIssue = false;
144 List<File> fileList = getLatestFiles();
145 StringBuilder msg = new StringBuilder();
147 for (File file : fileList) {
148 msg.append(file.getAbsolutePath().replaceAll(".*aai-schema", ""));
150 for (String prop : props) {
151 Document xmlDocument = getDocument(file);
152 XPath xPath = XPathFactory.newInstance().newXPath();
153 String expression = "/xml-bindings/java-types/java-type[" +
155 "count(xml-properties/xml-property[@name='container']) > 0 " +
156 "or count(xml-properties/xml-property[@name='dependentOn']) > 0" +
158 "and count(xml-properties/xml-property[@name='indexedProps' and not(contains(@value,'" + prop + "'))]) > 0 " + //prop is not in indexed props list
159 "and count(java-attributes/xml-element[@name='" + prop + "']) > 0 " + // prop is a property on obj
162 NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
164 if (nodeList.getLength() > 0) {
169 for (int i = 0; i < nodeList.getLength(); i++) {
170 String name = nodeList.item(i).getAttributes().getNamedItem("name").getNodeValue();
171 if (name.equals("InstanceFilter") || name.equals("InventoryResponseItems") || name.equals("InventoryResponseItem")) {
183 System.out.println(msg.toString());
184 fail("Missing index entry in oxm.");
189 private List<File> getFiles() {
190 Path currentRelativePath = Paths.get("../aai-schema/src/main/resources/").toAbsolutePath();
191 return FileUtils.listFiles(
192 currentRelativePath.toFile(),
193 new RegexFileFilter(".*\\.xml"),
194 DirectoryFileFilter.DIRECTORY)
196 .filter(file -> file.getAbsolutePath().contains("oxm"))
197 .filter(file -> !file.getAbsolutePath().contains("onap")) // skips onap for checks
198 .collect(Collectors.toList());
202 * Finds all of the oxm files for the latest version.
203 * @return list of the latest version of the oxm files.
205 private List<File> getLatestFiles() {
206 List<String> latest = new ArrayList<>();
207 Path currentRelativePath = Paths.get("../aai-schema/src/main/resources/").toAbsolutePath();
208 List<File> subDirs = Arrays.asList(currentRelativePath.toFile().listFiles(File::isDirectory));
209 for (File subDir : subDirs) {
210 String oxm = subDir.getAbsolutePath() + "/oxm";
211 File[] oxms = new File(oxm).listFiles(File::isDirectory);
212 Arrays.stream(oxms).map(File::getAbsolutePath).max(new Comparator<String>() {
213 public int compare(String o1, String o2) {
214 return extractInt(o1) - extractInt(o2);
216 int extractInt(String s) {
217 String num = s.replaceAll("\\D", "");
218 return num.isEmpty() ? 0 : Integer.parseInt(num);
220 }).ifPresent(latest::add);
223 List<File> latestFiles = new ArrayList<>();
227 new RegexFileFilter(".*\\.xml"),
228 DirectoryFileFilter.DIRECTORY)
230 .filter(file -> file.getAbsolutePath().contains("oxm"))
231 .forEach(latestFiles::add));
236 //TODO test that all oxm xml are valid xml
240 public String printNodeList(NodeList nodeList, Document doc) throws IOException {
241 StringBuilder stringBuilder = new StringBuilder();
242 for (int i = 0; i < nodeList.getLength(); i++) {
243 stringBuilder.append(printNode(nodeList.item(i), doc)).append("\n");
245 return stringBuilder.toString();
248 public String printNode(Node node, Document document) throws IOException {
249 StringWriter stringWriter = new StringWriter();
250 return stringWriter.toString();
254 private Document getDocument(File file) throws ParserConfigurationException, SAXException, IOException {
255 InputStream fileIS = new FileInputStream(file);
256 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
257 DocumentBuilder builder = builderFactory.newDocumentBuilder();
258 return builder.parse(fileIS);