Remove DataPostPath
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / databind / XmlChildBodyTest.java
index e845f1bfb7207aa6706d1c1c62f048088afb82c3..8ad3757d2e0eb324a8bf9db925826c07e1df6189 100644 (file)
@@ -7,44 +7,55 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.databind;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 
 import java.util.List;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.opendaylight.restconf.server.api.DatabindContext;
+import org.opendaylight.restconf.server.api.DatabindPath;
+import org.opendaylight.restconf.server.api.XmlChildBody;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
-import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
-import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
+import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
-public class XmlChildBodyTest extends AbstractBodyTest {
+class XmlChildBodyTest extends AbstractBodyTest {
     private static final QName TOP_LEVEL_LIST = QName.create("foo", "2017-08-09", "top-level-list");
 
-    private static EffectiveModelContext schemaContext;
+    private static DatabindPath.Data EMPTY_PATH;
+    private static DatabindPath.Data CONT_PATH;
 
-    @BeforeClass
-    public static void initialization() throws Exception {
+    @BeforeAll
+    static void beforeAll() throws Exception {
         final var testFiles = loadFiles("/instanceidentifier/yang");
         testFiles.addAll(loadFiles("/modules"));
         testFiles.addAll(loadFiles("/foo-xml-test/yang"));
-        schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
+        final var modelContext = YangParserTestUtils.parseYangFiles(testFiles);
+
+        final var contPath = YangInstanceIdentifier.of(CONT_QNAME);
+        final var databind = DatabindContext.ofModel(modelContext);
+        final var nodeAndStack = databind.schemaTree().enterPath(contPath).orElseThrow();
+        CONT_PATH = new DatabindPath.Data(databind, nodeAndStack.stack().toInference(), contPath, nodeAndStack.node());
+        EMPTY_PATH = new DatabindPath.Data(DatabindContext.ofModel(modelContext));
     }
 
     @Test
-    public void postXmlTest() throws Exception {
-        final var body = new XmlChildBody(XmlChildBodyTest.class.getResourceAsStream("/foo-xml-test/foo.xml"));
-        final var payload = body.toPayload(YangInstanceIdentifier.of(), Inference.ofDataTreePath(schemaContext));
+    void postXmlTest() {
+        final var body = new XmlChildBody(stringInputStream("""
+            <top-level-list xmlns="foo">
+                <key-leaf>key-value</key-leaf>
+                <ordinary-leaf>leaf-value</ordinary-leaf>
+            </top-level-list>"""));
+        final var payload = body.toPayload(EMPTY_PATH);
 
         final var entryId = NodeIdentifierWithPredicates.of(TOP_LEVEL_LIST,
             QName.create(TOP_LEVEL_LIST, "key-leaf"), "key-value");
         assertEquals(List.of(new NodeIdentifier(TOP_LEVEL_LIST), entryId), payload.prefix());
-        assertEquals(Builders.mapEntryBuilder()
+        assertEquals(ImmutableNodes.newMapEntryBuilder()
             .withNodeIdentifier(entryId)
             .withChild(ImmutableNodes.leafNode(QName.create(TOP_LEVEL_LIST, "key-leaf"), "key-value"))
             .withChild(ImmutableNodes.leafNode(QName.create(TOP_LEVEL_LIST, "ordinary-leaf"), "leaf-value"))
@@ -52,17 +63,22 @@ public class XmlChildBodyTest extends AbstractBodyTest {
     }
 
     @Test
-    public void moduleSubContainerDataPostTest() throws Exception {
-        final var body = new XmlChildBody(
-            XmlChildBodyTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
-        final var payload = body.toPayload(YangInstanceIdentifier.of(CONT_QNAME),
-            Inference.ofDataTreePath(schemaContext, CONT_QNAME));
+    void moduleSubContainerDataPostTest() {
+        final var body = new XmlChildBody(stringInputStream("""
+            <cont1 xmlns="instance:identifier:module">
+              <lflst11 xmlns="augment:module:leaf:list">lflst11_1</lflst11>
+              <lflst11 xmlns="augment:module:leaf:list">lflst11_2</lflst11>
+              <lflst11 xmlns="augment:module:leaf:list">lflst11_3</lflst11>
+              <lf11 xmlns:a="instance:identifier:module" xmlns:b="augment:module:leaf:list" \
+            xmlns="augment:module:leaf:list">/a:cont/a:cont1/b:lflst11[.="lflst11_1"]</lf11>
+            </cont1>"""));
+        final var payload = body.toPayload(CONT_PATH);
 
         final var lflst11 = QName.create("augment:module:leaf:list", "2014-01-27", "lflst11");
         assertEquals(List.of(new NodeIdentifier(CONT1_QNAME)), payload.prefix());
-        assertEquals(Builders.containerBuilder()
+        assertEquals(ImmutableNodes.newContainerBuilder()
             .withNodeIdentifier(new NodeIdentifier(CONT1_QNAME))
-            .withChild(Builders.leafSetBuilder()
+            .withChild(ImmutableNodes.newSystemLeafSetBuilder()
                 .withNodeIdentifier(new NodeIdentifier(lflst11))
                 .withChildValue("lflst11_1")
                 .withChildValue("lflst11_2")
@@ -77,33 +93,31 @@ public class XmlChildBodyTest extends AbstractBodyTest {
     }
 
     @Test
-    public void moduleSubContainerAugmentDataPostTest() throws Exception {
+    void moduleSubContainerAugmentDataPostTest() {
         final var body = new XmlChildBody(
             XmlChildBodyTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml"));
-        final var payload = body.toPayload(YangInstanceIdentifier.of(CONT_QNAME),
-            Inference.ofDataTreePath(schemaContext, CONT_QNAME));
+        final var payload = body.toPayload(CONT_PATH);
 
         final var contAugment = QName.create("augment:module", "2014-01-17", "cont-augment");
         assertEquals(List.of(new NodeIdentifier(contAugment)), payload.prefix());
-        assertEquals(Builders.containerBuilder()
+        assertEquals(ImmutableNodes.newContainerBuilder()
             .withNodeIdentifier(new NodeIdentifier(contAugment))
             .withChild(ImmutableNodes.leafNode(QName.create(contAugment, "leaf1"), "stryng"))
             .build(), payload.body());
     }
 
     @Test
-    public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
+    void moduleSubContainerChoiceAugmentDataPostTest() {
         final var body = new XmlChildBody(
             XmlChildBodyTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml"));
-        final var payload = body.toPayload(YangInstanceIdentifier.of(CONT_QNAME),
-            Inference.ofDataTreePath(schemaContext, CONT_QNAME));
+        final var payload = body.toPayload(CONT_PATH);
 
         final var container1 = QName.create("augment:module", "2014-01-17", "case-choice-case-container1");
         assertEquals(List.of(
             new NodeIdentifier(QName.create(container1, "augment-choice1")),
             new NodeIdentifier(QName.create(container1, "augment-choice2")),
             new NodeIdentifier(container1)), payload.prefix());
-        assertEquals(Builders.containerBuilder()
+        assertEquals(ImmutableNodes.newContainerBuilder()
             .withNodeIdentifier(new NodeIdentifier(container1))
             .withChild(ImmutableNodes.leafNode(QName.create(container1, "case-choice-case-leaf1"), "stryng"))
             .build(), payload.body());
@@ -115,14 +129,14 @@ public class XmlChildBodyTest extends AbstractBodyTest {
      * name, but also by correct namespace used in payload.
      */
     @Test
-    public void findFooContainerUsingNamespaceTest() throws Exception {
+    void findFooContainerUsingNamespaceTest() {
         final var body = new XmlChildBody(
             XmlChildBodyTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml"));
-        final var payload = body.toPayload(YangInstanceIdentifier.of(), Inference.ofDataTreePath(schemaContext));
+        final var payload = body.toPayload(EMPTY_PATH);
 
         final var fooBarContainer = new NodeIdentifier(QName.create("foo:module", "2016-09-29", "foo-bar-container"));
         assertEquals(List.of(fooBarContainer), payload.prefix());
-        assertEquals(Builders.containerBuilder().withNodeIdentifier(fooBarContainer).build(),
+        assertEquals(ImmutableNodes.newContainerBuilder().withNodeIdentifier(fooBarContainer).build(),
             payload.body());
     }
 
@@ -132,14 +146,14 @@ public class XmlChildBodyTest extends AbstractBodyTest {
      * name, but also by correct namespace used in payload.
      */
     @Test
-    public void findBarContainerUsingNamespaceTest() throws Exception {
+    void findBarContainerUsingNamespaceTest() {
         final var body = new XmlChildBody(
             XmlChildBodyTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml"));
-        final var payload = body.toPayload(YangInstanceIdentifier.of(), Inference.ofDataTreePath(schemaContext));
+        final var payload = body.toPayload(EMPTY_PATH);
 
         final var fooBarContainer = new NodeIdentifier(QName.create("bar:module", "2016-09-29", "foo-bar-container"));
         assertEquals(List.of(fooBarContainer), payload.prefix());
-        assertEquals(Builders.containerBuilder().withNodeIdentifier(fooBarContainer).build(),
+        assertEquals(ImmutableNodes.newContainerBuilder().withNodeIdentifier(fooBarContainer).build(),
             payload.body());
     }
 }