Fix YANG patch request for augmented element
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / XmlPatchBodyReaderTest.java
index c9b33b2062a3921439a211d88f8b2d0277bc2af0..d0826a3a38263545b3f6a5443f216bbd75cb6043 100644 (file)
@@ -15,7 +15,6 @@ 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;
@@ -136,6 +135,39 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
         checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
     }
 
+    /**
+     * Test of Yang Patch on the top augmented element.
+     */
+    @Test
+    public void moduleTargetTopLevelAugmentedContainerTest() throws Exception {
+        mockBodyReader("", xmlToPatchBodyReader, false);
+        final var inputStream = new ByteArrayInputStream("""
+            <yang-patch xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-patch">
+                <patch-id>test-patch</patch-id>
+                <comment>This test patch for augmented element</comment>
+                <edit>
+                    <edit-id>edit1</edit-id>
+                    <operation>replace</operation>
+                    <target>/test-m:container-root/test-m:container-lvl1/test-m-aug:container-aug</target>
+                    <value>
+                        <container-aug xmlns="test-ns-aug">
+                            <leaf-aug>data</leaf-aug>
+                        </container-aug>
+                    </value>
+                </edit>
+            </yang-patch>
+            """.getBytes(StandardCharsets.UTF_8));
+        final var expectedData = Builders.containerBuilder()
+                .withNodeIdentifier(new NodeIdentifier(CONT_AUG_QNAME))
+                .withChild(ImmutableNodes.leafNode(LEAF_AUG_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(CONT_AUG_QNAME, data.getIdentifier().getNodeType());
+        assertEquals(expectedData, data);
+    }
+
     /**
      * Test of Yang Patch on the top system map node element.
      */
@@ -176,9 +208,7 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
 
     /**
      * 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);
@@ -199,7 +229,7 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
         final var expectedData = Builders.leafSetBuilder()
                 .withNodeIdentifier(new NodeIdentifier(LEAF_SET_QNAME))
                 .withChild(Builders.leafSetEntryBuilder()
-                        .withNodeIdentifier(new NodeWithValue(LEAF_SET_QNAME, "data1"))
+                        .withNodeIdentifier(new NodeWithValue<>(LEAF_SET_QNAME, "data1"))
                         .withValue("data1")
                         .build())
                 .build();
@@ -280,4 +310,31 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
         assertEquals(CHOICE_CONT_QNAME, data.getIdentifier().getNodeType());
         assertEquals(expectedData, data);
     }
+
+    /**
+     * Test reading simple leaf value.
+     */
+    @Test
+    public void modulePatchSimpleLeafValueTest() throws Exception {
+        mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
+        final var inputStream = new ByteArrayInputStream("""
+                <yang-patch xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-patch">
+                    <patch-id>test-patch</patch-id>
+                    <comment>this is test patch</comment>
+                    <edit>
+                        <edit-id>edit1</edit-id>
+                        <operation>replace</operation>
+                        <target>/my-list2=my-leaf20/name</target>
+                        <value>
+                            <name xmlns="instance:identifier:patch:module">my-leaf20</name>
+                        </value>
+                    </edit>
+                </yang-patch>
+                """.getBytes(StandardCharsets.UTF_8));
+        final var returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
+        checkPatchContext(returnValue);
+        final var data = returnValue.getData().get(0).getNode();
+        assertEquals(LEAF_NAME_QNAME, data.getIdentifier().getNodeType());
+        assertEquals(ImmutableNodes.leafNode(LEAF_NAME_QNAME, "my-leaf20"), data);
+    }
 }