2 * Copyright (c) 2015 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;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
15 import java.io.InputStream;
16 import java.util.Collection;
17 import java.util.Optional;
18 import javax.ws.rs.core.MediaType;
19 import org.junit.Assert;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
23 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
24 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
25 import org.opendaylight.restconf.common.errors.RestconfError;
26 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
27 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
28 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.QNameModule;
31 import org.opendaylight.yangtools.yang.common.Revision;
32 import org.opendaylight.yangtools.yang.common.XMLNamespace;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
37 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
38 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42 public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
43 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
44 XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
46 private static EffectiveModelContext schemaContext;
48 private final XmlNormalizedNodeBodyReader xmlBodyReader;
50 public XmlBodyReaderMountPointTest() throws Exception {
52 this.xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler, mountPointService);
56 protected MediaType getMediaType() {
57 return new MediaType(MediaType.APPLICATION_XML, null);
61 public static void initialization() throws Exception {
62 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
63 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
64 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
68 public void moduleDataTest() throws Exception {
69 final DataSchemaNode dataSchemaNode = schemaContext
70 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
71 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
72 mockBodyReader(uri, this.xmlBodyReader, false);
73 final InputStream inputStream = XmlBodyReaderMountPointTest.class
74 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
75 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
76 null, null, this.mediaType, null, inputStream);
77 checkMountPointNormalizedNodeContext(returnValue);
78 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
82 public void moduleSubContainerDataPutTest() throws Exception {
83 final DataSchemaNode dataSchemaNode = schemaContext
84 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
85 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
86 mockBodyReader(uri, this.xmlBodyReader, false);
87 final InputStream inputStream = XmlBodyReaderMountPointTest.class
88 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
89 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
90 null, null, this.mediaType, null, inputStream);
91 checkMountPointNormalizedNodeContext(returnValue);
92 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
93 QName.create(dataSchemaNode.getQName(), "cont1"));
97 public void moduleSubContainerDataPostTest() throws Exception {
98 final DataSchemaNode dataSchemaNode = schemaContext
99 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
100 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
101 mockBodyReader(uri, this.xmlBodyReader, true);
102 final InputStream inputStream = XmlBodyReaderMountPointTest.class
103 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
104 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
105 null, null, this.mediaType, null, inputStream);
106 checkMountPointNormalizedNodeContext(returnValue);
107 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
111 public void moduleSubContainerDataPostActionTest() throws Exception {
112 final Optional<DataSchemaNode> dataSchemaNode = schemaContext
113 .findDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
114 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1/reset";
115 mockBodyReader(uri, this.xmlBodyReader, true);
116 final InputStream inputStream = XmlBodyReaderMountPointTest.class
117 .getResourceAsStream("/instanceidentifier/xml/xml_cont_action.xml");
118 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
119 null, null, this.mediaType, null, inputStream);
120 checkMountPointNormalizedNodeContext(returnValue);
121 checkExpectValueNormalizeNodeContext(dataSchemaNode.get(), returnValue);
125 public void rpcModuleInputTest() throws Exception {
126 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
127 mockBodyReader(uri, this.xmlBodyReader, true);
128 final InputStream inputStream = XmlBodyReaderMountPointTest.class
129 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
130 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
131 null, null, this.mediaType, null, inputStream);
132 checkNormalizedNodeContext(returnValue);
133 final ContainerNode contNode = (ContainerNode) returnValue.getData();
134 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(
135 QName.create(contNode.getIdentifier().getNodeType(), "cont"));
136 final Optional<DataContainerChild> contDataNodePotential =
137 contNode.findChildByArg(yangCont.getLastPathArgument());
138 assertTrue(contDataNodePotential.isPresent());
139 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
140 final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(
141 QName.create(contDataNode.getIdentifier().getNodeType(), "lf"));
142 final Optional<DataContainerChild> leafDataNode = contDataNode.findChildByArg(
143 yangLeaf.getLastPathArgument());
144 assertTrue(leafDataNode.isPresent());
145 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().body().toString()));
148 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
149 final NormalizedNodeContext nnContext) {
150 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
153 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
154 final NormalizedNodeContext nnContext, final QName qualifiedName) {
155 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier.of(dataSchemaNode.getQName());
156 final DOMMountPoint mountPoint = nnContext.getInstanceIdentifierContext().getMountPoint();
157 final DataSchemaNode mountDataSchemaNode = modelContext(mountPoint)
158 .getDataChildByName(dataSchemaNode.getQName());
159 assertNotNull(mountDataSchemaNode);
160 if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
161 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode).getDataChildByName(qualifiedName);
162 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent).node(child.getQName()).build();
163 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode().equals(child));
165 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
167 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
171 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
172 * used to distinguish between them to find correct one. Check if container was found not only according to its name
173 * but also by correct namespace used in payload.
176 public void findFooContainerUsingNamespaceTest() throws Exception {
177 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
178 final InputStream inputStream = XmlBodyReaderTest.class
179 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
180 final NormalizedNodeContext returnValue = this.xmlBodyReader
181 .readFrom(null, null, null, this.mediaType, null, inputStream);
183 // check return value
184 checkMountPointNormalizedNodeContext(returnValue);
185 // check if container was found both according to its name and namespace
186 assertEquals("Not correct container found, name was ignored",
187 "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
188 assertEquals("Not correct container found, namespace was ignored",
189 "foo:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
193 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
194 * used to distinguish between them to find correct one. Check if container was found not only according to its name
195 * but also by correct namespace used in payload.
198 public void findBarContainerUsingNamespaceTest() throws Exception {
199 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
200 final InputStream inputStream = XmlBodyReaderTest.class
201 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
202 final NormalizedNodeContext returnValue = this.xmlBodyReader
203 .readFrom(null, null, null, this.mediaType, null, inputStream);
205 // check return value
206 checkMountPointNormalizedNodeContext(returnValue);
207 // check if container was found both according to its name and namespace
208 assertEquals("Not correct container found, name was ignored",
209 "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
210 assertEquals("Not correct container found, namespace was ignored",
211 "bar:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
215 * Test PUT operation when message root element is not the same as the last element in request URI.
216 * PUT operation message should always start with schema node from URI otherwise exception should be
220 public void wrongRootElementTest() throws Exception {
221 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, false);
222 final InputStream inputStream =
223 XmlBodyReaderTest.class.getResourceAsStream(
224 "/instanceidentifier/xml/bug7933.xml");
226 this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
227 Assert.fail("Test should fail due to malformed PUT operation message");
228 } catch (final RestconfDocumentedException exception) {
229 final RestconfError restconfError = exception.getErrors().get(0);
230 Assert.assertEquals(RestconfError.ErrorType.PROTOCOL, restconfError.getErrorType());
231 Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());