Test Json/Xml PatchBodyReaders on mixin nodes
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / XmlPatchBodyReaderTest.java
index 71a7bee0bf61dc0668f631424f55ea4c537a25df..c9b33b2062a3921439a211d88f8b2d0277bc2af0 100644 (file)
@@ -10,14 +10,22 @@ package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertThrows;
 
+import java.io.ByteArrayInputStream;
 import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 import javax.ws.rs.core.MediaType;
 import org.junit.BeforeClass;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
+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;
 
 public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
@@ -127,4 +135,149 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHTargetTopLevelContainerWithEmptyURI.xml");
         checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
     }
+
+    /**
+     * Test of Yang Patch on the top system map node element.
+     */
+    @Test
+    public void moduleTargetMapNodeTest() throws Exception {
+        mockBodyReader("", xmlToPatchBodyReader, false);
+        final var inputStream = new ByteArrayInputStream("""
+            <yang-patch xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-patch">
+                <patch-id>map-patch</patch-id>
+                <comment>YANG patch comment</comment>
+                <edit>
+                    <edit-id>edit1</edit-id>
+                    <operation>replace</operation>
+                    <target>/map-model:cont-root/map-model:cont1/map-model:my-map=key</target>
+                    <value>
+                        <my-map xmlns="map:ns">
+                            <key-leaf>key</key-leaf>
+                            <data-leaf>data</data-leaf>
+                        </my-map>
+                    </value>
+                </edit>
+            </yang-patch>
+            """.getBytes(StandardCharsets.UTF_8));
+        final var expectedData = Builders.mapBuilder()
+                .withNodeIdentifier(new NodeIdentifier(MAP_CONT_QNAME))
+                .withChild(Builders.mapEntryBuilder()
+                        .withNodeIdentifier(NodeIdentifierWithPredicates.of(MAP_CONT_QNAME, KEY_LEAF_QNAME, "key"))
+                        .withChild(ImmutableNodes.leafNode(KEY_LEAF_QNAME, "key"))
+                        .withChild(ImmutableNodes.leafNode(DATA_LEAF_QNAME, "data"))
+                        .build())
+                .build();
+        final var returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
+        checkPatchContext(returnValue);
+        final var data = returnValue.getData().get(0).getNode();
+        assertEquals(MAP_CONT_QNAME, data.getIdentifier().getNodeType());
+        assertEquals(expectedData, data);
+    }
+
+    /**
+     * Test of Yang Patch on the leaf set node element.
+     * TODO: Remove ignore when NETCONF-937 will be resolved
+     */
+    @Ignore
+    @Test
+    public void modulePatchTargetLeafSetNodeTest() throws Exception {
+        mockBodyReader("", xmlToPatchBodyReader, false);
+        final var inputStream = new ByteArrayInputStream("""
+            <yang-patch xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-patch">
+                <patch-id>set-patch</patch-id>
+                <comment>YANG patch comment</comment>
+                <edit>
+                    <edit-id>edit1</edit-id>
+                    <operation>replace</operation>
+                    <target>/set-model:cont-root/set-model:cont1/set-model:my-set="data1"</target>
+                    <value>
+                        <my-set xmlns="set:ns">data1</my-set>
+                    </value>
+                </edit>
+            </yang-patch>
+            """.getBytes(StandardCharsets.UTF_8));
+        final var expectedData = Builders.leafSetBuilder()
+                .withNodeIdentifier(new NodeIdentifier(LEAF_SET_QNAME))
+                .withChild(Builders.leafSetEntryBuilder()
+                        .withNodeIdentifier(new NodeWithValue(LEAF_SET_QNAME, "data1"))
+                        .withValue("data1")
+                        .build())
+                .build();
+        final var returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
+        checkPatchContext(returnValue);
+        final var data = returnValue.getData().get(0).getNode();
+        assertEquals(LEAF_SET_QNAME, data.getIdentifier().getNodeType());
+        assertEquals(expectedData, data);
+    }
+
+    /**
+     * Test of Yang Patch on the top unkeyed list element.
+     */
+    @Test
+    public void moduleTargetUnkeyedListNodeTest() throws Exception {
+        mockBodyReader("", xmlToPatchBodyReader, false);
+        final var inputStream = new ByteArrayInputStream("""
+            <yang-patch xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-patch">
+                <patch-id>list-patch</patch-id>
+                <comment>YANG patch comment</comment>
+                <edit>
+                    <edit-id>edit1</edit-id>
+                    <operation>replace</operation>
+                    <target>/list-model:cont-root/list-model:cont1/list-model:unkeyed-list</target>
+                    <value>
+                        <unkeyed-list xmlns="list:ns">
+                            <leaf1>data1</leaf1>
+                            <leaf2>data2</leaf2>
+                        </unkeyed-list>
+                    </value>
+                </edit>
+            </yang-patch>
+            """.getBytes(StandardCharsets.UTF_8));
+        final var expectedData = Builders.unkeyedListBuilder()
+                .withNodeIdentifier(new NodeIdentifier(LIST_QNAME))
+                .withChild(Builders.unkeyedListEntryBuilder()
+                        .withNodeIdentifier(new NodeIdentifier(LIST_QNAME))
+                        .withChild(ImmutableNodes.leafNode(LIST_LEAF1_QNAME, "data1"))
+                        .withChild(ImmutableNodes.leafNode(LIST_LEAF2_QNAME, "data2"))
+                        .build())
+                .build();
+        final var returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
+        checkPatchContext(returnValue);
+        final var data = returnValue.getData().get(0).getNode();
+        assertEquals(LIST_QNAME, data.getIdentifier().getNodeType());
+        assertEquals(expectedData, data);
+    }
+
+    /**
+     * Test of Yang Patch on the top case node element.
+     */
+    @Test
+    public void moduleTargetCaseNodeTest() throws Exception {
+        mockBodyReader("", xmlToPatchBodyReader, false);
+        final var inputStream = new ByteArrayInputStream("""
+            <yang-patch xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-patch">
+                <patch-id>choice-patch</patch-id>
+                <comment>YANG patch comment</comment>
+                <edit>
+                    <edit-id>edit1</edit-id>
+                    <operation>replace</operation>
+                    <target>/choice-model:cont-root/choice-model:cont1/choice-model:case-cont1</target>
+                    <value>
+                        <case-cont1 xmlns="choice:ns">
+                            <case-leaf1>data</case-leaf1>
+                        </case-cont1>
+                    </value>
+                </edit>
+            </yang-patch>
+            """.getBytes(StandardCharsets.UTF_8));
+        final var expectedData = Builders.containerBuilder()
+                .withNodeIdentifier(new NodeIdentifier(CHOICE_CONT_QNAME))
+                .withChild(ImmutableNodes.leafNode(CASE_LEAF1_QNAME, "data"))
+                .build();
+        final var returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
+        checkPatchContext(returnValue);
+        final var data = returnValue.getData().get(0).getNode();
+        assertEquals(CHOICE_CONT_QNAME, data.getIdentifier().getNodeType());
+        assertEquals(expectedData, data);
+    }
 }