From: Igor Foltin Date: Tue, 31 May 2016 10:53:59 +0000 (+0200) Subject: Bug 1441: Bug fixes, clean-up and test migration X-Git-Tag: release/boron~88 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=7d464a013773ab1dcb196191010e99833c22cf2d;p=yangtools.git Bug 1441: Bug fixes, clean-up and test migration - fix of yang-modeled anyxml deserialization - fix of anyxml deserialization - fix of deserialization of identityref and instance-identifier leaves - code clean-up of XmlParserStream and related classes - migration of unit tests to new xml parser - copied new xml serializer to yang-data-codec-xml module Change-Id: Id214b78849998cf54e087685dcc78e3ded74ab69 Signed-off-by: Igor Foltin --- diff --git a/yang/yang-data-codec-gson/pom.xml b/yang/yang-data-codec-gson/pom.xml index 6a74e61fe8..587d9cfb77 100644 --- a/yang/yang-data-codec-gson/pom.xml +++ b/yang/yang-data-codec-gson/pom.xml @@ -37,6 +37,11 @@ org.opendaylight.yangtools yang-data-util + + org.opendaylight.yangtools + yang-data-codec-xml + test + org.opendaylight.yangtools yang-parser-impl diff --git a/yang/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/YangModeledAnyXmlSupportTest.java b/yang/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/YangModeledAnyXmlSupportTest.java index 9033049a2d..fc93965366 100644 --- a/yang/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/YangModeledAnyXmlSupportTest.java +++ b/yang/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/YangModeledAnyXmlSupportTest.java @@ -8,9 +8,11 @@ package org.opendaylight.yangtools.yang.data.codec.gson; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules; import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile; -import com.google.common.base.Preconditions; + import com.google.gson.JsonElement; import com.google.gson.JsonParser; import com.google.gson.stream.JsonReader; @@ -22,11 +24,10 @@ import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; import java.net.URISyntaxException; -import java.util.Collections; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; @@ -35,43 +36,40 @@ import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter; +import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult; -import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.DomUtils; -import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; -import org.w3c.dom.Document; import org.xml.sax.SAXException; public class YangModeledAnyXmlSupportTest { - private static final XMLOutputFactory XML_FACTORY; - private static final DocumentBuilderFactory BUILDERFACTORY; - - static { - XML_FACTORY = XMLOutputFactory.newFactory(); - XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false); - - BUILDERFACTORY = DocumentBuilderFactory.newInstance(); - BUILDERFACTORY.setNamespaceAware(true); - BUILDERFACTORY.setCoalescing(true); - BUILDERFACTORY.setIgnoringElementContentWhitespace(true); - BUILDERFACTORY.setIgnoringComments(true); - } - private static SchemaContext schemaContext; - private static Document xmlDoc; private static ContainerNode data; @BeforeClass - public static void init() throws IOException, URISyntaxException, ReactorException, SAXException { + public static void init() throws IOException, URISyntaxException, ReactorException, SAXException, + XMLStreamException, ParserConfigurationException { schemaContext = loadModules("/yang-modeled-anyxml/yang"); - xmlDoc = loadDocument("/yang-modeled-anyxml/xml/baz.xml"); - data = DomToNormalizedNodeParserFactory - .getInstance(DomUtils.defaultValueCodecProvider(), schemaContext).getContainerNodeParser() - .parse(Collections.singletonList(xmlDoc.getDocumentElement()), schemaContext); + + final InputStream resourceAsStream = YangModeledAnyXmlSupportTest.class.getResourceAsStream( + "/yang-modeled-anyxml/xml/baz.xml"); + + final XMLInputFactory factory = XMLInputFactory.newInstance(); + final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream); + + final NormalizedNodeResult result = new NormalizedNodeResult(); + + final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result); + + final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext); + xmlParser.parse(reader); + + assertNotNull(result.getResult()); + assertTrue(result.getResult() instanceof ContainerNode); + data = (ContainerNode) result.getResult(); } @Test @@ -83,12 +81,12 @@ public class YangModeledAnyXmlSupportTest { jsonParser.parse(new JsonReader(new StringReader(inputJson))); final NormalizedNode transformedInput = result.getResult(); - assertEquals(data.getValue().iterator().next(), transformedInput); + assertEquals(data, transformedInput); } @Test public void normalizedNodesToJsonTest() throws IOException, URISyntaxException, SAXException { - final DataContainerChild baz = data.getValue().iterator().next(); + final DataContainerChild baz = data; final Writer writer = new StringWriter(); final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, baz); @@ -113,26 +111,4 @@ public class YangModeledAnyXmlSupportTest { nodeWriter.close(); return writer.toString(); } - - private static Document loadDocument(final String xmlPath) throws IOException, SAXException { - final InputStream resourceAsStream = YangModeledAnyXmlSupportTest.class.getResourceAsStream(xmlPath); - - final Document currentConfigElement = readXmlToDocument(resourceAsStream); - Preconditions.checkNotNull(currentConfigElement); - return currentConfigElement; - } - - private static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException { - final DocumentBuilder dBuilder; - try { - dBuilder = BUILDERFACTORY.newDocumentBuilder(); - } catch (final ParserConfigurationException e) { - throw new RuntimeException("Failed to parse XML document", e); - } - final Document doc = dBuilder.parse(xmlContent); - - doc.getDocumentElement().normalize(); - return doc; - } - } \ No newline at end of file diff --git a/yang/yang-data-codec-xml/pom.xml b/yang/yang-data-codec-xml/pom.xml index 750e497f7a..55a393099b 100644 --- a/yang/yang-data-codec-xml/pom.xml +++ b/yang/yang-data-codec-xml/pom.xml @@ -43,6 +43,11 @@ junit test + + xmlunit + xmlunit + test + + + 3 + + augment + + 1 + 1 + 1 + + + + diff --git a/yang/yang-data-codec-xml/src/test/resources/schema/augment_choice_hell_ok2.xml b/yang/yang-data-codec-xml/src/test/resources/schema/augment_choice_hell_ok2.xml new file mode 100644 index 0000000000..fc510077fc --- /dev/null +++ b/yang/yang-data-codec-xml/src/test/resources/schema/augment_choice_hell_ok2.xml @@ -0,0 +1,14 @@ + + + 2 + + 3 + + augment + + 1 + 1 + 1 + + + diff --git a/yang/yang-data-codec-xml/src/test/resources/schema/augment_choice_hell_ok3.xml b/yang/yang-data-codec-xml/src/test/resources/schema/augment_choice_hell_ok3.xml new file mode 100644 index 0000000000..e367c455f9 --- /dev/null +++ b/yang/yang-data-codec-xml/src/test/resources/schema/augment_choice_hell_ok3.xml @@ -0,0 +1,8 @@ + + + + leaf-value + + + + diff --git a/yang/yang-data-codec-xml/src/test/resources/schema/rpc-test-model.yang b/yang/yang-data-codec-xml/src/test/resources/schema/rpc-test-model.yang new file mode 100644 index 0000000000..652b6a0d71 --- /dev/null +++ b/yang/yang-data-codec-xml/src/test/resources/schema/rpc-test-model.yang @@ -0,0 +1,44 @@ +module rpc-test-model { +yang-version 1; + namespace "org:opendaylight:rpc-reply:test:ns:yang"; + prefix "user"; + + organization "Cisco Systems"; + contact "Lukas Sedlak"; + description "Test model for testing rpc input message translation to DOM Nodes."; + + revision "2014-07-17" { + description "Initial revision"; + } + + rpc rock-the-house { + output { + leaf zip-code { + type string; + } + } + } + + rpc activate-software-image { + input { + leaf image-name { + type string; + } + } + output { + container image-properties { + list image-property { + key "image-id"; + + leaf image-id { + type string; + } + } + } + + leaf status { + type string; + } + } + } +} \ No newline at end of file diff --git a/yang/yang-data-codec-xml/src/test/resources/schema/rpc-test-payload1.xml b/yang/yang-data-codec-xml/src/test/resources/schema/rpc-test-payload1.xml new file mode 100644 index 0000000000..d81377a3cd --- /dev/null +++ b/yang/yang-data-codec-xml/src/test/resources/schema/rpc-test-payload1.xml @@ -0,0 +1,4 @@ + + 123014 + \ No newline at end of file diff --git a/yang/yang-data-codec-xml/src/test/resources/schema/rpc-test-payload2.xml b/yang/yang-data-codec-xml/src/test/resources/schema/rpc-test-payload2.xml new file mode 100644 index 0000000000..79792cc024 --- /dev/null +++ b/yang/yang-data-codec-xml/src/test/resources/schema/rpc-test-payload2.xml @@ -0,0 +1,12 @@ + + + + + id12345_test_data + + + testing_data + \ No newline at end of file diff --git a/yang/yang-data-codec-xml/src/test/resources/schema/simple.xml b/yang/yang-data-codec-xml/src/test/resources/schema/simple.xml new file mode 100644 index 0000000000..634bed7130 --- /dev/null +++ b/yang/yang-data-codec-xml/src/test/resources/schema/simple.xml @@ -0,0 +1,74 @@ + + + true + + 44 + + a + b + + + 1 + + 32 + 16 + prefix:b + direct Value + 16 + /prefix:container/prefix:list[prefix:uint32InList="1"] + + augmentInList + augmentInListCase1 + + + 2 + + 32 + 16 + b + + + true + + pref2:44 + + + + + + augmentInListCase2 + + + 4 + + + + + + module + 2012-12-12 + x:yang + + + + + + + + 3 + + + choice1Case1 + 41 + deep + + + choice2Case1 + + + 999 + + + + + \ No newline at end of file diff --git a/yang/yang-data-codec-xml/src/test/resources/schema/simple2.xml b/yang/yang-data-codec-xml/src/test/resources/schema/simple2.xml new file mode 100644 index 0000000000..2c4a5d1974 --- /dev/null +++ b/yang/yang-data-codec-xml/src/test/resources/schema/simple2.xml @@ -0,0 +1,44 @@ + + + choice1Case1 + 41 + deep + + + aug1 + + + + aug2 + + + 66 + + + + + + 661 + + + 662 + + + + + + 6621 + + + + + + aug3 + + + + + + + + diff --git a/yang/yang-data-codec-xml/src/test/resources/schema/simple_xml_with_attributes.xml b/yang/yang-data-codec-xml/src/test/resources/schema/simple_xml_with_attributes.xml new file mode 100644 index 0000000000..163a313f2d --- /dev/null +++ b/yang/yang-data-codec-xml/src/test/resources/schema/simple_xml_with_attributes.xml @@ -0,0 +1,14 @@ + + + + + 3 + + + false + + a + + + + diff --git a/yang/yang-data-codec-xml/src/test/resources/schema/test.yang b/yang/yang-data-codec-xml/src/test/resources/schema/test.yang new file mode 100644 index 0000000000..d5def9cfba --- /dev/null +++ b/yang/yang-data-codec-xml/src/test/resources/schema/test.yang @@ -0,0 +1,243 @@ +// vi: set smarttab et sw=4 tabstop=4: +module test { + yang-version 1; + namespace "urn:opendaylight:params:xml:ns:yang:controller:test"; + prefix "test"; + + organization "Cisco Systems, Inc."; + + revision "2014-3-13" { + description + "Initial revision"; + } + + identity a {} + + identity b { + base "test:a"; + } + + grouping listGroup { + list list { + key "uint32InList"; + + leaf uint32InList { + type uint32; + } + + container containerInList{ + leaf uint32 { + type uint32; + } + leaf uint16 { + type uint16; + } + + leaf identityr { + type identityref { + base "test:a"; + } + } + + leaf uint16-ref { + type leafref { + path "../uint16"; + } + } + + leaf instance-id { + type instance-identifier; + } + + anyxml anyX; + } + } + } + + grouping innerContainerGrouping { + container innerContainer { + leaf uint16 { + type uint16; + } + + container innerInnerContainer { + + leaf uint16 { + type uint16; + } + + leaf uint32 { + type uint32; + } + } + } + } + + container container { + leaf uint32 { + type uint32; + } + + leaf decimal64 { + type decimal64 { + fraction-digits 2; + } + } + + leaf boolean { + type boolean; + } + + leaf binary { + type binary; + } + + leaf string { + type string; + } + + uses listGroup { + augment "list/" { + leaf stringAugmentedToList{ + type string; + } + + choice choiceInList { + case caseInList1 { + leaf stringAugmentedToListInCase1 { + type string; + } + } + case caseInList2 { + leaf stringAugmentedToListInCase2 { + type string; + } + } + } + } + } + + list directList { + leaf stringInDirectList { + type string; + } + } + + uses innerContainerGrouping; + + choice choice{} + choice choice2{} + + leaf-list leafList { + type string; + } + + leaf identityRef { + type identityref { + base test-identity; + } + } + } + + augment "/container/" { + leaf augmentUint32 { + type uint32; + } + } + + augment "/container/directList/" { + leaf augmentedString { + type uint32; + } + } + + augment "/container/choice/" { + case test-identity-augment { + when "/container/identityRef = 'test-identity'"; + leaf augmentString1 { + type string; + } + + leaf augmentInt1 { + type uint32; + } + } + case test-identity-augment2 { + when "/container/identityRef = 'test-identity2'"; + leaf augmentString2 { + type string; + } + + leaf augmentInt2 { + type uint32; + } + } + } + + augment "/container/choice/test-identity-augment/" { + + choice augmentedChoiceInCase { + + case augmentedCaseInAugmentedChoice { + leaf stringInAugmentedCaseInAugmentedChoice { + type string; + } + } + + case augmentedCaseInAugmentedChoice2 { + leaf stringInAugmentedCaseInAugmentedChoice2 { + type string; + } + } + } + } + + augment "/container/choice/test-identity-augment/augmentedChoiceInCase/" { + case augmentedCaseInAugmentedChoiceFromAugment { + leaf stringInAugmentedCaseInAugmentedChoiceFromAugment { + type string; + } + } + } + + augment "/container/choice2/" { + case test-identity-augment { + when "/container/identityRef = 'test-identity'"; + container augmentContainer { + leaf augmentStringInaugmentContainer { + type string; + } + } + } + case test-identity-augment2 { + when "/container/identityRef = 'test-identity2'"; + list augmentedList { + leaf augmentStringInaugmentList { + type string; + } + } + } + } + + + augment "/container/choice2/test-identity-augment2/augmentedList/" { + + container augmentedContainerInAugmentedListInAugmentedCase { + leaf-list leafInAugmentedContainerInAugmentedListInAugmentedCase { + type uint32; + } + } + + list augmentedListInAugmentedListInAugmentedCase { + leaf-list leafInAugmentedListInAugmentedListInAugmentedCase { + type uint32; + } + } + } + + identity test-identity {} + identity test-identity2 { + base test-identity; + } + +} \ No newline at end of file