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