Fix missing location in error reports
[yangtools.git] / yang / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / XmlToNormalizedNodesTest.java
index 28d1cab4b86322b3a1eb00687d2fac87e4d27dea..b3824db9800757e1acba913d50430e2b409f9676 100644 (file)
@@ -5,20 +5,20 @@
  * 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;
@@ -46,7 +46,7 @@ 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;
@@ -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;
 
@@ -153,13 +153,9 @@ public class XmlToNormalizedNodesTest {
         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) {
-            assertEquals("Duplicate namespace \"foo-namespace\" element \"decimal64-leaf\" in XML input at: line 7 "
-                    + "column 25", ex.getMessage());
-        }
+        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
@@ -173,13 +169,9 @@ public class XmlToNormalizedNodesTest {
         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) {
-            assertEquals("Duplicate namespace \"foo-namespace\" element \"my-anyxml\" in XML input at: line 19 "
-                    + "column 20", ex.getMessage());
-        }
+        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
@@ -193,13 +185,9 @@ public class XmlToNormalizedNodesTest {
         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) {
-            assertEquals("Duplicate namespace \"foo-namespace\" element \"leaf-container\" in XML input at: line 13 "
-                    + "column 21", ex.getMessage());
-        }
+        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
@@ -213,13 +201,8 @@ public class XmlToNormalizedNodesTest {
         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
@@ -233,13 +216,8 @@ public class XmlToNormalizedNodesTest {
         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 \"</my-leaf-1>\"."));
-        }
+        final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
+        assertThat(ex.getMessage(), containsString("</my-leaf-1>"));
     }
 
     @Test
@@ -253,13 +231,8 @@ public class XmlToNormalizedNodesTest {
         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 \"</my-container-1>\"."));
-        }
+        final XMLStreamException ex = assertThrows(XMLStreamException.class, () -> xmlParser.parse(reader));
+        assertThat(ex.getMessage(), containsString("</my-container-1>"));
     }
 
     @Test
@@ -273,27 +246,24 @@ public class XmlToNormalizedNodesTest {
         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() {
         // 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))
@@ -341,13 +311,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(
@@ -355,7 +325,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(