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.yangtools.yang.common.ErrorTag;
15 import org.opendaylight.yangtools.yang.common.ErrorType;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
21 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
23 class JsonResourceBodyTest extends AbstractResourceBodyTest {
24 JsonResourceBodyTest() {
25 super(JsonResourceBody::new);
29 void moduleDataTest() throws Exception {
30 final var entryId = NodeIdentifierWithPredicates.of(LST11,
31 Map.of(KEYVALUE111, "value1", KEYVALUE112, "value2"));
33 assertEquals(ImmutableNodes.newContainerBuilder()
34 .withNodeIdentifier(CONT_NID)
35 .withChild(ImmutableNodes.newContainerBuilder()
36 .withNodeIdentifier(CONT1_NID)
37 .withChild(ImmutableNodes.newSystemMapBuilder()
38 .withNodeIdentifier(new NodeIdentifier(LST11))
39 .withChild(ImmutableNodes.newMapEntryBuilder()
40 .withNodeIdentifier(entryId)
41 .withChild(ImmutableNodes.leafNode(KEYVALUE111, "value1"))
42 .withChild(ImmutableNodes.leafNode(KEYVALUE112, "value2"))
43 .withChild(ImmutableNodes.leafNode(LF111, YangInstanceIdentifier.of(CONT_NID, CONT1_NID,
44 new NodeIdentifier(LST11), entryId, LF112_NID)))
45 .withChild(ImmutableNodes.leafNode(LF112_NID, "lf112 value"))
49 .build(), parse("instance-identifier-module:cont", """
51 "instance-identifier-module:cont": {
53 "augment-module:lst11": [
55 "keyvalue111":"value1",
56 "keyvalue112":"value2",
57 "augment-augment-module:lf111": "/instance-identifier-module:cont/cont1\
58 /augment-module:lst11[keyvalue111=\\"value1\\"][keyvalue112=\\"value2\\"]/augment-augment-module:lf112",
59 "augment-augment-module:lf112": "lf112 value"
69 void moduleSubContainerDataPutTest() throws Exception {
70 assertEquals(ImmutableNodes.newContainerBuilder()
71 .withNodeIdentifier(CONT1_NID)
72 .withChild(ImmutableNodes.leafNode(LF11, YangInstanceIdentifier.of(CONT_NID, CONT1_NID,
73 new NodeIdentifier(LFLST11), new NodeWithValue<>(LFLST11, "lflst11_1"))))
74 .build(), parse("instance-identifier-module:cont/cont1", """
76 "instance-identifier-module:cont1": {
77 "augment-module-leaf-list:lf11" : "/instance-identifier-module:cont\
78 /instance-identifier-module:cont1/augment-module-leaf-list:lflst11[.=\\"lflst11_1\\"]"
84 void testRangeViolation() {
85 assertRangeViolation(() -> parse("netconf786:foo", """
94 void testMismatchedInput() {
95 final var error = assertError(() -> parse("base:cont", """
97 "ietf-restconf:restconf-state" : {
101 Payload name ((urn:ietf:params:xml:ns:yang:ietf-restconf-monitoring?revision=2017-01-26)restconf-state) is \
102 different from identifier name ((ns?revision=2016-02-28)cont)""", error.getErrorMessage());
103 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
104 assertEquals(ErrorTag.MALFORMED_MESSAGE, error.getErrorTag());
108 void testMissingKeys() {
109 final var error = assertError(() -> parse("nested-module:depth1-cont/depth2-list2=one,two", """
112 "depth3-lf1-key" : "one"
116 Error parsing input: List entry (urn:nested:module?revision=2014-06-03)depth2-list2 is missing leaf values \
117 for [depth3-lf2-key]""", error.getErrorMessage());
118 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
119 assertEquals(ErrorTag.MALFORMED_MESSAGE, error.getErrorTag());
123 void testJukeboxBand() throws Exception {
124 final var one = QName.create("urn:nested:module", "2014-06-03", "depth3-lf1-key");
125 final var two = QName.create("urn:nested:module", "2014-06-03", "depth3-lf2-key");
127 assertEquals(ImmutableNodes.newMapEntryBuilder()
128 .withNodeIdentifier(NodeIdentifierWithPredicates.of(
129 QName.create("urn:nested:module", "2014-06-03", "depth2-list2"), Map.of(one, "one", two, "two")))
130 .withChild(ImmutableNodes.leafNode(one, "one"))
131 .withChild(ImmutableNodes.leafNode(two, "two"))
132 .build(), parse("nested-module:depth1-cont/depth2-list2=one,two", """
135 "depth3-lf1-key" : "one",
136 "depth3-lf2-key" : "two"