X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fyang%2Fyang-data-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fyang%2Fdata%2Fimpl%2FNodeHelper.java;h=a7e7d2f74cbddf91807a3fddcf400e4b0235298e;hp=3b7c0d0fee15a76d50bcf0222635dd083328bc27;hb=refs%2Fchanges%2F93%2F593%2F5;hpb=9d0e5ef647eb242f7750e30482b403676f56c682 diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeHelper.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeHelper.java old mode 100755 new mode 100644 index 3b7c0d0fee..a7e7d2f74c --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeHelper.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeHelper.java @@ -7,9 +7,20 @@ */ package org.opendaylight.controller.yang.data.impl; +import groovy.lang.Binding; +import groovy.lang.GroovyShell; +import groovy.lang.Script; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.PrintStream; +import java.io.Reader; import java.io.StringWriter; +import java.lang.reflect.Method; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -20,6 +31,8 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import org.custommonkey.xmlunit.Diff; +import org.junit.Assert; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; import org.opendaylight.controller.yang.data.api.Node; @@ -27,7 +40,10 @@ import org.opendaylight.controller.yang.model.api.Module; import org.opendaylight.controller.yang.model.api.SchemaContext; import org.opendaylight.controller.yang.model.parser.api.YangModelParser; import org.opendaylight.controller.yang.parser.impl.YangParserImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; +import org.xml.sax.SAXException; /** * @author michal.rehak @@ -35,6 +51,8 @@ import org.w3c.dom.Document; */ public abstract class NodeHelper { + private static final Logger LOG = LoggerFactory.getLogger(NodeHelper.class); + /** xml source of example network configuration */ public static final String NETWORK_XML = "\n" + @@ -127,145 +145,145 @@ public abstract class NodeHelper { String xmlString = result.getWriter().toString(); out.println(xmlString); } - + /** * @param qName * @return example tree, see {@link #NETWORK_XML} */ public static CompositeNode buildTestConfigTree(QName qName) { List> value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "element-id"), null, "ntElementId_09")); - CompositeNode ntElementNode1 = NodeFactory.createCompositeNode(new QName(qName, "network-element"), null, value); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "element-id"), null, "ntElementId_09")); + CompositeNode ntElementNode1 = NodeFactory.createImmutableCompositeNode(new QName(qName, "network-element"), null, value); assignParentToChildren(ntElementNode1); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "element-id"), null, "ntElementId_10")); - CompositeNode ntElementNode2 = NodeFactory.createCompositeNode(new QName(qName, "network-element"), null, value); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "element-id"), null, "ntElementId_10")); + CompositeNode ntElementNode2 = NodeFactory.createImmutableCompositeNode(new QName(qName, "network-element"), null, value); assignParentToChildren(ntElementNode2); value = new ArrayList>(); value.add(ntElementNode1); value.add(ntElementNode2); - CompositeNode ntElementsNode = NodeFactory.createCompositeNode( + CompositeNode ntElementsNode = NodeFactory.createImmutableCompositeNode( new QName(qName, "network-elements"), null, value); assignParentToChildren(ntElementsNode); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "dest-node"), null, "nodeId_07")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "dest-tp"), null, "tpId_08")); - CompositeNode destination = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "dest-node"), null, "nodeId_07")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "dest-tp"), null, "tpId_08")); + CompositeNode destination = NodeFactory.createImmutableCompositeNode( new QName(qName, "destination"), null, value); assignParentToChildren(destination); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "source-node"), null, "nodeId_05")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "source-tp"), null, "tpId_06")); - CompositeNode source = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "source-node"), null, "nodeId_05")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "source-tp"), null, "tpId_06")); + CompositeNode source = NodeFactory.createImmutableCompositeNode( new QName(qName, "source"), null, value); assignParentToChildren(source); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "link-id"), null, "linkId_04")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "link-id"), null, "linkId_04")); value.add(source); value.add(destination); - CompositeNode link1 = NodeFactory.createCompositeNode( + CompositeNode link1 = NodeFactory.createImmutableCompositeNode( new QName(qName, "link"), null, value); assignParentToChildren(link1); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "dest-node"), null, "nodeId_14")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "dest-tp"), null, "tpId_15")); - destination = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "dest-node"), null, "nodeId_14")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "dest-tp"), null, "tpId_15")); + destination = NodeFactory.createImmutableCompositeNode( new QName(qName, "destination"), null, value); assignParentToChildren(destination); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "source-node"), null, "nodeId_12")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "source-tp"), null, "tpId_13")); - source = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "source-node"), null, "nodeId_12")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "source-tp"), null, "tpId_13")); + source = NodeFactory.createImmutableCompositeNode( new QName(qName, "source"), null, value); assignParentToChildren(source); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "link-id"), null, "linkId_11")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "link-id"), null, "linkId_11")); value.add(source); value.add(destination); - CompositeNode link2 = NodeFactory.createCompositeNode( + CompositeNode link2 = NodeFactory.createImmutableCompositeNode( new QName(qName, "link"), null, value); assignParentToChildren(link2); value = new ArrayList>(); value.add(link1); value.add(link2); - CompositeNode links = NodeFactory.createCompositeNode( + CompositeNode links = NodeFactory.createImmutableCompositeNode( new QName(qName, "links"), null, value); assignParentToChildren(links); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "tp-id"), null, "tpId_03")); - CompositeNode terminationPointNode1 = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "tp-id"), null, "tpId_03")); + CompositeNode terminationPointNode1 = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-point"), null, value); assignParentToChildren(terminationPointNode1); value = new ArrayList>(); value.add(terminationPointNode1); - CompositeNode terminationPointsNode = NodeFactory.createCompositeNode( + CompositeNode terminationPointsNode = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-points"), null, value); assignParentToChildren(terminationPointsNode); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "node-id"), null, "nodeId_02")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "supporting-ne"), null, "networkId_02")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "node-id"), null, "nodeId_02")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "supporting-ne"), null, "networkId_02")); value.add(terminationPointsNode); - CompositeNode node1Node = NodeFactory.createCompositeNode( + CompositeNode node1Node = NodeFactory.createImmutableCompositeNode( new QName(qName, "node"), null, value); assignParentToChildren(node1Node); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "tp-id"), null, "tpId_18")); - terminationPointNode1 = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "tp-id"), null, "tpId_18")); + terminationPointNode1 = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-point"), null, value); assignParentToChildren(terminationPointNode1); value = new ArrayList>(); value.add(terminationPointNode1); - terminationPointsNode = NodeFactory.createCompositeNode( + terminationPointsNode = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-points"), null, value); assignParentToChildren(terminationPointsNode); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "node-id"), null, "nodeId_16")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "supporting-ne"), null, "networkId_17")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "node-id"), null, "nodeId_16")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "supporting-ne"), null, "networkId_17")); value.add(terminationPointsNode); - CompositeNode node2Node = NodeFactory.createCompositeNode( + CompositeNode node2Node = NodeFactory.createImmutableCompositeNode( new QName(qName, "node"), null, value); assignParentToChildren(node2Node); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "tp-id"), null, "tpId_18")); - terminationPointNode1 = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "tp-id"), null, "tpId_18")); + terminationPointNode1 = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-point"), null, value); assignParentToChildren(terminationPointNode1); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "tp-id"), null, "tpId_19")); - CompositeNode terminationPointNode2 = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "tp-id"), null, "tpId_19")); + CompositeNode terminationPointNode2 = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-point"), null, value); assignParentToChildren(terminationPointNode2); value = new ArrayList>(); value.add(terminationPointNode1); value.add(terminationPointNode2); - terminationPointsNode = NodeFactory.createCompositeNode( + terminationPointsNode = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-points"), null, value); assignParentToChildren(terminationPointsNode); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "node-id"), null, "nodeId_19")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "supporting-ne"), null, "networkId_20")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "node-id"), null, "nodeId_19")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "supporting-ne"), null, "networkId_20")); value.add(terminationPointsNode); - CompositeNode node3Node = NodeFactory.createCompositeNode( + CompositeNode node3Node = NodeFactory.createImmutableCompositeNode( new QName(qName, "node"), null, value); assignParentToChildren(node3Node); @@ -273,27 +291,28 @@ public abstract class NodeHelper { value.add(node1Node); value.add(node2Node); value.add(node3Node); - CompositeNode nodesNode = NodeFactory.createCompositeNode( + CompositeNode nodesNode = NodeFactory.createImmutableCompositeNode( new QName(qName, "nodes"), null, value); assignParentToChildren(nodesNode); value = new ArrayList>(); value.add(links); value.add(nodesNode); - value.add(NodeFactory.createSimpleNode(new QName(qName, "topology-id"), null, "topId_01")); - CompositeNode topology = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "topology-id"), null, "topId_01")); + CompositeNode topology = NodeFactory.createImmutableCompositeNode( new QName(qName, "topology"), null, value); assignParentToChildren(topology); value = new ArrayList>(); value.add(topology); - CompositeNode topologies = NodeFactory.createCompositeNode( + CompositeNode topologies = NodeFactory.createImmutableCompositeNode( new QName(qName, "topologies"), null, value); assignParentToChildren(topologies); value = new ArrayList>(); value.add(topologies); - CompositeNode network = NodeFactory.createCompositeNode( + value.add(ntElementsNode); + CompositeNode network = NodeFactory.createImmutableCompositeNode( new QName(qName, "network"), null, value); assignParentToChildren(network); @@ -323,5 +342,70 @@ public abstract class NodeHelper { .parseYangModelsFromStreams(yangInputStreams); return yParser.resolveSchemaContext(modules); } + + /** + * @param scriptName + * @return tree root + * @throws Exception + */ + public static CompositeNode loadConfigByGroovy(String scriptName) throws Exception { + InputStream configStream = NodeHelper.class.getResourceAsStream(scriptName); + Binding binding = new Binding(); + GroovyShell gShell = new GroovyShell(binding); + LOG.debug("groovy: starting script parse.. " + scriptName); + Script configScript = gShell.parse(new InputStreamReader(configStream)); + LOG.debug("groovy: starting script.. " + scriptName); + configScript.run(); + LOG.debug("groovy: digging result"); + Object xmlGen = binding.getVariable("xmlGen"); + LOG.debug("xmlGen = " + xmlGen); + Method getter = xmlGen.getClass().getDeclaredMethod("getBuilder", new Class[0]); + MyNodeBuilder builder = (MyNodeBuilder) getter.invoke(xmlGen, new Object[0]); + + return builder.getRootNode(); + } + + /** + * @param pattern , e.g.:
"//{0}:network/{1}:xx[text() = 'sss']"
+ * @param nsArg , e.g.:
{"uri:ns1", "uri:ns2"}
+ * @return pattern with namespaces:
//uri:ns1:network/uri:ns2:xx[text() = ''sss'']"
+ */ + public static String AddNamespaceToPattern(String pattern, Object... nsArg) { + Object[] ns = nsArg; + String patternNs = pattern.replaceAll("'", "''"); + if (ns == null) { + ns = new Object[]{""}; + } else { + // add ':' into pattern after placeholders + patternNs = patternNs.replaceAll("(\\{[0-9]+\\})", "$1:"); + } + + return MessageFormat.format(patternNs, ns); + } + + /** + * @param tree + * @param xmlFile + * @param clazz + * @throws Exception + * @throws SAXException + * @throws IOException + */ + public static void compareXmlTree(Document tree, String xmlFile, Class clazz) throws Exception, + SAXException, IOException { + ByteArrayOutputStream actualRaw = new ByteArrayOutputStream(); + dumpDoc(tree, new PrintStream(actualRaw)); + Reader actualReader = new InputStreamReader(new ByteArrayInputStream(actualRaw.toByteArray())); + + Reader expectedReader = new InputStreamReader(clazz.getResourceAsStream(xmlFile)); + Diff myDiff = new Diff(expectedReader, actualReader); + myDiff.overrideDifferenceListener(new IgnoreWhiteCharsDiffListener()); + + boolean similar = myDiff.similar(); + if (! similar) { + System.out.println(new String(actualRaw.toByteArray())); + } + Assert.assertEquals(myDiff.toString(), true, similar); + } }