4dfd51254ddbca5f53e7b0b08507694d3209a522
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / databind / JsonResourceBodyTest.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.databind;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11
12 import java.util.Map;
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;
22
23 class JsonResourceBodyTest extends AbstractResourceBodyTest {
24     JsonResourceBodyTest() {
25         super(JsonResourceBody::new);
26     }
27
28     @Test
29     void moduleDataTest() throws Exception {
30         final var entryId = NodeIdentifierWithPredicates.of(LST11,
31             Map.of(KEYVALUE111, "value1", KEYVALUE112, "value2"));
32
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"))
46                         .build())
47                     .build())
48                 .build())
49             .build(), parse("instance-identifier-module:cont", """
50                 {
51                   "instance-identifier-module:cont": {
52                     "cont1": {
53                       "augment-module:lst11": [
54                         {
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"
60                         }
61                       ]
62                     }
63                   }
64                 }"""));
65
66     }
67
68     @Test
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", """
75                 {
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\\"]"
79                   }
80                 }"""));
81     }
82
83     @Test
84     void testRangeViolation() {
85         assertRangeViolation(() -> parse("netconf786:foo", """
86             {
87               "netconf786:foo": {
88                 "bar": 100
89               }
90             }"""));
91     }
92
93     @Test
94     void testMismatchedInput() {
95         final var error = assertError(() -> parse("base:cont", """
96             {
97               "ietf-restconf:restconf-state" : {
98               }
99             }"""));
100         assertEquals("""
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());
105     }
106
107     @Test
108     void testMissingKeys() {
109         final var error = assertError(() -> parse("nested-module:depth1-cont/depth2-list2=one,two", """
110                 {
111                   "depth2-list2" : {
112                     "depth3-lf1-key" : "one"
113                   }
114                 }"""));
115         assertEquals("""
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());
120     }
121
122     @Test
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");
126
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", """
133             {
134               "depth2-list2" : {
135                 "depth3-lf1-key" : "one",
136                 "depth3-lf2-key" : "two"
137               }
138             }"""));
139     }
140 }