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 150e7b301d559bdc95d238b5f447381246a62a91..6f4b1cbc7d0be6fb2df6adf86bb5a3020b7f41aa 100644 (file)
@@ -5,14 +5,15 @@
  * 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.restconf.nb.rfc8040.jersey.providers.patch;
 
 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+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,16 +21,20 @@ import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 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 {
 
-    private final JsonToPatchBodyReader jsonToPatchBodyReader;
+    private final JsonPatchBodyReader jsonToPatchBodyReader;
     private static EffectiveModelContext schemaContext;
 
     public JsonPatchBodyReaderTest() throws Exception {
         super(schemaContext);
-        jsonToPatchBodyReader = new JsonToPatchBodyReader(schemaContextHandler, mountPointServiceHandler);
+        jsonToPatchBodyReader = new JsonPatchBodyReader(schemaContextHandler, mountPointService);
     }
 
     @Override
@@ -47,11 +52,10 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
         mockBodyReader(uri, jsonToPatchBodyReader, false);
 
-        final InputStream inputStream = JsonBodyReaderTest.class
-                .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
+        final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
+            "/instanceidentifier/json/jsonPATCHdata.json");
 
-        final PatchContext returnValue = jsonToPatchBodyReader
-                .readFrom(null, null, null, mediaType, null, inputStream);
+        final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
         checkPatchContext(returnValue);
     }
 
@@ -63,11 +67,10 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
         mockBodyReader(uri, jsonToPatchBodyReader, false);
 
-        final InputStream inputStream = JsonBodyReaderTest.class
-                .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
+        final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
+            "/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
 
-        final PatchContext returnValue = jsonToPatchBodyReader
-                .readFrom(null, null, null, mediaType, null, inputStream);
+        final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
         checkPatchContext(returnValue);
     }
 
@@ -80,15 +83,12 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
         mockBodyReader(uri, jsonToPatchBodyReader, false);
 
-        final InputStream inputStream = JsonBodyReaderTest.class
-                .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
+        final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
+            "/instanceidentifier/json/jsonPATCHdataValueMissing.json");
 
-        try {
-            jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
-            fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
-        } catch (final RestconfDocumentedException e) {
-            assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
-        }
+        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+            () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
+        assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
     }
 
     /**
@@ -100,15 +100,12 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
         mockBodyReader(uri, jsonToPatchBodyReader, false);
 
-        final InputStream inputStream = JsonBodyReaderTest.class
-                .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
+        final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
+            "/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
 
-        try {
-            jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
-            fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
-        } catch (final RestconfDocumentedException e) {
-            assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
-        }
+        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+            () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
+        assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
     }
 
     /**
@@ -119,11 +116,10 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
         final String uri = "instance-identifier-patch-module:patch-cont";
         mockBodyReader(uri, jsonToPatchBodyReader, false);
 
-        final InputStream inputStream = JsonBodyReaderTest.class
-                .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
+        final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
+            "/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
 
-        final PatchContext returnValue = jsonToPatchBodyReader
-                .readFrom(null, null, null, mediaType, null, inputStream);
+        final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
         checkPatchContext(returnValue);
     }
 
@@ -135,11 +131,10 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
         mockBodyReader(uri, jsonToPatchBodyReader, false);
 
-        final InputStream inputStream = JsonBodyReaderTest.class
-                .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
+        final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
+            "/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
 
-        final PatchContext returnValue = jsonToPatchBodyReader
-                .readFrom(null, null, null, mediaType, null, inputStream);
+        final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
         checkPatchContext(returnValue);
     }
 
@@ -151,11 +146,10 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
         final String uri = "instance-identifier-patch-module:patch-cont";
         mockBodyReader(uri, jsonToPatchBodyReader, false);
 
-        final InputStream inputStream = JsonBodyReaderTest.class
-                .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
+        final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
+            "/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
 
-        final PatchContext returnValue = jsonToPatchBodyReader
-                .readFrom(null, null, null, mediaType, null, inputStream);
+        final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
         checkPatchContext(returnValue);
     }
 
@@ -167,12 +161,60 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
         mockBodyReader(uri, jsonToPatchBodyReader, false);
 
-        final InputStream inputStream =
-                JsonBodyReaderTest.class
-                .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
+        final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
+            "/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
+
+        final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
+        checkPatchContext(returnValue);
+    }
+
+    /**
+     * Test of Yang Patch on the top-level container with empty URI for data root.
+     */
+    @Test
+    public void modulePatchTargetTopLevelContainerWithEmptyURITest() throws Exception {
+        final String uri = "";
+        mockBodyReader(uri, jsonToPatchBodyReader, false);
+
+        final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
+                "/instanceidentifier/json/jsonPATCHTargetTopLevelContainerWithEmptyURI.json");
+
+        final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
+        checkPatchContext(returnValue);
+    }
 
-        final PatchContext returnValue = jsonToPatchBodyReader
-                .readFrom(null, null, null, mediaType, null, inputStream);
+    /**
+     * 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);
     }
 }