2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.nb.rfc8040.databind;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
13 import org.junit.jupiter.api.Test;
14 import org.opendaylight.restconf.server.api.JsonResourceBody;
15 import org.opendaylight.yangtools.yang.common.ErrorTag;
16 import org.opendaylight.yangtools.yang.common.ErrorType;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
22 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
24 class JsonResourceBodyTest extends AbstractResourceBodyTest {
25 JsonResourceBodyTest() {
26 super(JsonResourceBody::new);
30 void moduleDataTest() throws Exception {
31 final var entryId = NodeIdentifierWithPredicates.of(LST11,
32 Map.of(KEYVALUE111, "value1", KEYVALUE112, "value2"));
34 assertEquals(ImmutableNodes.newContainerBuilder()
35 .withNodeIdentifier(CONT_NID)
36 .withChild(ImmutableNodes.newContainerBuilder()
37 .withNodeIdentifier(CONT1_NID)
38 .withChild(ImmutableNodes.newSystemMapBuilder()
39 .withNodeIdentifier(new NodeIdentifier(LST11))
40 .withChild(ImmutableNodes.newMapEntryBuilder()
41 .withNodeIdentifier(entryId)
42 .withChild(ImmutableNodes.leafNode(KEYVALUE111, "value1"))
43 .withChild(ImmutableNodes.leafNode(KEYVALUE112, "value2"))
44 .withChild(ImmutableNodes.leafNode(LF111, YangInstanceIdentifier.of(CONT_NID, CONT1_NID,
45 new NodeIdentifier(LST11), entryId, LF112_NID)))
46 .withChild(ImmutableNodes.leafNode(LF112_NID, "lf112 value"))
50 .build(), parse("instance-identifier-module:cont", """
52 "instance-identifier-module:cont": {
54 "augment-module:lst11": [
56 "keyvalue111":"value1",
57 "keyvalue112":"value2",
58 "augment-augment-module:lf111": "/instance-identifier-module:cont/cont1\
59 /augment-module:lst11[keyvalue111=\\"value1\\"][keyvalue112=\\"value2\\"]/augment-augment-module:lf112",
60 "augment-augment-module:lf112": "lf112 value"
70 void moduleSubContainerDataPutTest() throws Exception {
71 assertEquals(ImmutableNodes.newContainerBuilder()
72 .withNodeIdentifier(CONT1_NID)
73 .withChild(ImmutableNodes.leafNode(LF11, YangInstanceIdentifier.of(CONT_NID, CONT1_NID,
74 new NodeIdentifier(LFLST11), new NodeWithValue<>(LFLST11, "lflst11_1"))))
75 .build(), parse("instance-identifier-module:cont/cont1", """
77 "instance-identifier-module:cont1": {
78 "augment-module-leaf-list:lf11" : "/instance-identifier-module:cont\
79 /instance-identifier-module:cont1/augment-module-leaf-list:lflst11[.=\\"lflst11_1\\"]"
85 void testRangeViolation() {
86 assertRangeViolation(() -> parse("netconf786:foo", """
95 void testMismatchedInput() {
96 final var error = assertError(() -> parse("base:cont", """
98 "ietf-restconf:restconf-state" : {
102 Payload name ((urn:ietf:params:xml:ns:yang:ietf-restconf-monitoring?revision=2017-01-26)restconf-state) is \
103 different from identifier name ((ns?revision=2016-02-28)cont)""", error.getErrorMessage());
104 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
105 assertEquals(ErrorTag.MALFORMED_MESSAGE, error.getErrorTag());
109 void testMissingKeys() {
110 final var error = assertError(() -> parse("nested-module:depth1-cont/depth2-list2=one,two", """
113 "depth3-lf1-key" : "one"
117 Error parsing input: List entry (urn:nested:module?revision=2014-06-03)depth2-list2 is missing leaf values \
118 for [depth3-lf2-key]""", error.getErrorMessage());
119 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
120 assertEquals(ErrorTag.MALFORMED_MESSAGE, error.getErrorTag());
124 void testJukeboxBand() throws Exception {
125 final var one = QName.create("urn:nested:module", "2014-06-03", "depth3-lf1-key");
126 final var two = QName.create("urn:nested:module", "2014-06-03", "depth3-lf2-key");
128 assertEquals(ImmutableNodes.newMapEntryBuilder()
129 .withNodeIdentifier(NodeIdentifierWithPredicates.of(
130 QName.create("urn:nested:module", "2014-06-03", "depth2-list2"), Map.of(one, "one", two, "two")))
131 .withChild(ImmutableNodes.leafNode(one, "one"))
132 .withChild(ImmutableNodes.leafNode(two, "two"))
133 .build(), parse("nested-module:depth1-cont/depth2-list2=one,two", """
136 "depth3-lf1-key" : "one",
137 "depth3-lf2-key" : "two"