[AAI] Release docker artifacts 1.12.3 for aai/schema-service
[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.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;
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.Files;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.util.*;
46 import java.util.stream.Collectors;
47
48 import static org.junit.Assert.fail;
49
50 public class ValidateOXMTest {
51
52         @Test
53         public void testFindXmlPropContainingSpace() throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
54                 boolean foundIssue = false;
55                 List<File> fileList = getLatestFiles();
56
57                 StringBuilder msg = new StringBuilder();
58                 for (File file : fileList) {
59                         msg.append(file.getAbsolutePath().replaceAll(".*aai-schema", ""));
60                         msg.append("\n");
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);
65
66                         for (int i = 0; i < nodeList.getLength(); i++) {
67                                 foundIssue = true;
68                                 msg.append("\t");
69                                 msg.append(nodeList.item(i).getParentNode().getParentNode().getAttributes().getNamedItem("name").getNodeValue());
70                                 msg.append("\n");
71                                 msg.append("\t");
72                                 msg.append("\n");
73                         }
74
75                 }
76
77                 if (foundIssue) {
78                         System.out.println(msg.toString());
79                         fail("Node type xml-property should have space.");
80                 }
81
82         }
83
84         /**
85          * Verifies that all of the node types in the oxm's have their uri templates.
86          * @throws XPathExpressionException
87          * @throws IOException
88          * @throws SAXException
89          * @throws ParserConfigurationException
90          */
91         @Test
92         public void allNodeTypesHaveAAIUriTemplate() throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
93                 boolean foundIssue = false;
94                 List<File> fileList = getFiles();
95
96                 StringBuilder msg = new StringBuilder();
97                 for (File file : fileList) {
98                         msg.append(file.getAbsolutePath().replaceAll(".*aai-schema", ""));
99                         msg.append("\n");
100                         Document xmlDocument = getDocument(file);
101                         XPath xPath = XPathFactory.newInstance().newXPath();
102                         String expression = "/xml-bindings/java-types/java-type[" +
103                                         "(" +
104                                                 "count(xml-properties/xml-property[@name='container']) > 0 " +
105                                                 "or count(xml-properties/xml-property[@name='dependentOn']) > 0" +
106                                         ") " +
107                                         "and count(xml-properties/xml-property[@name='uriTemplate']) = 0 " +
108                                         "]";
109                         NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
110
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")) {
114                                         continue;
115                                 }
116                                 foundIssue = true;
117                                 msg.append("\t");
118                                 msg.append(name);
119                                 msg.append("\n");
120                         }
121                 }
122                 if (foundIssue) {
123                         System.out.println(msg.toString());
124                         fail("Missing uriTemplate in oxm.");
125                 }
126
127         }
128
129
130     /**
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
137      */
138     @Test
139     public void aliasedIndexedPropsAreInIndexedListWithPropName() throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
140
141         final List<String> props = Arrays.asList("model-invariant-id","model-version-id");
142
143         boolean foundIssue = false;
144         List<File> fileList = getLatestFiles();
145         StringBuilder msg = new StringBuilder();
146
147         for (File file : fileList) {
148             msg.append(file.getAbsolutePath().replaceAll(".*aai-schema", ""));
149             msg.append("\n");
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[" +
154                     "(" +
155                     "count(xml-properties/xml-property[@name='container']) > 0 " +
156                     "or count(xml-properties/xml-property[@name='dependentOn']) > 0" +
157                     ") " +
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
160                     "]";
161
162                 NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
163
164                 if (nodeList.getLength() > 0) {
165                     msg.append("\t")
166                         .append(prop)
167                         .append("\n");
168                 }
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")) {
172                         continue;
173                     }
174                     foundIssue = true;
175                     msg.append("\t\t")
176                         .append(name)
177                         .append("\n");
178                 }
179             }
180         }
181
182         if (foundIssue) {
183             System.out.println(msg.toString());
184             fail("Missing index entry in oxm.");
185         }
186
187     }
188
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)
195                                 .stream()
196                                 .filter(file -> file.getAbsolutePath().contains("oxm"))
197                                 .filter(file -> !file.getAbsolutePath().contains("onap")) // skips onap for checks
198                                 .collect(Collectors.toList());
199         }
200
201     /**
202      * Finds all of the oxm files for the latest version.
203      * @return list of the latest version of the oxm files.
204      */
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);
215                 }
216                 int extractInt(String s) {
217                     String num = s.replaceAll("\\D", "");
218                     return num.isEmpty() ? 0 : Integer.parseInt(num);
219                 }
220             }).ifPresent(latest::add);
221         }
222
223         List<File> latestFiles = new ArrayList<>();
224         latest.forEach(s ->
225             FileUtils.listFiles(
226                 new File(s),
227                 new RegexFileFilter(".*\\.xml"),
228                 DirectoryFileFilter.DIRECTORY)
229                 .stream()
230                 .filter(file -> file.getAbsolutePath().contains("oxm"))
231                 .forEach(latestFiles::add));
232
233         return latestFiles;
234     }
235
236         //TODO test that all oxm xml are valid xml
237
238
239
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");
244                 }
245                 return stringBuilder.toString();
246         }
247
248         public String printNode(Node node, Document document) throws IOException {
249                 StringWriter stringWriter = new StringWriter();
250                 return stringWriter.toString();
251
252         }
253
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);
259         }
260
261 }