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.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;
47 public class XmlBodyReaderTest extends AbstractBodyReaderTest {
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"));
54 public XmlBodyReaderTest() throws Exception {
56 this.xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler);
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));
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 assertTrue(data.getValue().size() == 2);
92 for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
93 switch (child.getNodeType().getLocalName()) {
95 assertEquals("key-value", child.getValue());
99 assertEquals("leaf-value", child.getValue());
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,
118 checkNormalizedNodeContext(returnValue);
119 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
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,
135 checkNormalizedNodeContext(returnValue);
136 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
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,
151 checkNormalizedNodeContext(returnValue);
152 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
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,
171 checkNormalizedNodeContext(returnValue);
172 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
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,
195 checkNormalizedNodeContext(returnValue);
196 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
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));
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.
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,
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());
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.
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,
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());
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
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");
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());