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