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,
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;
}
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.
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;
}
}
- 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());
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;
}
}"""));
}
+
+ @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());
+ }
}
*/
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()
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());
+ }
+
}