5efba38bb415039425e03937eda151df3a9f47be
[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.impl.schema.Builders;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23
24 class JsonResourceBodyTest extends AbstractResourceBodyTest {
25     JsonResourceBodyTest() {
26         super(JsonResourceBody::new);
27     }
28
29     @Test
30     void moduleDataTest() throws Exception {
31         final var entryId = NodeIdentifierWithPredicates.of(LST11,
32             Map.of(KEYVALUE111, "value1", KEYVALUE112, "value2"));
33
34         assertEquals(Builders.containerBuilder()
35             .withNodeIdentifier(CONT_NID)
36             .withChild(Builders.containerBuilder()
37                 .withNodeIdentifier(CONT1_NID)
38                 .withChild(Builders.mapBuilder()
39                     .withNodeIdentifier(new NodeIdentifier(LST11))
40                     .withChild(Builders.mapEntryBuilder()
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"))
47                         .build())
48                     .build())
49                 .build())
50             .build(), parse("instance-identifier-module:cont", """
51                 {
52                   "instance-identifier-module:cont": {
53                     "cont1": {
54                       "augment-module:lst11": [
55                         {
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"
61                         }
62                       ]
63                     }
64                   }
65                 }"""));
66
67     }
68
69     @Test
70     void moduleSubContainerDataPutTest() throws Exception {
71         assertEquals(Builders.containerBuilder()
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", """
76                 {
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\\"]"
80                   }
81                 }"""));
82     }
83
84     @Test
85     void testRangeViolation() {
86         assertRangeViolation(() -> parse("netconf786:foo", """
87             {
88               "netconf786:foo": {
89                 "bar": 100
90               }
91             }"""));
92     }
93
94     @Test
95     void testMismatchedInput() {
96         final var error = assertError(() -> parse("base:cont", """
97             {
98               "ietf-restconf:restconf-state" : {
99               }
100             }"""));
101         assertEquals("""
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());
106     }
107
108     @Test
109     void testMissingKeys() {
110         final var error = assertError(() -> parse("nested-module:depth1-cont/depth2-list2=one,two", """
111                 {
112                   "depth2-list2" : {
113                     "depth3-lf1-key" : "one"
114                   }
115                 }"""));
116         assertEquals("""
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());
121     }
122
123     @Test
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");
127
128         assertEquals(Builders.mapEntryBuilder()
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", """
134             {
135               "depth2-list2" : {
136                 "depth3-lf1-key" : "one",
137                 "depth3-lf2-key" : "two"
138               }
139             }"""));
140     }
141 }