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.controller.sal.rest.impl.test.providers;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
15 import com.google.common.collect.Sets;
17 import java.io.InputStream;
18 import java.util.Collection;
19 import java.util.Optional;
20 import javax.ws.rs.core.MediaType;
21 import org.junit.Assert;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
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.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.YangInstanceIdentifier.NodeIdentifier;
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.EffectiveModelContext;
42 import org.opendaylight.yangtools.yang.model.api.Module;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45 public class TestXmlBodyReader extends AbstractBodyReaderTest {
47 private final XmlNormalizedNodeBodyReader xmlBodyReader;
48 private static EffectiveModelContext schemaContext;
49 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
50 XMLNamespace.of("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 assertEquals(2, data.size());
89 for (final DataContainerChild child : data.body()) {
90 switch (child.getIdentifier().getNodeType().getLocalName()) {
92 assertEquals("key-value", child.body());
96 assertEquals("leaf-value", child.body());
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(XMLNamespace.of("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(XMLNamespace.of("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.getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
202 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
204 checkNormalizedNodeContext(returnValue);
205 final ContainerNode contNode = (ContainerNode) returnValue.getData();
206 final Optional<DataContainerChild> contDataNodePotential = contNode.findChildByArg(new NodeIdentifier(
207 QName.create(contNode.getIdentifier().getNodeType(), "cont")));
208 assertTrue(contDataNodePotential.isPresent());
209 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
210 final Optional<DataContainerChild> leafDataNode = contDataNode.findChildByArg(new NodeIdentifier(
211 QName.create(contDataNode.getIdentifier().getNodeType(), "lf")));
212 assertTrue(leafDataNode.isPresent());
213 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().body().toString()));
216 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
217 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
218 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
219 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
220 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
224 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
225 * used to distinguish between them to find correct one. Check if container was found not only according to its name
226 * but also by correct namespace used in payload.
229 public void findFooContainerUsingNamespaceTest() throws Exception {
230 mockBodyReader("", this.xmlBodyReader, true);
231 final InputStream inputStream = TestXmlBodyReader.class
232 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
233 final NormalizedNodeContext returnValue = this.xmlBodyReader
234 .readFrom(null, null, null, this.mediaType, null, inputStream);
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",
240 "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
241 assertEquals("Not correct container found, namespace was ignored",
242 "foo:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
246 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
247 * used to distinguish between them to find correct one. Check if container was found not only according to its name
248 * but also by correct namespace used in payload.
251 public void findBarContainerUsingNamespaceTest() throws Exception {
252 mockBodyReader("", this.xmlBodyReader, true);
253 final InputStream inputStream = TestXmlBodyReader.class
254 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
255 final NormalizedNodeContext returnValue = this.xmlBodyReader
256 .readFrom(null, null, null, this.mediaType, null, inputStream);
258 // check return value
259 checkNormalizedNodeContext(returnValue);
260 // check if container was found both according to its name and namespace
261 assertEquals("Not correct container found, name was ignored",
262 "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
263 assertEquals("Not correct container found, namespace was ignored",
264 "bar:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
268 * Test PUT operation when message root element is not the same as the last element in request URI.
269 * PUT operation message should always start with schema node from URI otherwise exception should be
273 public void wrongRootElementTest() throws Exception {
274 mockBodyReader("instance-identifier-module:cont", this.xmlBodyReader, false);
275 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
276 "/instanceidentifier/xml/bug7933.xml");
278 this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
279 Assert.fail("Test should fail due to malformed PUT operation message");
280 } catch (final RestconfDocumentedException exception) {
281 final RestconfError restconfError = exception.getErrors().get(0);
282 Assert.assertEquals(RestconfError.ErrorType.PROTOCOL, restconfError.getErrorType());
283 Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());