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