Use SchemaContextHandler non-statically
[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.jersey.providers.XmlNormalizedNodeBodyReader;
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.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
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.DataNodeContainer;
42 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.Module;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
46
47 public class XmlBodyReaderTest extends AbstractBodyReaderTest {
48
49     private final XmlNormalizedNodeBodyReader xmlBodyReader;
50     private static SchemaContext schemaContext;
51     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
52         URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
53
54     public XmlBodyReaderTest() throws Exception {
55         super(schemaContext);
56         this.xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler);
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     }
72
73     @Test
74     public void putXmlTest() throws Exception {
75         runXmlTest(false, "foo:top-level-list=key-value");
76     }
77
78     @Test
79     public void postXmlTest() throws Exception {
80         runXmlTest(true, "");
81     }
82
83     private void runXmlTest(final boolean isPost, final String path) throws Exception {
84         mockBodyReader(path, xmlBodyReader, isPost);
85         final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream("/foo-xml-test/foo.xml");
86         final NormalizedNodeContext nnc = xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
87         assertNotNull(nnc);
88
89         assertTrue(nnc.getData() instanceof MapEntryNode);
90         final MapEntryNode data = (MapEntryNode) nnc.getData();
91         assertTrue(data.getValue().size() == 2);
92         for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
93             switch (child.getNodeType().getLocalName()) {
94                 case "key-leaf":
95                     assertEquals("key-value", child.getValue());
96                     break;
97
98                 case "ordinary-leaf":
99                     assertEquals("leaf-value", child.getValue());
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 moduleSubContainerAugmentDataPostTest() throws Exception {
157         final DataSchemaNode dataSchemaNode = schemaContext
158                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
159         final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
160         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
161         final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
162                 Sets.newHashSet(contAugmentQName));
163         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
164                 .node(contAugmentQName);
165         final String uri = "instance-identifier-module:cont";
166         mockBodyReader(uri, this.xmlBodyReader, true);
167         final InputStream inputStream = XmlBodyReaderTest.class
168                 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
169         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
170                 inputStream);
171         checkNormalizedNodeContext(returnValue);
172         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
173     }
174
175     @Test
176     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
177         final DataSchemaNode dataSchemaNode = schemaContext
178                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
179         final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
180         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
181         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
182         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
183         final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
184                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
185         final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
186                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
187         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
188                 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
189         final String uri = "instance-identifier-module:cont";
190         mockBodyReader(uri, this.xmlBodyReader, true);
191         final InputStream inputStream = XmlBodyReaderTest.class
192                 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
193         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
194                 inputStream);
195         checkNormalizedNodeContext(returnValue);
196         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
197     }
198
199     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
200             final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
201         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
202         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
203         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
204     }
205
206     /**
207      * Test when container with the same name is placed in two modules
208      * (foo-module and bar-module). Namespace must be used to distinguish
209      * between them to find correct one. Check if container was found not only
210      * according to its name but also by correct namespace used in payload.
211      */
212     @Test
213     public void findFooContainerUsingNamespaceTest() throws Exception {
214         mockBodyReader("", this.xmlBodyReader, true);
215         final InputStream inputStream = XmlBodyReaderTest.class
216                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
217         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
218                 inputStream);
219
220         // check return value
221         checkNormalizedNodeContext(returnValue);
222         // check if container was found both according to its name and namespace
223         assertEquals("Not correct container found, name was ignored", "foo-bar-container",
224                 returnValue.getData().getNodeType().getLocalName());
225         assertEquals("Not correct container found, namespace was ignored", "foo:module",
226                 returnValue.getData().getNodeType().getNamespace().toString());
227     }
228
229     /**
230      * Test when container with the same name is placed in two modules
231      * (foo-module and bar-module). Namespace must be used to distinguish
232      * between them to find correct one. Check if container was found not only
233      * according to its name but also by correct namespace used in payload.
234      */
235     @Test
236     public void findBarContainerUsingNamespaceTest() throws Exception {
237         mockBodyReader("", this.xmlBodyReader, true);
238         final InputStream inputStream = XmlBodyReaderTest.class
239                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
240         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
241                 inputStream);
242
243         // check return value
244         checkNormalizedNodeContext(returnValue);
245         // check if container was found both according to its name and namespace
246         assertEquals("Not correct container found, name was ignored", "foo-bar-container",
247                 returnValue.getData().getNodeType().getLocalName());
248         assertEquals("Not correct container found, namespace was ignored", "bar:module",
249                 returnValue.getData().getNodeType().getNamespace().toString());
250     }
251
252     /**
253      * Test PUT operation when message root element is not the same as the last element in request URI.
254      * PUT operation message should always start with schema node from URI otherwise exception should be
255      * thrown.
256      */
257     @Test
258     public void wrongRootElementTest() throws Exception {
259         mockBodyReader("instance-identifier-module:cont", this.xmlBodyReader, false);
260         final InputStream inputStream =
261                 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/bug7933.xml");
262         try {
263             this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
264             Assert.fail("Test should fail due to malformed PUT operation message");
265         } catch (final RestconfDocumentedException exception) {
266             final RestconfError restconfError = exception.getErrors().get(0);
267             Assert.assertEquals(RestconfError.ErrorType.PROTOCOL, restconfError.getErrorType());
268             Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
269         }
270     }
271
272 }