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
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.test;
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;
16 import com.google.common.collect.Sets;
18 import java.io.InputStream;
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;
46 public class XmlBodyReaderTest extends AbstractBodyReaderTest {
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"));
53 public XmlBodyReaderTest() throws Exception {
55 this.xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler, mountPointServiceHandler);
59 protected MediaType getMediaType() {
60 return new MediaType(MediaType.APPLICATION_XML, null);
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);
72 public void putXmlTest() throws Exception {
73 runXmlTest(false, "foo:top-level-list=key-value");
77 public void postXmlTest() throws Exception {
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);
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()) {
93 assertEquals("key-value", child.getValue());
97 assertEquals("leaf-value", child.getValue());
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,
116 checkNormalizedNodeContext(returnValue);
117 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
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,
133 checkNormalizedNodeContext(returnValue);
134 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
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,
149 checkNormalizedNodeContext(returnValue);
150 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
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,
167 checkNormalizedNodeContext(returnValue);
168 assertTrue(returnValue.getInstanceIdentifierContext().getSchemaNode() instanceof ActionDefinition);
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,
187 checkNormalizedNodeContext(returnValue);
188 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
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,
211 checkNormalizedNodeContext(returnValue);
212 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
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));
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.
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,
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());
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.
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,
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());
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
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");
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());