/** * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ package org.onap.aai.schemagen.genxsd; import static org.hamcrest.CoreMatchers.both; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.collection.IsIn.in; import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; import static org.hamcrest.core.Every.everyItem; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Vector; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.commons.lang.StringUtils; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class XSDElementTest { private static final Logger logger = LoggerFactory.getLogger("XSDElementTest.class"); private static final int maxSizeForXml = 20000; protected String testXML; protected Document doc = null; protected NodeList javaTypeNodes = null; public String getTestXML() { return testXML; } @BeforeClass public static void setUpBeforeClass() throws Exception { } @Before public void setUp() throws Exception { setUp(0); } public void setUp(int sbopt) throws Exception { StringBuilder sb = new StringBuilder(maxSizeForXml); addNamespace(sb); addBusiness(sb); addCustomers(sb); if (sbopt == 0) { addCustomer(sb); } else { addCustomerNoSubscriberType(sb); addCustomerSubscriberType(sb); } addServiceSubscriptions(sb); addServiceSubscription(sb); addEndOfXML(sb); testXML = sb.toString(); init(); } public void setUpRelationship() throws Exception { StringBuilder sb = new StringBuilder(maxSizeForXml); addNamespaceNoInventory(sb); addRelationship(sb); addRelationshipList(sb); addRelatedToProperty(sb); addRelationshipData(sb); addEndOfXML(sb); testXML = sb.toString(); init(); } private void addNamespace(StringBuilder sb) { sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); } private void addNamespaceNoInventory(StringBuilder sb) { sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); } private void addBusiness(StringBuilder sb) { sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); } private void addCustomers(StringBuilder sb) { sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); } private void addCustomer(StringBuilder sb) { sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); // sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); } private void addCustomerNoSubscriberType(StringBuilder sb) { sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); } private void addCustomerSubscriberType(StringBuilder sb) { sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); } private void addServiceSubscriptions(StringBuilder sb) { sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); } private void addServiceSubscription(StringBuilder sb) { sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); // sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); } private void addRelationshipList(StringBuilder sb) { sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "/n"); sb.append("\n"); sb.append("\n"); } private void addRelationship(StringBuilder sb) { sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append(""); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); } private void addRelatedToProperty(StringBuilder sb) { sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); } private void addRelationshipData(StringBuilder sb) { sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append( "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); } private void addEndOfXML(StringBuilder sb) { sb.append("\n"); sb.append("\n"); } public void init() throws ParserConfigurationException, SAXException, IOException { DocumentBuilder dBuilder = null; try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); dBuilder = dbFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw e; } try { InputSource isInput = new InputSource(new StringReader(testXML)); doc = dBuilder.parse(isInput); } catch (SAXException e) { throw e; } catch (IOException e) { throw e; } NodeList bindingsNodes = doc.getElementsByTagName("xml-bindings"); Element bindingElement; NodeList javaTypesNodes; Element javaTypesElement; if (bindingsNodes == null || bindingsNodes.getLength() == 0) { throw new SAXException("OXM file error: missing in XML"); } bindingElement = (Element) bindingsNodes.item(0); javaTypesNodes = bindingElement.getElementsByTagName("java-types"); if (javaTypesNodes.getLength() < 1) { throw new SAXException("OXM file error: missing in XML"); } javaTypesElement = (Element) javaTypesNodes.item(0); javaTypeNodes = javaTypesElement.getElementsByTagName("java-type"); if (javaTypeNodes.getLength() < 1) { throw new SAXException( "OXM file error: missing in XML"); } logger.debug(testXML); } @Test public void testXSDElement() { // repeat of testGetIndexedProps() which uses the constructor ArrayList target = new ArrayList(); target.add("subscriber-name"); target.add("global-customer-id"); target.add("subscriber-type"); target.add("service-type"); Vector indexedProps = new Vector(); for (int i = 0; i < javaTypeNodes.getLength(); ++i) { XSDElement javaTypeElement = new XSDElement((Element) javaTypeNodes.item(i)); indexedProps.addAll(javaTypeElement.getIndexedProps()); } assertThat(new ArrayList<>(indexedProps), both(everyItem(is(in(target.toArray())))).and(containsInAnyOrder(target.toArray()))); } @Test public void testName() { ArrayList target = new ArrayList(); target.add("ServiceSubscriptions"); target.add("ServiceSubscription"); target.add("Inventory"); target.add("Business"); target.add("Customers"); target.add("Customer"); ArrayList names = new ArrayList(); for (int i = 0; i < javaTypeNodes.getLength(); ++i) { XSDElement javaTypeElement = new XSDElement((Element) javaTypeNodes.item(i)); names.add(javaTypeElement.name()); } logger.debug(String.join("|", names)); assertThat(names, both(everyItem(is(in(target.toArray())))).and(containsInAnyOrder(target.toArray()))); } @Test public void testGetAddTypes() { HashMap> map = new HashMap>(); HashMap> target = new HashMap>(); target.put("Customer", new ArrayList<>(Arrays.asList("ServiceSubscriptions", "RelationshipList"))); target.put("Customer", new ArrayList<>(Arrays.asList("ServiceSubscriptions"))); target.put("Business", new ArrayList<>(Arrays.asList("Customers"))); target.put("Inventory", new ArrayList<>(Arrays.asList("Business"))); target.put("Customers", new ArrayList<>(Arrays.asList("Customer"))); target.put("ServiceSubscription", new ArrayList<>(Arrays.asList("RelationshipList"))); target.put("ServiceSubscription", new ArrayList<>(Arrays.asList())); target.put("ServiceSubscriptions", new ArrayList<>(Arrays.asList("ServiceSubscription"))); for (int i = 0; i < javaTypeNodes.getLength(); ++i) { XSDElement javaTypeElement = new XSDElement((Element) javaTypeNodes.item(i)); ArrayList addTypes = new ArrayList(); NodeList xmlElementNodes = javaTypeElement.getElementsByTagName("xml-element"); String name = javaTypeElement.name(); for (int j = 0; j < xmlElementNodes.getLength(); ++j) { XSDElement xmlElement = new XSDElement((Element) xmlElementNodes.item(j)); addTypes.addAll(xmlElement.getAddTypes("v11")); map.put(name, addTypes); } } for (String key : map.keySet()) { assertThat("Expected for key:" + key, map.get(key), equalTo(target.get(key))); } } /* * @Test * public void testGetRequiredElements() { * HashMap> map = new HashMap>(); * ArrayList target = new ArrayList(); * target.add("global-customer-id\n"); * target.add("subscriber-name\n"); * target.add("subscriber-type"); * for ( int i = 0; i < javaTypeNodes.getLength(); ++i ) { * XSDElement javaTypeElement = new XSDElement((Element) javaTypeNodes.item(i)); * ArrayList requiredItems = new ArrayList(); * String name=javaTypeElement.name(); * requiredItems.addAll(javaTypeElement.getRequiredElements("v11")); * map.put(name,requiredItems); * } * for(String key : map.keySet()) { * assertThat(map.get(key),equalTo(target)); * } * } */ @Test public void testGetPathDescriptionProperty() { ArrayList target = new ArrayList(); target.add("Namespace for business related constructs"); target .add("Collection of customer identifiers to provide linkage back to BSS information."); target.add("customer identifiers to provide linkage back to BSS information."); target.add("Collection of objects that group service instances."); target.add("Object that group service instances."); List descs = new ArrayList(); for (int i = 0; i < javaTypeNodes.getLength(); ++i) { XSDElement javaTypeElement = new XSDElement((Element) javaTypeNodes.item(i)); if (javaTypeElement.getPathDescriptionProperty() != null) descs.add(javaTypeElement.getPathDescriptionProperty()); } logger.debug(String.join("|", descs)); assertThat(new ArrayList<>(descs), both(everyItem(is(in(target.toArray())))).and(containsInAnyOrder(target.toArray()))); } @Test public void testGetIndexedProps() { ArrayList target = new ArrayList(); target.add("subscriber-name"); target.add("global-customer-id"); target.add("subscriber-type"); target.add("service-type"); Vector indexedProps = new Vector(); for (int i = 0; i < javaTypeNodes.getLength(); ++i) { XSDElement javaTypeElement = new XSDElement((Element) javaTypeNodes.item(i)); indexedProps.addAll(javaTypeElement.getIndexedProps()); } assertThat(new ArrayList<>(indexedProps), both(everyItem(is(in(target.toArray())))).and(containsInAnyOrder(target.toArray()))); } @Test public void testGetContainerProperty() { ArrayList target = new ArrayList(); target.add("service-subscriptions"); target.add("customers"); List containers = new ArrayList(); for (int i = 0; i < javaTypeNodes.getLength(); ++i) { XSDElement javaTypeElement = new XSDElement((Element) javaTypeNodes.item(i)); if (javaTypeElement.getContainerProperty() != null) containers.add(javaTypeElement.getContainerProperty()); } logger.debug(String.join("|", containers)); assertThat(new ArrayList<>(containers), both(everyItem(is(in(target.toArray())))).and(containsInAnyOrder(target.toArray()))); } @Test public void testGetQueryParamYAML() { ArrayList target = new ArrayList(); target.add( " - name: global-customer-id\n in: query\n required: false\n type: string\n"); target.add( " - name: subscriber-name\n in: query\n required: false\n type: string\n"); target.add( " - name: subscriber-type\n in: query\n required: false\n type: string\n"); Vector indexedProps = new Vector(); for (int i = 0; i < javaTypeNodes.getLength(); ++i) { XSDElement javaTypeElement = new XSDElement((Element) javaTypeNodes.item(i)); if (javaTypeElement.getContainerProperty() != null) { indexedProps.addAll(javaTypeElement.getIndexedProps()); String container = javaTypeElement.getContainerProperty(); Vector containerProps = new Vector(); NodeList xmlElementNodes = javaTypeElement.getElementsByTagName("xml-element"); for (int j = 0; j < xmlElementNodes.getLength(); ++j) { XSDElement xmlElement = new XSDElement((Element) xmlElementNodes.item(j)); if (indexedProps.contains(xmlElement.name())) containerProps.add(xmlElement.getQueryParamYAML()); } GetOperation.addContainerProps(container, containerProps); } } /* * List queryParams = new ArrayList(); * for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) { * XSDElement javaTypeElement = new XSDElement((Element) javaTypeNodes.item(i)); * if(javaTypeElement.getQueryParamYAML() != null) * queryParams.add(javaTypeElement.getQueryParamYAML()); * } */ assertThat(GetOperation.containers.get("customers"), equalTo(target)); } @Test public void testGetPathParamYAML() { ArrayList target = new ArrayList(); target.add( " - name: Inventory\n in: path\n description: Inventory\n required: true\n"); target.add( " - name: Business\n in: path\n description: Business\n required: true\n"); target.add( " - name: Customers\n in: path\n description: Customers\n required: true\n"); target.add( " - name: Customer\n in: path\n description: Customer\n required: true\n"); target.add( " - name: ServiceSubscriptions\n in: path\n description: ServiceSubscriptions\n required: true\n"); target.add( " - name: ServiceSubscription\n in: path\n description: ServiceSubscription\n required: true\n"); List pathParams = new ArrayList(); for (int i = 0; i < javaTypeNodes.getLength(); ++i) { XSDElement javaTypeElement = new XSDElement((Element) javaTypeNodes.item(i)); if (javaTypeElement.getPathParamYAML(javaTypeElement.name()) != null) pathParams.add(javaTypeElement.getPathParamYAML(javaTypeElement.name())); } logger.debug(String.join("|", pathParams)); assertThat(new ArrayList<>(pathParams), both(everyItem(is(in(target.toArray())))).and(containsInAnyOrder(target.toArray()))); } @Test public void testGetHTMLAnnotation() { ArrayList target = new ArrayList(); target.add(" " + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR + " @org.onap.aai.annotations.Metadata(description=\"Namespace for business related constructs\")" + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR); target.add(" " + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR + " @org.onap.aai.annotations.Metadata(description=\"Collection of customer identifiers to provide linkage back to BSS information.\")" + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR); target.add(" " + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR + " @org.onap.aai.annotations.Metadata(description=\"customer identifiers to provide linkage back to BSS information.\",nameProps=\"subscriber-name\",indexedProps=\"subscriber-name,global-customer-id,subscriber-type\",searchable=\"global-customer-id,subscriber-name\",uniqueProps=\"global-customer-id\",container=\"customers\",namespace=\"business\")" + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR); target.add(" " + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR + " @org.onap.aai.annotations.Metadata(description=\"Collection of objects that group service instances.\")" + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR); target.add(" " + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR + " @org.onap.aai.annotations.Metadata(description=\"Object that group service instances.\",indexedProps=\"service-type\",dependentOn=\"customer\",container=\"service-subscriptions\",crossEntityReference=\"service-instance,service-type\")" + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR + " " + OxmFileProcessor.LINE_SEPARATOR); List annotes = new ArrayList(); for (int i = 0; i < javaTypeNodes.getLength(); ++i) { XSDElement javaTypeElement = new XSDElement((Element) javaTypeNodes.item(i)); if (StringUtils .isNotEmpty(javaTypeElement.getHTMLAnnotation(javaTypeElement.name(), ""))) annotes.add(javaTypeElement.getHTMLAnnotation(javaTypeElement.name(), " ")); } logger.debug("result:"); logger.debug(String.join("|", annotes)); logger.debug("Expected:"); logger.debug(String.join("|", target)); assertThat(new ArrayList<>(annotes), both(everyItem(is(in(target.toArray())))).and(containsInAnyOrder(target.toArray()))); } @Test public void testGetTypePropertyYAML() { ArrayList target = new ArrayList(); target.add(" Inventory:\n type: "); target.add( " Business:\n type: description: Namespace for business related constructs\n"); target.add( " Customers:\n type: description: Collection of customer identifiers to provide linkage back to BSS information.\n"); target.add( " Customer:\n type: description: customer identifiers to provide linkage back to BSS information.\n"); target.add( " ServiceSubscriptions:\n type: description: Collection of objects that group service instances.\n"); target.add( " ServiceSubscription:\n type: description: Object that group service instances.\n"); StringBuilder sb = new StringBuilder( " Customer:\n type: description: |\n customer identifiers to provide linkage back to BSS information.\n"); sb.append( " *This property can be used as a filter to find the start node for a dsl query\n"); String yamlDesc = sb.toString(); List types = new ArrayList(); String container; String customerDesc = null; for (int i = 0; i < javaTypeNodes.getLength(); ++i) { XSDElement javaTypeElement = new XSDElement((Element) javaTypeNodes.item(i)); if (javaTypeElement.getTypePropertyYAML(false) != null) types.add(javaTypeElement.getTypePropertyYAML(false)); container = javaTypeElement.getContainerProperty(); if ("customers".equals(container)) { customerDesc = javaTypeElement.getTypePropertyYAML(true); } } assertThat(new ArrayList<>(types), both(everyItem(is(in(target.toArray())))).and(containsInAnyOrder(target.toArray()))); assertEquals(customerDesc, yamlDesc); } @Test public void testIsStandardType() { HashMap> map = new HashMap>(); HashMap> target = new HashMap>(); target.put("Customer", new ArrayList<>(Arrays.asList("global-customer-id", "subscriber-name", "subscriber-type", "resource-version"))); target.put("Business", new ArrayList<>()); target.put("Inventory", new ArrayList<>()); target.put("Customers", new ArrayList<>()); target.put("ServiceSubscriptions", new ArrayList<>()); target.put("ServiceSubscription", new ArrayList<>( Arrays.asList("service-type", "temp-ub-sub-account-id", "resource-version"))); for (int i = 0; i < javaTypeNodes.getLength(); ++i) { XSDElement javaTypeElement = new XSDElement((Element) javaTypeNodes.item(i)); ArrayList addTypes = new ArrayList(); NodeList xmlElementNodes = javaTypeElement.getElementsByTagName("xml-element"); String name = javaTypeElement.name(); for (int j = 0; j < xmlElementNodes.getLength(); ++j) { XSDElement xmlElement = new XSDElement((Element) xmlElementNodes.item(j)); if (xmlElement.isStandardType()) addTypes.add(xmlElement.name()); } map.put(name, addTypes); } for (String key : map.keySet()) { assertThat(map.get(key), equalTo(target.get(key))); } } }