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;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
18 import com.google.common.collect.Sets;
20 import java.io.InputStream;
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;
48 public class XmlBodyReaderTest extends AbstractBodyReaderTest {
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"));
55 public XmlBodyReaderTest() throws Exception {
56 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
60 protected MediaType getMediaType() {
61 return new MediaType(MediaType.APPLICATION_XML, null);
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);
75 public void putXmlTest() throws Exception {
76 runXmlTest(false, "foo:top-level-list=key-value");
80 public void postXmlTest() throws Exception {
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);
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()) {
96 assertEquals("key-value", child.getValue());
100 assertEquals("leaf-value", child.getValue());
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,
119 checkNormalizedNodeContext(returnValue);
120 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
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,
136 checkNormalizedNodeContext(returnValue);
137 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
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,
152 checkNormalizedNodeContext(returnValue);
153 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
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,
172 checkNormalizedNodeContext(returnValue);
173 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
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,
196 checkNormalizedNodeContext(returnValue);
197 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
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));
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.
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,
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());
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.
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,
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());
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
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");
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());