X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-codec-xml%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fcodec%2Fxml%2FXmlToNormalizedNodesTest.java;h=c17bb835602a918c6c4c83114ca87584eeaf369e;hb=33c07eff30a65dd0889cc08dc88ddad72039321d;hp=a483ce76708459d744201dea34f9aa555849dd8d;hpb=2645b72cbef49a22f7520cb19b49c2d4b337ce7d;p=yangtools.git diff --git a/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlToNormalizedNodesTest.java b/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlToNormalizedNodesTest.java index a483ce7670..c17bb83560 100644 --- a/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlToNormalizedNodesTest.java +++ b/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlToNormalizedNodesTest.java @@ -5,31 +5,31 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.yangtools.yang.data.codec.xml; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import java.io.IOException; import java.io.InputStream; -import java.net.URI; import java.net.URISyntaxException; -import java.util.Collections; import java.util.HashMap; import java.util.Map; import javax.xml.parsers.ParserConfigurationException; -import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import org.opendaylight.yangtools.util.xml.UntrustedXML; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; +import org.opendaylight.yangtools.yang.common.XMLNamespace; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; @@ -38,25 +38,25 @@ import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode; import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.LeafNode; -import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode; import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; import org.xml.sax.SAXException; public class XmlToNormalizedNodesTest { - private static final QNameModule FOO_MODULE = QNameModule.create(URI.create("foo-namespace")); + private static final QNameModule FOO_MODULE = QNameModule.create(XMLNamespace.of("foo-namespace")); private static final QName PARENT_CONTAINER = QName.create(FOO_MODULE, "parent-container"); - private static final QNameModule BAZ_MODULE = QNameModule.create(URI.create("baz-namespace")); + private static final QNameModule BAZ_MODULE = QNameModule.create(XMLNamespace.of("baz-namespace")); private static final QName OUTER_CONTAINER = QName.create(BAZ_MODULE, "outer-container"); private static final QName MY_CONTAINER_1 = QName.create(BAZ_MODULE, "my-container-1"); @@ -80,7 +80,7 @@ public class XmlToNormalizedNodesTest { private static final QName MY_SECOND_KEY_LEAF = QName.create(BAZ_MODULE, "my-second-key-leaf"); private static final QName MY_LEAF_IN_LIST_3 = QName.create(BAZ_MODULE, "my-leaf-in-list-3"); - private static SchemaContext schemaContext; + private static EffectiveModelContext schemaContext; private static ContainerSchemaNode outerContainerSchema; private static ContainerSchemaNode parentContainerSchema; @@ -105,8 +105,7 @@ public class XmlToNormalizedNodesTest { ParserConfigurationException { final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/baz.xml"); - final XMLInputFactory factory = XMLInputFactory.newInstance(); - final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream); + final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream); final NormalizedNodeResult result = new NormalizedNodeResult(); final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result); @@ -117,10 +116,10 @@ public class XmlToNormalizedNodesTest { xmlParser.flush(); xmlParser.close(); - final NormalizedNode transformedInput = result.getResult(); + final NormalizedNode transformedInput = result.getResult(); assertNotNull(transformedInput); - final NormalizedNode expectedNormalizedNode = buildOuterContainerNode(); + final NormalizedNode expectedNormalizedNode = buildOuterContainerNode(); assertNotNull(expectedNormalizedNode); assertEquals(expectedNormalizedNode, transformedInput); @@ -131,8 +130,7 @@ public class XmlToNormalizedNodesTest { ParserConfigurationException, SAXException { final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/foo.xml"); - final XMLInputFactory factory = XMLInputFactory.newInstance(); - final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream); + final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream); final NormalizedNodeResult result = new NormalizedNodeResult(); final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result); @@ -140,7 +138,7 @@ public class XmlToNormalizedNodesTest { final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema); xmlParser.parse(reader); - final NormalizedNode transformedInput = result.getResult(); + final NormalizedNode transformedInput = result.getResult(); assertNotNull(transformedInput); } @@ -149,20 +147,15 @@ public class XmlToNormalizedNodesTest { ParserConfigurationException, SAXException, URISyntaxException { final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo.xml"); - final XMLInputFactory factory = XMLInputFactory.newInstance(); - final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream); + final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream); final NormalizedNodeResult result = new NormalizedNodeResult(); final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result); final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema); - try { - xmlParser.parse(reader); - fail("IllegalStateException should have been thrown because of duplicate leaf."); - } catch (IllegalStateException ex) { - assertTrue(ex.getMessage().contains("Duplicate element \"decimal64-leaf\" in XML input")); - } - + final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader)); + assertThat(ex.getMessage(), containsString("Duplicate element \"decimal64-leaf\" in namespace" + + " \"foo-namespace\" with parent \"container leaf-container\" in XML input")); } @Test @@ -170,19 +163,15 @@ public class XmlToNormalizedNodesTest { ParserConfigurationException, SAXException, URISyntaxException { final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-2.xml"); - final XMLInputFactory factory = XMLInputFactory.newInstance(); - final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream); + final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream); final NormalizedNodeResult result = new NormalizedNodeResult(); final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result); final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema); - try { - xmlParser.parse(reader); - fail("IllegalStateException should have been thrown because of duplicate anyxml"); - } catch (IllegalStateException ex) { - assertTrue(ex.getMessage().contains("Duplicate element \"my-anyxml\" in XML input")); - } + final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader)); + assertThat(ex.getMessage(), containsString("Duplicate element \"my-anyxml\" in namespace" + + " \"foo-namespace\" with parent \"container anyxml-container\" in XML input")); } @Test @@ -190,19 +179,15 @@ public class XmlToNormalizedNodesTest { ParserConfigurationException, SAXException, URISyntaxException { final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-foo-3.xml"); - final XMLInputFactory factory = XMLInputFactory.newInstance(); - final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream); + final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream); final NormalizedNodeResult result = new NormalizedNodeResult(); final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result); final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, parentContainerSchema); - try { - xmlParser.parse(reader); - fail("IllegalStateException should have been thrown because of duplicate container"); - } catch (IllegalStateException ex) { - assertTrue(ex.getMessage().contains("Duplicate element \"leaf-container\" in XML input")); - } + final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader)); + assertThat(ex.getMessage(), containsString("Duplicate element \"leaf-container\" in namespace" + + " \"foo-namespace\" with parent \"container parent-container\" in XML input")); } @Test @@ -210,20 +195,14 @@ public class XmlToNormalizedNodesTest { ParserConfigurationException, SAXException, URISyntaxException { final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz.xml"); - final XMLInputFactory factory = XMLInputFactory.newInstance(); - final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream); + final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream); final NormalizedNodeResult result = new NormalizedNodeResult(); final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result); final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema); - try { - xmlParser.parse(reader); - fail("XMLStreamException should have been thrown because of unterminated leaf element."); - } catch (XMLStreamException ex) { - assertTrue(ex.getMessage().contains("elementGetText() function expects text only elment but " - + "START_ELEMENT was encountered.")); - } + final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader)); + assertThat(ex.getMessage(), containsString(" START_ELEMENT ")); } @Test @@ -231,20 +210,14 @@ public class XmlToNormalizedNodesTest { ParserConfigurationException, SAXException, URISyntaxException { final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-2.xml"); - final XMLInputFactory factory = XMLInputFactory.newInstance(); - final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream); + final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream); final NormalizedNodeResult result = new NormalizedNodeResult(); final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result); final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema); - try { - xmlParser.parse(reader); - fail("XMLStreamException should have been thrown because of unterminated leaf element."); - } catch (XMLStreamException ex) { - assertTrue(ex.getMessage().contains("The element type \"my-leaf-1\" must be terminated by the matching " - + "end-tag \"\".")); - } + final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader)); + assertThat(ex.getMessage(), containsString("")); } @Test @@ -252,20 +225,14 @@ public class XmlToNormalizedNodesTest { ParserConfigurationException, SAXException, URISyntaxException { final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-4.xml"); - final XMLInputFactory factory = XMLInputFactory.newInstance(); - final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream); + final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream); final NormalizedNodeResult result = new NormalizedNodeResult(); final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result); final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema); - try { - xmlParser.parse(reader); - fail("XMLStreamException should have been thrown because of unterminated container element."); - } catch (XMLStreamException ex) { - assertTrue(ex.getMessage().contains("The element type \"my-container-1\" must be terminated by the " - + "matching end-tag \"\".")); - } + final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader)); + assertThat(ex.getMessage(), containsString("")); } @Test @@ -273,34 +240,30 @@ public class XmlToNormalizedNodesTest { ParserConfigurationException, SAXException, URISyntaxException { final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream("/invalid-baz-3.xml"); - final XMLInputFactory factory = XMLInputFactory.newInstance(); - final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream); + final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream); final NormalizedNodeResult result = new NormalizedNodeResult(); final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result); final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, outerContainerSchema); - try { - xmlParser.parse(reader); - fail("IllegalStateException should have been thrown because of an unknown child node."); - } catch (IllegalStateException ex) { - assertEquals("Schema for node with name my-container-1 and namespace baz-namespace does not exist at " - + "AbsoluteSchemaPath{path=[(baz-namespace)outer-container, (baz-namespace)my-container-1]}", - ex.getMessage()); - } + final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader)); + + assertThat(ex.getMessage(), containsString("Schema for node with name my-container-1 and namespace " + + "baz-namespace does not exist at " + + "AbsoluteSchemaPath{path=[(baz-namespace)outer-container, (baz-namespace)my-container-1]}")); } - private static NormalizedNode buildOuterContainerNode() { + private static NormalizedNode buildOuterContainerNode() { // my-container-1 MapNode myKeyedListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(MY_KEYED_LIST)) .withChild(Builders.mapEntryBuilder().withNodeIdentifier( - new NodeIdentifierWithPredicates(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue1")) + NodeIdentifierWithPredicates.of(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue1")) .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1)) .withValue("listleafvalue1").build()) .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2)) .withValue("listleafvalue2").build()).build()) .withChild(Builders.mapEntryBuilder().withNodeIdentifier( - new NodeIdentifierWithPredicates(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue2")) + NodeIdentifierWithPredicates.of(MY_KEYED_LIST, MY_KEY_LEAF, "listkeyvalue2")) .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_1)) .withValue("listleafvalue12").build()) .withChild(Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_IN_LIST_2)) @@ -309,7 +272,8 @@ public class XmlToNormalizedNodesTest { LeafNode myLeaf1Node = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAF_1)) .withValue("value1").build(); - LeafSetNode myLeafListNode = Builders.leafSetBuilder().withNodeIdentifier(new NodeIdentifier(MY_LEAFLIST)) + SystemLeafSetNode myLeafListNode = Builders.leafSetBuilder() + .withNodeIdentifier(new NodeIdentifier(MY_LEAFLIST)) .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier( new NodeWithValue<>(MY_LEAFLIST, "lflvalue1")).withValue("lflvalue1").build()) .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier( @@ -348,13 +312,13 @@ public class XmlToNormalizedNodesTest { MapNode myDoublyKeyedListNode = Builders.mapBuilder() .withNodeIdentifier(new NodeIdentifier(MY_DOUBLY_KEYED_LIST)) .withChild(Builders.mapEntryBuilder().withNodeIdentifier( - new NodeIdentifierWithPredicates(MY_DOUBLY_KEYED_LIST, keys)) + NodeIdentifierWithPredicates.of(MY_DOUBLY_KEYED_LIST, keys)) .withChild(Builders.leafBuilder().withNodeIdentifier( new NodeIdentifier(MY_LEAF_IN_LIST_3)).withValue("listleafvalue1").build()).build()) .build(); AugmentationNode myDoublyKeyedListAugNode = Builders.augmentationBuilder().withNodeIdentifier( - new AugmentationIdentifier(Collections.singleton(MY_DOUBLY_KEYED_LIST))) + new AugmentationIdentifier(ImmutableSet.of(MY_DOUBLY_KEYED_LIST))) .withChild(myDoublyKeyedListNode).build(); ContainerNode myContainer3Node = Builders.containerBuilder().withNodeIdentifier( @@ -362,7 +326,7 @@ public class XmlToNormalizedNodesTest { .withChild(myDoublyKeyedListAugNode).build(); AugmentationNode myContainer3AugNode = Builders.augmentationBuilder().withNodeIdentifier( - new AugmentationIdentifier(Collections.singleton(MY_CONTAINER_3))) + new AugmentationIdentifier(ImmutableSet.of(MY_CONTAINER_3))) .withChild(myContainer3Node).build(); ContainerNode outerContainerNode = Builders.containerBuilder().withNodeIdentifier(