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