Fix YANG patch request for augmented element
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / JsonPatchBodyReaderTest.java
index d9f42d9978f6247a0f53a38c25545f5c8ba58551..6f4b1cbc7d0be6fb2df6adf86bb5a3020b7f41aa 100644 (file)
@@ -11,7 +11,9 @@ import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
 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.Test;
@@ -20,6 +22,9 @@ import org.opendaylight.restconf.common.patch.PatchContext;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+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 JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
@@ -177,4 +182,39 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
         checkPatchContext(returnValue);
     }
+
+    /**
+     * Test of Yang Patch on the top augmented element.
+     */
+    @Test
+    public void modulePatchTargetTopLevelAugmentedContainerTest() throws Exception {
+        mockBodyReader("", jsonToPatchBodyReader, false);
+        final var inputStream = new ByteArrayInputStream(("{\n"
+            + "    \"ietf-yang-patch:yang-patch\": {\n"
+            + "        \"patch-id\": \"test-patch\",\n"
+            + "        \"comment\": \"comment\",\n"
+            + "        \"edit\": [\n"
+            + "            {\n"
+            + "                \"edit-id\": \"edit1\",\n"
+            + "                \"operation\": \"replace\",\n"
+            + "                \"target\": \"/test-m:container-root/test-m:container-lvl1/test-m-aug:container-aug\",\n"
+            + "                \"value\": {\n"
+            + "                    \"container-aug\": {\n"
+            + "                        \"leaf-aug\": \"data\"\n"
+            + "                    }\n"
+            + "                }\n"
+            + "            }\n"
+            + "        ]\n"
+            + "    }\n"
+            + "}").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 = jsonToPatchBodyReader.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);
+    }
 }