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
9 package org.opendaylight.controller.sal.rest.impl.test.providers;
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 javax.ws.rs.core.MediaType;
22 import org.junit.Assert;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
26 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
27 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
28 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
29 import org.opendaylight.restconf.common.errors.RestconfError;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.QNameModule;
32 import org.opendaylight.yangtools.yang.common.Revision;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
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.DataNodeContainer;
40 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.Module;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45 public class TestXmlBodyReader extends AbstractBodyReaderTest {
47 private final XmlNormalizedNodeBodyReader xmlBodyReader;
48 private static SchemaContext schemaContext;
49 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
50 URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
52 public TestXmlBodyReader() {
53 super(schemaContext, null);
54 this.xmlBodyReader = new XmlNormalizedNodeBodyReader(controllerContext);
58 protected MediaType getMediaType() {
59 return new MediaType(MediaType.APPLICATION_XML, null);
63 public static void initialization() throws Exception {
64 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
65 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
66 testFiles.addAll(TestRestconfUtils.loadFiles("/foo-xml-test/yang"));
67 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
71 public void putXmlTest() throws Exception {
72 runXmlTest(false, "foo:top-level-list/key-value");
76 public void postXmlTest() throws Exception {
80 private void runXmlTest(final boolean isPost, final String path) throws Exception {
81 mockBodyReader(path, xmlBodyReader, isPost);
82 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream("/foo-xml-test/foo.xml");
83 final NormalizedNodeContext nnc = xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
86 assertTrue(nnc.getData() instanceof MapEntryNode);
87 final MapEntryNode data = (MapEntryNode) nnc.getData();
88 assertTrue(data.getValue().size() == 2);
89 for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
90 switch (child.getNodeType().getLocalName()) {
92 assertEquals("key-value", child.getValue());
96 assertEquals("leaf-value", child.getValue());
105 public void moduleDataTest() throws Exception {
106 final DataSchemaNode dataSchemaNode =
107 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
108 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
109 final String uri = "instance-identifier-module:cont";
110 mockBodyReader(uri, this.xmlBodyReader, false);
111 final InputStream inputStream = TestXmlBodyReader.class
112 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
113 final NormalizedNodeContext returnValue = this.xmlBodyReader
114 .readFrom(null, null, null, this.mediaType, null, inputStream);
115 checkNormalizedNodeContext(returnValue);
116 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
120 public void moduleSubContainerDataPutTest() throws Exception {
121 final DataSchemaNode dataSchemaNode =
122 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
123 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
124 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
125 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
126 final String uri = "instance-identifier-module:cont/cont1";
127 mockBodyReader(uri, this.xmlBodyReader, false);
128 final InputStream inputStream = TestXmlBodyReader.class
129 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
130 final NormalizedNodeContext returnValue = this.xmlBodyReader
131 .readFrom(null, null, null, this.mediaType, null, inputStream);
132 checkNormalizedNodeContext(returnValue);
133 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
137 public void moduleSubContainerDataPostTest() throws Exception {
138 final DataSchemaNode dataSchemaNode =
139 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
140 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
141 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
142 final String uri = "instance-identifier-module:cont";
143 mockBodyReader(uri, this.xmlBodyReader, true);
144 final InputStream inputStream = TestXmlBodyReader.class
145 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
146 final NormalizedNodeContext returnValue = this.xmlBodyReader
147 .readFrom(null, null, null, this.mediaType, null, inputStream);
148 checkNormalizedNodeContext(returnValue);
149 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
153 public void moduleSubContainerAugmentDataPostTest() throws Exception {
154 final DataSchemaNode dataSchemaNode =
155 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
156 final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
157 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
158 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
159 Sets.newHashSet(contAugmentQName));
160 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
161 .node(augII).node(contAugmentQName);
162 final String uri = "instance-identifier-module:cont";
163 mockBodyReader(uri, this.xmlBodyReader, true);
164 final InputStream inputStream = TestXmlBodyReader.class
165 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
166 final NormalizedNodeContext returnValue = this.xmlBodyReader
167 .readFrom(null, null, null, this.mediaType, null, inputStream);
168 checkNormalizedNodeContext(returnValue);
169 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
173 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
174 final DataSchemaNode dataSchemaNode =
175 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
176 final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
177 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
178 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
179 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
180 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
181 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
182 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
183 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
184 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
185 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
186 .node(containerQName);
187 final String uri = "instance-identifier-module:cont";
188 mockBodyReader(uri, this.xmlBodyReader, true);
189 final InputStream inputStream = TestXmlBodyReader.class
190 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
191 final NormalizedNodeContext returnValue = this.xmlBodyReader
192 .readFrom(null, null, null, this.mediaType, null, inputStream);
193 checkNormalizedNodeContext(returnValue);
194 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
198 public void rpcModuleInputTest() throws Exception {
199 final String uri = "invoke-rpc-module:rpc-test";
200 mockBodyReader(uri, this.xmlBodyReader, true);
201 final InputStream inputStream = TestXmlBodyReader.class
202 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
203 final NormalizedNodeContext returnValue = this.xmlBodyReader
204 .readFrom(null, null, null, this.mediaType, null, inputStream);
205 checkNormalizedNodeContext(returnValue);
206 final ContainerNode contNode = (ContainerNode) returnValue.getData();
207 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
208 final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode
209 .getChild(yangCont.getLastPathArgument());
210 assertTrue(contDataNodePotential.isPresent());
211 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
212 final YangInstanceIdentifier yangLeaf =
213 YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
214 final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(
215 yangLeaf.getLastPathArgument());
216 assertTrue(leafDataNode.isPresent());
217 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
220 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
221 final NormalizedNodeContext nnContext) {
222 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
225 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
226 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
227 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
228 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
229 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
233 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
234 * used to distinguish between them to find correct one. Check if container was found not only according to its name
235 * but also by correct namespace used in payload.
238 public void findFooContainerUsingNamespaceTest() throws Exception {
239 mockBodyReader("", this.xmlBodyReader, true);
240 final InputStream inputStream = TestXmlBodyReader.class
241 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
242 final NormalizedNodeContext returnValue = this.xmlBodyReader
243 .readFrom(null, null, null, this.mediaType, null, inputStream);
245 // check return value
246 checkNormalizedNodeContext(returnValue);
247 // check if container was found both according to its name and namespace
248 assertEquals("Not correct container found, name was ignored",
249 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
250 assertEquals("Not correct container found, namespace was ignored",
251 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
255 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
256 * used to distinguish between them to find correct one. Check if container was found not only according to its name
257 * but also by correct namespace used in payload.
260 public void findBarContainerUsingNamespaceTest() throws Exception {
261 mockBodyReader("", this.xmlBodyReader, true);
262 final InputStream inputStream = TestXmlBodyReader.class
263 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
264 final NormalizedNodeContext returnValue = this.xmlBodyReader
265 .readFrom(null, null, null, this.mediaType, null, inputStream);
267 // check return value
268 checkNormalizedNodeContext(returnValue);
269 // check if container was found both according to its name and namespace
270 assertEquals("Not correct container found, name was ignored",
271 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
272 assertEquals("Not correct container found, namespace was ignored",
273 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
277 * Test PUT operation when message root element is not the same as the last element in request URI.
278 * PUT operation message should always start with schema node from URI otherwise exception should be
282 public void wrongRootElementTest() throws Exception {
283 mockBodyReader("instance-identifier-module:cont", this.xmlBodyReader, false);
284 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
285 "/instanceidentifier/xml/bug7933.xml");
287 this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
288 Assert.fail("Test should fail due to malformed PUT operation message");
289 } catch (final RestconfDocumentedException exception) {
290 final RestconfError restconfError = exception.getErrors().get(0);
291 Assert.assertEquals(RestconfError.ErrorType.PROTOCOL, restconfError.getErrorType());
292 Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());