50db4a3db3abdd0b0dae296b9c536867512f6f07
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / databind / XmlResourceBodyTest.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 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doReturn;
15
16 import java.util.Map;
17 import java.util.Optional;
18 import org.junit.jupiter.api.Test;
19 import org.opendaylight.mdsal.dom.api.DOMActionService;
20 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
21 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
22 import org.opendaylight.mdsal.dom.api.DOMRpcService;
23 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
24 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
25 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
26 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
27 import org.opendaylight.yangtools.yang.common.ErrorTag;
28 import org.opendaylight.yangtools.yang.common.ErrorType;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
34 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36
37 class XmlResourceBodyTest extends AbstractResourceBodyTest {
38     private static final QName TOP_LEVEL_LIST = QName.create("foo", "2017-08-09", "top-level-list");
39
40     XmlResourceBodyTest() {
41         super(XmlResourceBody::new);
42     }
43
44     private void mockMount() {
45         doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
46         doReturn(Optional.of(FixedDOMSchemaService.of(IID_SCHEMA))).when(mountPoint)
47             .getService(DOMSchemaService.class);
48         doReturn(Optional.of(dataBroker)).when(mountPoint).getService(DOMDataBroker.class);
49         doReturn(Optional.empty()).when(mountPoint).getService(DOMActionService.class);
50         doReturn(Optional.empty()).when(mountPoint).getService(DOMRpcService.class);
51         doReturn(Optional.empty()).when(mountPoint).getService(DOMMountPointService.class);
52         doReturn(Optional.empty()).when(mountPoint).getService(NetconfDataTreeService.class);
53     }
54
55     /**
56      * Test PUT operation when message root element is not the same as the last element in request URI.
57      * PUT operation message should always start with schema node from URI otherwise exception should be
58      * thrown.
59      */
60     @Test
61     void wrongRootElementTest() {
62         mockMount();
63
64         assertThrowsException("",
65             "Incorrect message root element (instance:identifier:module)cont1, should be "
66                 + "(urn:ietf:params:xml:ns:yang:ietf-restconf)data");
67         assertThrowsException("instance-identifier-module:cont/yang-ext:mount",
68             "Incorrect message root element (instance:identifier:module)cont1, should be "
69                 + "(urn:ietf:params:xml:ns:yang:ietf-restconf)data");
70         assertThrowsException("instance-identifier-module:cont",
71             "Incorrect message root element (instance:identifier:module)cont1, should be "
72                 + "(instance:identifier:module)cont");
73         assertThrowsException("instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont",
74             "Incorrect message root element (instance:identifier:module)cont1, should be "
75                 + "(instance:identifier:module)cont");
76     }
77
78     private void assertThrowsException(final String uriPath, final String expectedErrorMessage) {
79         final var ex = assertThrows(RestconfDocumentedException.class,
80             () -> parse(uriPath, """
81                 <cont1 xmlns="instance:identifier:module"/>"""));
82         final var restconfError = ex.getErrors().get(0);
83         assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
84         assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
85         assertEquals(expectedErrorMessage, restconfError.getErrorMessage());
86     }
87
88     @Test
89     void testRangeViolation() throws Exception {
90         assertRangeViolation(() -> parse("netconf786:foo", """
91             <foo xmlns="netconf786"><bar>100</bar></foo>"""));
92     }
93
94     @Test
95     void putXmlTest() throws Exception {
96         final var keyName = QName.create(TOP_LEVEL_LIST, "key-leaf");
97         assertEquals(Builders.mapEntryBuilder()
98             .withNodeIdentifier(NodeIdentifierWithPredicates.of(TOP_LEVEL_LIST, keyName, "key-value"))
99             .withChild(ImmutableNodes.leafNode(keyName, "key-value"))
100             .withChild(ImmutableNodes.leafNode(QName.create(keyName, "ordinary-leaf"), "leaf-value"))
101             .build(), parse("foo:top-level-list=key-value", """
102                 <top-level-list xmlns="foo">
103                     <key-leaf>key-value</key-leaf>
104                     <ordinary-leaf>leaf-value</ordinary-leaf>
105                 </top-level-list>"""));
106     }
107
108     @Test
109     void moduleDataTest() throws Exception {
110         testModuleData("instance-identifier-module:cont");
111     }
112
113     @Test
114     void moduleDataMountPointTest() throws Exception {
115         mockMount();
116
117         testModuleData("instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont");
118     }
119
120     private void testModuleData(final String uriPath) throws Exception {
121         final var entryId = NodeIdentifierWithPredicates.of(LST11,
122             Map.of(KEYVALUE111, "value1", KEYVALUE112, "value2"));
123
124         assertEquals(Builders.containerBuilder()
125             .withNodeIdentifier(CONT_NID)
126             .withChild(Builders.containerBuilder()
127                 .withNodeIdentifier(CONT1_NID)
128                 .withChild(Builders.mapBuilder()
129                     .withNodeIdentifier(new NodeIdentifier(LST11))
130                     .withChild(Builders.mapEntryBuilder()
131                         .withNodeIdentifier(entryId)
132                         .withChild(ImmutableNodes.leafNode(KEYVALUE111, "value1"))
133                         .withChild(ImmutableNodes.leafNode(KEYVALUE112, "value2"))
134                         .withChild(ImmutableNodes.leafNode(LF111, YangInstanceIdentifier.of(CONT_NID, CONT1_NID,
135                             new NodeIdentifier(LST11), entryId, LF112_NID)))
136                         .withChild(ImmutableNodes.leafNode(LF112_NID, "lf112 value"))
137                         .build())
138                     .build())
139                 .build())
140             .build(), parse(uriPath, """
141                 <cont xmlns="instance:identifier:module">
142                   <cont1>
143                     <lst11 xmlns="augment:module" xmlns:c="augment:augment:module">
144                       <keyvalue111>value1</keyvalue111>
145                       <keyvalue112>value2</keyvalue112>
146                       <lf111 xmlns="augment:augment:module" xmlns:a="instance:identifier:module" \
147                 xmlns:b="augment:module">/a:cont/a:cont1/b:lst11[b:keyvalue111="value1"][b:keyvalue112="value2"]\
148                 /c:lf112</lf111>
149                       <lf112 xmlns="augment:augment:module">lf112 value</lf112>
150                     </lst11>
151                   </cont1>
152                 </cont>"""));
153     }
154
155     @Test
156     void moduleSubContainerDataPutTest() throws Exception {
157         testModuleSubContainerDataPut("instance-identifier-module:cont/cont1");
158     }
159
160     @Test
161     void moduleSubContainerDataPutMountPointTest() throws Exception {
162         mockMount();
163
164         testModuleSubContainerDataPut(
165             "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1");
166     }
167
168     private void testModuleSubContainerDataPut(final String uriPath) throws Exception {
169         assertEquals(Builders.containerBuilder()
170             .withNodeIdentifier(CONT1_NID)
171             .withChild(Builders.leafSetBuilder()
172                 .withNodeIdentifier(new NodeIdentifier(LFLST11))
173                 .withChild(Builders.leafSetEntryBuilder()
174                     .withNodeIdentifier(new NodeWithValue<>(LFLST11, "lflst11_3"))
175                     .withValue("lflst11_3")
176                     .build())
177                 .withChild(Builders.leafSetEntryBuilder()
178                     .withNodeIdentifier(new NodeWithValue<>(LFLST11, "lflst11_1"))
179                     .withValue("lflst11_1")
180                     .build())
181                 .withChild(Builders.leafSetEntryBuilder()
182                     .withNodeIdentifier(new NodeWithValue<>(LFLST11, "lflst11_2"))
183                     .withValue("lflst11_2")
184                     .build())
185                 .build())
186             .withChild(ImmutableNodes.leafNode(LF11, YangInstanceIdentifier.of(CONT_NID, CONT1_NID,
187                 new NodeIdentifier(LFLST11), new NodeWithValue<>(LFLST11, "lflst11_1"))))
188             .build(), parse(uriPath, """
189                 <cont1 xmlns="instance:identifier:module">
190                   <lflst11 xmlns="augment:module:leaf:list">lflst11_1</lflst11>
191                   <lflst11 xmlns="augment:module:leaf:list">lflst11_2</lflst11>
192                   <lflst11 xmlns="augment:module:leaf:list">lflst11_3</lflst11>
193                   <lf11 xmlns:a="instance:identifier:module" xmlns:b="augment:module:leaf:list" \
194                 xmlns="augment:module:leaf:list">/a:cont/a:cont1/b:lflst11[.="lflst11_1"]</lf11>
195                 </cont1>"""));
196     }
197
198     @Test
199     void testMismatchedInput() throws Exception {
200         final var error = assertError(() -> parse("base:cont", """
201             <restconf-state xmlns="urn:ietf:params:xml:ns:yang:ietf-restconf"/>"""));
202         assertEquals("""
203             Incorrect message root element (urn:ietf:params:xml:ns:yang:ietf-restconf)restconf-state, should be \
204             (ns)cont""", error.getErrorMessage());
205         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
206         assertEquals(ErrorTag.MALFORMED_MESSAGE, error.getErrorTag());
207     }
208
209     @Test
210     void testMissingKeys() throws Exception {
211         final var ex = assertThrows(IllegalArgumentException.class,
212             () -> parse("nested-module:depth1-cont/depth2-list2=one,two", """
213                 <depth2-list2 xmlns="urn:nested:module">
214                   <depth3-lf1-key>one</depth3-lf1-key>
215                 </depth2-list2>"""));
216         assertNull(ex.getMessage());
217     }
218 }