Generalize validTopLevelNodeName() test 14/109114/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Nov 2023 22:08:05 +0000 (23:08 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Nov 2023 22:20:39 +0000 (23:20 +0100)
Assert the same violation in JSON/XML tests, which shows this method is
that XML is doing its own thing.

Since these are exercising this code path as they should, we can hide
the method and inline it.

Change-Id: I43d172283f4c0089726271e830b7e62f54b7db0c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/databind/ResourceBody.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/AbstractResourceBodyTest.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/JsonResourceBodyTest.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/ResourceBodyTest.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/XmlResourceBodyTest.java

index 3db17365dfcfc8835d91ef4aae7c5f994dd0ed8f..21e879bbe13cb63236d6b59de67abdbf36e6732a 100644 (file)
@@ -58,10 +58,10 @@ public abstract sealed class ResourceBody extends AbstractBody permits JsonResou
     public @NonNull NormalizedNode toNormalizedNode(final @NonNull DataPutPath path,
             final @NonNull SchemaNode schemaNode) {
         final var instance = path.instance();
-        final var expected = instance.isEmpty() ? DATA_NID : instance.getLastPathArgument();
+        final var expectedName = instance.isEmpty() ? DATA_NID : instance.getLastPathArgument();
         final var holder = new NormalizationResultHolder();
         try (var streamWriter = ImmutableNormalizedNodeStreamWriter.from(holder)) {
-            streamTo(path, expected, acquireStream(), streamWriter);
+            streamTo(path, expectedName, acquireStream(), streamWriter);
         } catch (IOException e) {
             LOG.debug("Error reading input", e);
             throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
@@ -83,7 +83,13 @@ public abstract sealed class ResourceBody extends AbstractBody permits JsonResou
             data = parsedData;
         }
 
-        validTopLevelNodeName(expected, data);
+        final var dataName = data.name();
+        if (!dataName.equals(expectedName)) {
+            throw new RestconfDocumentedException(
+                "Payload name (" + dataName + ") is different from identifier name (" + expectedName + ")",
+                ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
+        }
+
         validateListKeysEqualityInPayloadAndUri(schemaNode, instance, data);
         return data;
     }
@@ -91,22 +97,6 @@ public abstract sealed class ResourceBody extends AbstractBody permits JsonResou
     abstract void streamTo(@NonNull DataPutPath path, @NonNull PathArgument name, @NonNull InputStream inputStream,
         @NonNull NormalizedNodeStreamWriter writer) throws IOException;
 
-    /**
-     * Valid top level node name.
-     *
-     * @param apiPath path of node
-     * @param data data
-     */
-    @VisibleForTesting
-    static final void validTopLevelNodeName(final PathArgument expected, final NormalizedNode data) {
-        final var payloadName = data.name();
-        if (!payloadName.equals(expected)) {
-            throw new RestconfDocumentedException(
-                "Payload name (" + payloadName + ") is different from identifier name (" + expected + ")",
-                ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
-        }
-    }
-
     /**
      * Validates whether keys in {@code payload} are equal to values of keys in
      * {@code iiWithData} for list schema node.
index 1882a378b48f146fbe016d3371b7f5ef455f9b63..627ce0641df3ddc5a93e82978ade14d06f07e2b6 100644 (file)
@@ -25,6 +25,7 @@ import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.restconf.api.ApiPath;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
+import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.restconf.nb.rfc8040.legacy.InstanceIdentifierContext;
 import org.opendaylight.restconf.server.api.DataPutPath;
 import org.opendaylight.restconf.server.api.DatabindContext;
@@ -88,12 +89,15 @@ abstract class AbstractResourceBodyTest extends AbstractBodyTest {
         }
     }
 
-    static final void assertRangeViolation(final Executable executable) {
+    static final RestconfError assertError(final Executable executable) {
         final var ex = assertThrows(RestconfDocumentedException.class, executable);
         final var errors = ex.getErrors();
         assertEquals(1, errors.size());
+        return errors.get(0);
+    }
 
-        final var error = errors.get(0);
+    static final void assertRangeViolation(final Executable executable) {
+        final var error = assertError(executable);
         assertEquals(ErrorType.APPLICATION, error.getErrorType());
         assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
         assertEquals("bar error app tag", error.getErrorAppTag());
index 456eae5398d8111f4b77df98a6a7da575ed7850b..fe03e2a5d057f1111ba3f49b34c1776d5d7f2230 100644 (file)
@@ -11,6 +11,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 
 import java.util.Map;
 import org.junit.jupiter.api.Test;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 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;
@@ -87,4 +89,18 @@ class JsonResourceBodyTest extends AbstractResourceBodyTest {
               }
             }"""));
     }
+
+    @Test
+    void testMismatchedInput() throws Exception {
+        final var error = assertError(() -> parse("base:cont", """
+            {
+              "ietf-restconf:restconf-state" : {
+              }
+            }"""));
+        assertEquals("""
+            Payload name ((urn:ietf:params:xml:ns:yang:ietf-restconf-monitoring?revision=2017-01-26)restconf-state) is \
+            different from identifier name ((ns?revision=2016-02-28)cont)""", error.getErrorMessage());
+        assertEquals(ErrorType.PROTOCOL, error.getErrorType());
+        assertEquals(ErrorTag.MALFORMED_MESSAGE, error.getErrorTag());
+    }
 }
index 17d81686cbb3ec7720023a657b0128837d061ffe..d929e31c9325a024f8701ea94ecab303bbf0fd40 100644 (file)
@@ -7,27 +7,12 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.databind;
 
-import static org.junit.Assert.assertThrows;
-
 import org.junit.Test;
-import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.nb.rfc8040.AbstractJukeboxTest;
 import org.opendaylight.restconf.nb.rfc8040.legacy.InstanceIdentifierContext;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 
 public class ResourceBodyTest extends AbstractJukeboxTest {
-    @Test
-    public void testValidTopLevelNodeName() {
-        ResourceBody.validTopLevelNodeName(GAP_IID.getLastPathArgument(), GAP_LEAF);
-    }
-
-    @Test
-    public void testValidTopLevelNodeNameWrongTopIdentifier() {
-        // FIXME: more asserts
-        assertThrows(RestconfDocumentedException.class,
-            () -> ResourceBody.validTopLevelNodeName(PLAYLIST_IID.getLastPathArgument(), GAP_LEAF));
-    }
-
     @Test
     public void testValidateListKeysEqualityInPayloadAndUri() {
         final var path = YangInstanceIdentifier.builder()
index adfa9fdfb538517e881b9c22854bc01a71faa90d..e9621ff898e8d1ae354d2baae941924cf8ee0e52 100644 (file)
@@ -183,4 +183,16 @@ class XmlResourceBodyTest extends AbstractResourceBodyTest {
                 xmlns="augment:module:leaf:list">/a:cont/a:cont1/b:lflst11[.="lflst11_1"]</lf11>
                 </cont1>"""));
     }
+
+    @Test
+    void testMismatchedInput() throws Exception {
+        final var error = assertError(() -> parse("base:cont", """
+            <restconf-state xmlns="urn:ietf:params:xml:ns:yang:ietf-restconf"/>"""));
+        assertEquals("""
+            Incorrect message root element (urn:ietf:params:xml:ns:yang:ietf-restconf)restconf-state, should be \
+            (ns)cont""", error.getErrorMessage());
+        assertEquals(ErrorType.PROTOCOL, error.getErrorType());
+        assertEquals(ErrorTag.MALFORMED_MESSAGE, error.getErrorTag());
+    }
+
 }