90db4c358db9ed7e5a0cf502f3b6241dd6c15a3a
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / test / XmlBodyReaderTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.jersey.providers.test;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThrows;
15 import static org.junit.Assert.fail;
16
17 import com.google.common.collect.Sets;
18 import java.io.ByteArrayInputStream;
19 import java.io.File;
20 import java.io.InputStream;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Collection;
23 import java.util.Optional;
24 import java.util.Set;
25 import javax.ws.rs.core.MediaType;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
29 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
30 import org.opendaylight.restconf.common.errors.RestconfError;
31 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
32 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.XmlNormalizedNodeBodyReader;
33 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
34 import org.opendaylight.yangtools.yang.common.ErrorTag;
35 import org.opendaylight.yangtools.yang.common.ErrorType;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.common.QNameModule;
38 import org.opendaylight.yangtools.yang.common.Revision;
39 import org.opendaylight.yangtools.yang.common.XMLNamespace;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
44 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
45 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
47 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
48 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
49 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
51 import org.opendaylight.yangtools.yang.model.api.Module;
52 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
53
54 public class XmlBodyReaderTest extends AbstractBodyReaderTest {
55     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
56         XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
57     private static final QName TOP_LEVEL_LIST = QName.create("foo", "2017-08-09", "top-level-list");
58
59     private static EffectiveModelContext schemaContext;
60
61     private final XmlNormalizedNodeBodyReader xmlBodyReader;
62
63     public XmlBodyReaderTest() throws Exception {
64         super(schemaContext);
65         xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler, mountPointService);
66     }
67
68     @Override
69     protected MediaType getMediaType() {
70         return new MediaType(MediaType.APPLICATION_XML, null);
71     }
72
73     @BeforeClass
74     public static void initialization() throws Exception {
75         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
76         testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
77         testFiles.addAll(TestRestconfUtils.loadFiles("/foo-xml-test/yang"));
78         schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
79     }
80
81     @Test
82     public void putXmlTest() throws Exception {
83         runXmlTest(false, "foo:top-level-list=key-value");
84     }
85
86     @Test
87     public void postXmlTest() throws Exception {
88         runXmlTest(true, "");
89     }
90
91     private void runXmlTest(final boolean isPost, final String path) throws Exception {
92         mockBodyReader(path, xmlBodyReader, isPost);
93         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
94             XmlBodyReaderTest.class.getResourceAsStream("/foo-xml-test/foo.xml"));
95         assertNotNull(payload);
96
97         final InstanceIdentifierContext iid = payload.getInstanceIdentifierContext();
98         assertEquals(YangInstanceIdentifier.create(
99             new NodeIdentifier(TOP_LEVEL_LIST),
100             NodeIdentifierWithPredicates.of(TOP_LEVEL_LIST, QName.create(TOP_LEVEL_LIST, "key-leaf"), "key-value")),
101             iid.getInstanceIdentifier());
102
103         assertThat(payload.getData(), instanceOf(MapEntryNode.class));
104         final MapEntryNode data = (MapEntryNode) payload.getData();
105         assertEquals(2, data.size());
106         for (final DataContainerChild child : data.body()) {
107             switch (child.getIdentifier().getNodeType().getLocalName()) {
108                 case "key-leaf":
109                     assertEquals("key-value", child.body());
110                     break;
111                 case "ordinary-leaf":
112                     assertEquals("leaf-value", child.body());
113                     break;
114                 default:
115                     fail();
116             }
117         }
118     }
119
120     @Test
121     public void moduleDataTest() throws Exception {
122         final DataSchemaNode dataSchemaNode = schemaContext
123                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
124         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
125         final String uri = "instance-identifier-module:cont";
126         mockBodyReader(uri, xmlBodyReader, false);
127         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
128             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmldata.xml"));
129         checkNormalizedNodePayload(payload);
130         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
131     }
132
133     @Test
134     public void moduleSubContainerDataPutTest() throws Exception {
135         final DataSchemaNode dataSchemaNode = schemaContext
136                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
137         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
138         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
139         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
140         final String uri = "instance-identifier-module:cont/cont1";
141         mockBodyReader(uri, xmlBodyReader, false);
142         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
143             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
144         checkNormalizedNodePayload(payload);
145         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, payload, dataII);
146     }
147
148     @Test
149     public void moduleSubContainerDataPostTest() throws Exception {
150         final DataSchemaNode dataSchemaNode = schemaContext
151                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
152         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
153         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
154         final String uri = "instance-identifier-module:cont";
155         mockBodyReader(uri, xmlBodyReader, true);
156         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
157             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
158         checkNormalizedNodePayload(payload);
159         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
160     }
161
162     @Test
163     public void moduleSubContainerDataPostActionTest() throws Exception {
164         final Optional<DataSchemaNode> dataSchemaNode = schemaContext
165             .findDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
166         final QName cont1QName = QName.create(dataSchemaNode.get().getQName(), "cont1");
167         final QName actionQName = QName.create(dataSchemaNode.get().getQName(), "reset");
168         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.get().getQName())
169             .node(cont1QName).node(actionQName);
170         final String uri = "instance-identifier-module:cont/cont1/reset";
171         mockBodyReader(uri, xmlBodyReader, true);
172         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
173             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_cont_action.xml"));
174         checkNormalizedNodePayload(payload);
175         assertThat(payload.getInstanceIdentifierContext().getSchemaNode(), instanceOf(ActionDefinition.class));
176     }
177
178     @Test
179     public void moduleSubContainerAugmentDataPostTest() throws Exception {
180         final DataSchemaNode dataSchemaNode = schemaContext
181                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
182         final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
183         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
184         final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
185                 Sets.newHashSet(contAugmentQName));
186         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
187                 .node(contAugmentQName);
188         final String uri = "instance-identifier-module:cont";
189         mockBodyReader(uri, xmlBodyReader, true);
190         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
191             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml"));
192         checkNormalizedNodePayload(payload);
193         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
194     }
195
196     @Test
197     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
198         final DataSchemaNode dataSchemaNode = schemaContext
199                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
200         final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
201         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
202         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
203         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
204             .node(new AugmentationIdentifier(Set.of(augmentChoice1QName)))
205             .node(augmentChoice1QName)
206             // FIXME: DataSchemaContextTree bug? case children seem to ignore augments
207             // .node(new AugmentationIdentifier(Set.of(augmentChoice2QName)))
208             .node(augmentChoice2QName)
209             .node(QName.create(augmentChoice1QName, "case-choice-case-container1"));
210         final String uri = "instance-identifier-module:cont";
211         mockBodyReader(uri, xmlBodyReader, true);
212         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
213             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml"));
214         checkNormalizedNodePayload(payload);
215         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
216     }
217
218     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
219             final NormalizedNodePayload nnContext, final YangInstanceIdentifier dataNodeIdent) {
220         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
221         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
222         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
223     }
224
225     /**
226      * Test when container with the same name is placed in two modules
227      * (foo-module and bar-module). Namespace must be used to distinguish
228      * between them to find correct one. Check if container was found not only
229      * according to its name but also by correct namespace used in payload.
230      */
231     @Test
232     public void findFooContainerUsingNamespaceTest() throws Exception {
233         mockBodyReader("", xmlBodyReader, true);
234         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
235             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml"));
236
237         // check return value
238         checkNormalizedNodePayload(payload);
239         // check if container was found both according to its name and namespace
240         final var payloadNodeType = payload.getData().getIdentifier().getNodeType();
241         assertEquals("foo-bar-container", payloadNodeType.getLocalName());
242         assertEquals("foo:module", payloadNodeType.getNamespace().toString());
243     }
244
245     /**
246      * Test when container with the same name is placed in two modules
247      * (foo-module and bar-module). Namespace must be used to distinguish
248      * between them to find correct one. Check if container was found not only
249      * according to its name but also by correct namespace used in payload.
250      */
251     @Test
252     public void findBarContainerUsingNamespaceTest() throws Exception {
253         mockBodyReader("", xmlBodyReader, true);
254         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
255             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml"));
256
257         // check return value
258         checkNormalizedNodePayload(payload);
259         // check if container was found both according to its name and namespace
260         final var payloadNodeType = payload.getData().getIdentifier().getNodeType();
261         assertEquals("foo-bar-container", payloadNodeType.getLocalName());
262         assertEquals("bar:module", payloadNodeType.getNamespace().toString());
263     }
264
265     /**
266      * Test PUT operation when message root element is not the same as the last element in request URI.
267      * PUT operation message should always start with schema node from URI otherwise exception should be
268      * thrown.
269      */
270     @Test
271     public void wrongRootElementTest() throws Exception {
272         mockBodyReader("instance-identifier-module:cont", xmlBodyReader, false);
273         final InputStream inputStream =
274                 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/bug7933.xml");
275
276         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
277             () -> xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
278
279         final RestconfError restconfError = ex.getErrors().get(0);
280         assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
281         assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
282     }
283
284     @Test
285     public void testRangeViolation() throws Exception {
286         mockBodyReader("netconf786:foo", xmlBodyReader, false);
287
288         final InputStream inputStream = new ByteArrayInputStream(
289             "<foo xmlns=\"netconf786\"><bar>100</bar></foo>".getBytes(StandardCharsets.UTF_8));
290
291         assertRangeViolation(() -> xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
292     }
293 }