2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.test;
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;
15 import com.google.common.collect.Sets;
16 import java.io.ByteArrayInputStream;
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.ErrorType;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.common.Revision;
35 import org.opendaylight.yangtools.yang.common.XMLNamespace;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
38 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
40 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
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.EffectiveModelContext;
44 import org.opendaylight.yangtools.yang.model.api.Module;
45 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47 public class XmlBodyReaderTest extends AbstractBodyReaderTest {
48 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
49 XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
51 private static EffectiveModelContext schemaContext;
53 private final XmlNormalizedNodeBodyReader xmlBodyReader;
55 public XmlBodyReaderTest() throws Exception {
57 this.xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler, mountPointService);
61 protected MediaType getMediaType() {
62 return new MediaType(MediaType.APPLICATION_XML, null);
66 public static void initialization() throws Exception {
67 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
68 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
69 testFiles.addAll(TestRestconfUtils.loadFiles("/foo-xml-test/yang"));
70 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
74 public void putXmlTest() throws Exception {
75 runXmlTest(false, "foo:top-level-list=key-value");
79 public void postXmlTest() throws Exception {
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);
89 assertTrue(nnc.getData() instanceof MapEntryNode);
90 final MapEntryNode data = (MapEntryNode) nnc.getData();
91 assertEquals(2, data.size());
92 for (final DataContainerChild child : data.body()) {
93 switch (child.getIdentifier().getNodeType().getLocalName()) {
95 assertEquals("key-value", child.body());
98 assertEquals("leaf-value", child.body());
107 public void moduleDataTest() throws Exception {
108 final DataSchemaNode dataSchemaNode = schemaContext
109 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
110 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
111 final String uri = "instance-identifier-module:cont";
112 mockBodyReader(uri, this.xmlBodyReader, false);
113 final InputStream inputStream = XmlBodyReaderTest.class
114 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
115 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
117 checkNormalizedNodeContext(returnValue);
118 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
122 public void moduleSubContainerDataPutTest() throws Exception {
123 final DataSchemaNode dataSchemaNode = schemaContext
124 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
125 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
126 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
127 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
128 final String uri = "instance-identifier-module:cont/cont1";
129 mockBodyReader(uri, this.xmlBodyReader, false);
130 final InputStream inputStream = XmlBodyReaderTest.class
131 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
132 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
134 checkNormalizedNodeContext(returnValue);
135 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
139 public void moduleSubContainerDataPostTest() throws Exception {
140 final DataSchemaNode dataSchemaNode = schemaContext
141 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
142 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
143 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
144 final String uri = "instance-identifier-module:cont";
145 mockBodyReader(uri, this.xmlBodyReader, true);
146 final InputStream inputStream = XmlBodyReaderTest.class
147 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
148 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
150 checkNormalizedNodeContext(returnValue);
151 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
155 public void moduleSubContainerDataPostActionTest() throws Exception {
156 final Optional<DataSchemaNode> dataSchemaNode = schemaContext
157 .findDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
158 final QName cont1QName = QName.create(dataSchemaNode.get().getQName(), "cont1");
159 final QName actionQName = QName.create(dataSchemaNode.get().getQName(), "reset");
160 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.get().getQName())
161 .node(cont1QName).node(actionQName);
162 final String uri = "instance-identifier-module:cont/cont1/reset";
163 mockBodyReader(uri, this.xmlBodyReader, true);
164 final InputStream inputStream = XmlBodyReaderTest.class
165 .getResourceAsStream("/instanceidentifier/xml/xml_cont_action.xml");
166 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
168 checkNormalizedNodeContext(returnValue);
169 assertTrue(returnValue.getInstanceIdentifierContext().getSchemaNode() instanceof ActionDefinition);
173 public void moduleSubContainerAugmentDataPostTest() throws Exception {
174 final DataSchemaNode dataSchemaNode = schemaContext
175 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
176 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
177 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
178 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
179 Sets.newHashSet(contAugmentQName));
180 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
181 .node(contAugmentQName);
182 final String uri = "instance-identifier-module:cont";
183 mockBodyReader(uri, this.xmlBodyReader, true);
184 final InputStream inputStream = XmlBodyReaderTest.class
185 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
186 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
188 checkNormalizedNodeContext(returnValue);
189 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
193 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
194 final DataSchemaNode dataSchemaNode = schemaContext
195 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
196 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
197 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
198 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
199 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
200 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
201 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
202 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
203 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
204 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
205 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
206 final String uri = "instance-identifier-module:cont";
207 mockBodyReader(uri, this.xmlBodyReader, true);
208 final InputStream inputStream = XmlBodyReaderTest.class
209 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
210 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
212 checkNormalizedNodeContext(returnValue);
213 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
216 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
217 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
218 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
219 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
220 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
224 * Test when container with the same name is placed in two modules
225 * (foo-module and bar-module). Namespace must be used to distinguish
226 * between them to find correct one. Check if container was found not only
227 * according to its name but also by correct namespace used in payload.
230 public void findFooContainerUsingNamespaceTest() throws Exception {
231 mockBodyReader("", this.xmlBodyReader, true);
232 final InputStream inputStream = XmlBodyReaderTest.class
233 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
234 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
237 // check return value
238 checkNormalizedNodeContext(returnValue);
239 // check if container was found both according to its name and namespace
240 assertEquals("Not correct container found, name was ignored", "foo-bar-container",
241 returnValue.getData().getIdentifier().getNodeType().getLocalName());
242 assertEquals("Not correct container found, namespace was ignored", "foo:module",
243 returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
247 * Test when container with the same name is placed in two modules
248 * (foo-module and bar-module). Namespace must be used to distinguish
249 * between them to find correct one. Check if container was found not only
250 * according to its name but also by correct namespace used in payload.
253 public void findBarContainerUsingNamespaceTest() throws Exception {
254 mockBodyReader("", this.xmlBodyReader, true);
255 final InputStream inputStream = XmlBodyReaderTest.class
256 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
257 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
260 // check return value
261 checkNormalizedNodeContext(returnValue);
262 // check if container was found both according to its name and namespace
263 assertEquals("Not correct container found, name was ignored", "foo-bar-container",
264 returnValue.getData().getIdentifier().getNodeType().getLocalName());
265 assertEquals("Not correct container found, namespace was ignored", "bar:module",
266 returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
270 * Test PUT operation when message root element is not the same as the last element in request URI.
271 * PUT operation message should always start with schema node from URI otherwise exception should be
275 public void wrongRootElementTest() throws Exception {
276 mockBodyReader("instance-identifier-module:cont", this.xmlBodyReader, false);
277 final InputStream inputStream =
278 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/bug7933.xml");
280 this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
281 Assert.fail("Test should fail due to malformed PUT operation message");
282 } catch (final RestconfDocumentedException exception) {
283 final RestconfError restconfError = exception.getErrors().get(0);
284 Assert.assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
285 Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
290 public void testRangeViolation() throws Exception {
291 mockBodyReader("netconf786:foo", this.xmlBodyReader, false);
293 final InputStream inputStream = new ByteArrayInputStream(
294 "<foo xmlns=\"netconf786\"><bar>100</bar></foo>".getBytes(StandardCharsets.UTF_8));
296 assertRangeViolation(() -> xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream));