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