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