f1c3dcb122854e627b42b6720606df694e7a8cc4
[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 import static org.junit.jupiter.api.Assertions.assertNull;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
13
14 import java.util.Map;
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;
25
26 class JsonResourceBodyTest extends AbstractResourceBodyTest {
27     JsonResourceBodyTest() {
28         super(JsonResourceBody::new);
29     }
30
31     @Test
32     void moduleDataTest() throws Exception {
33         final var entryId = NodeIdentifierWithPredicates.of(LST11,
34             Map.of(KEYVALUE111, "value1", KEYVALUE112, "value2"));
35
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"))
49                         .build())
50                     .build())
51                 .build())
52             .build(), parse("instance-identifier-module:cont", """
53                 {
54                   "instance-identifier-module:cont": {
55                     "cont1": {
56                       "augment-module:lst11": [
57                         {
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"
63                         }
64                       ]
65                     }
66                   }
67                 }"""));
68
69     }
70
71     @Test
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", """
78                 {
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\\"]"
82                   }
83                 }"""));
84     }
85
86     @Test
87     void testRangeViolation() {
88         assertRangeViolation(() -> parse("netconf786:foo", """
89             {
90               "netconf786:foo": {
91                 "bar": 100
92               }
93             }"""));
94     }
95
96     @Test
97     void testMismatchedInput() {
98         final var error = assertError(() -> parse("base:cont", """
99             {
100               "ietf-restconf:restconf-state" : {
101               }
102             }"""));
103         assertEquals("""
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());
108     }
109
110     @Test
111     void testMissingKeys() {
112         final var ex = assertThrows(IllegalArgumentException.class,
113             () -> parse("nested-module:depth1-cont/depth2-list2=one,two", """
114                 {
115                   "depth2-list2" : {
116                     "depth3-lf1-key" : "one"
117                   }
118                 }"""));
119         assertNull(ex.getMessage());
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(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", """
133             {
134               "depth2-list2" : {
135                 "depth3-lf1-key" : "one",
136                 "depth3-lf2-key" : "two"
137               }
138             }"""));
139     }
140 }