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;
11 import static org.junit.jupiter.api.Assertions.assertNull;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import org.junit.jupiter.api.Test;
16 import org.opendaylight.yangtools.yang.common.ErrorTag;
17 import org.opendaylight.yangtools.yang.common.ErrorType;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
23 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
26 class JsonResourceBodyTest extends AbstractResourceBodyTest {
27 JsonResourceBodyTest() {
28 super(JsonResourceBody::new);
32 void moduleDataTest() throws Exception {
33 final var entryId = NodeIdentifierWithPredicates.of(LST11,
34 Map.of(KEYVALUE111, "value1", KEYVALUE112, "value2"));
36 assertEquals(Builders.containerBuilder()
37 .withNodeIdentifier(CONT_NID)
38 .withChild(Builders.containerBuilder()
39 .withNodeIdentifier(CONT1_NID)
40 .withChild(Builders.mapBuilder()
41 .withNodeIdentifier(new NodeIdentifier(LST11))
42 .withChild(Builders.mapEntryBuilder()
43 .withNodeIdentifier(entryId)
44 .withChild(ImmutableNodes.leafNode(KEYVALUE111, "value1"))
45 .withChild(ImmutableNodes.leafNode(KEYVALUE112, "value2"))
46 .withChild(ImmutableNodes.leafNode(LF111, YangInstanceIdentifier.of(CONT_NID, CONT1_NID,
47 new NodeIdentifier(LST11), entryId, LF112_NID)))
48 .withChild(ImmutableNodes.leafNode(LF112_NID, "lf112 value"))
52 .build(), parse("instance-identifier-module:cont", """
54 "instance-identifier-module:cont": {
56 "augment-module:lst11": [
58 "keyvalue111":"value1",
59 "keyvalue112":"value2",
60 "augment-augment-module:lf111": "/instance-identifier-module:cont/cont1\
61 /augment-module:lst11[keyvalue111=\\"value1\\"][keyvalue112=\\"value2\\"]/augment-augment-module:lf112",
62 "augment-augment-module:lf112": "lf112 value"
72 void moduleSubContainerDataPutTest() throws Exception {
73 assertEquals(Builders.containerBuilder()
74 .withNodeIdentifier(CONT1_NID)
75 .withChild(ImmutableNodes.leafNode(LF11, YangInstanceIdentifier.of(CONT_NID, CONT1_NID,
76 new NodeIdentifier(LFLST11), new NodeWithValue<>(LFLST11, "lflst11_1"))))
77 .build(), parse("instance-identifier-module:cont/cont1", """
79 "instance-identifier-module:cont1": {
80 "augment-module-leaf-list:lf11" : "/instance-identifier-module:cont\
81 /instance-identifier-module:cont1/augment-module-leaf-list:lflst11[.=\\"lflst11_1\\"]"
87 void testRangeViolation() {
88 assertRangeViolation(() -> parse("netconf786:foo", """
97 void testMismatchedInput() {
98 final var error = assertError(() -> parse("base:cont", """
100 "ietf-restconf:restconf-state" : {
104 Payload name ((urn:ietf:params:xml:ns:yang:ietf-restconf-monitoring?revision=2017-01-26)restconf-state) is \
105 different from identifier name ((ns?revision=2016-02-28)cont)""", error.getErrorMessage());
106 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
107 assertEquals(ErrorTag.MALFORMED_MESSAGE, error.getErrorTag());
111 void testMissingKeys() {
112 final var ex = assertThrows(IllegalArgumentException.class,
113 () -> parse("nested-module:depth1-cont/depth2-list2=one,two", """
116 "depth3-lf1-key" : "one"
119 assertNull(ex.getMessage());
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(Builders.mapEntryBuilder()
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"