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.NormalizedNodeContext;
26 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
27 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
28 import org.opendaylight.restconf.common.errors.RestconfError;
29 import org.opendaylight.yangtools.yang.common.ErrorTag;
30 import org.opendaylight.yangtools.yang.common.ErrorType;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.common.QNameModule;
33 import org.opendaylight.yangtools.yang.common.Revision;
34 import org.opendaylight.yangtools.yang.common.XMLNamespace;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
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.EffectiveModelContext;
44 import org.opendaylight.yangtools.yang.model.api.Module;
45 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47 public class TestXmlBodyReader extends AbstractBodyReaderTest {
49 private final XmlNormalizedNodeBodyReader xmlBodyReader;
50 private static EffectiveModelContext schemaContext;
51 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
52 XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
54 public TestXmlBodyReader() {
55 super(schemaContext, null);
56 this.xmlBodyReader = new XmlNormalizedNodeBodyReader(controllerContext);
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("/invoke-rpc"));
68 testFiles.addAll(TestRestconfUtils.loadFiles("/foo-xml-test/yang"));
69 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
73 public void putXmlTest() throws Exception {
74 runXmlTest(false, "foo:top-level-list/key-value");
78 public void postXmlTest() throws Exception {
82 private void runXmlTest(final boolean isPost, final String path) throws Exception {
83 mockBodyReader(path, xmlBodyReader, isPost);
84 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream("/foo-xml-test/foo.xml");
85 final NormalizedNodeContext nnc = xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
88 assertTrue(nnc.getData() instanceof MapEntryNode);
89 final MapEntryNode data = (MapEntryNode) nnc.getData();
90 assertEquals(2, data.size());
91 for (final DataContainerChild child : data.body()) {
92 switch (child.getIdentifier().getNodeType().getLocalName()) {
94 assertEquals("key-value", child.body());
98 assertEquals("leaf-value", child.body());
107 public void moduleDataTest() throws Exception {
108 final DataSchemaNode dataSchemaNode =
109 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
110 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
111 final String uri = "instance-identifier-module:cont";
112 mockBodyReader(uri, this.xmlBodyReader, false);
113 final InputStream inputStream = TestXmlBodyReader.class
114 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
115 final NormalizedNodeContext returnValue = this.xmlBodyReader
116 .readFrom(null, null, null, this.mediaType, null, inputStream);
117 checkNormalizedNodeContext(returnValue);
118 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
122 public void moduleSubContainerDataPutTest() throws Exception {
123 final DataSchemaNode dataSchemaNode =
124 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
125 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
126 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
127 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
128 final String uri = "instance-identifier-module:cont/cont1";
129 mockBodyReader(uri, this.xmlBodyReader, false);
130 final InputStream inputStream = TestXmlBodyReader.class
131 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
132 final NormalizedNodeContext returnValue = this.xmlBodyReader
133 .readFrom(null, null, null, this.mediaType, null, inputStream);
134 checkNormalizedNodeContext(returnValue);
135 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
139 public void moduleSubContainerDataPostTest() throws Exception {
140 final DataSchemaNode dataSchemaNode =
141 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
142 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
143 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
144 final String uri = "instance-identifier-module:cont";
145 mockBodyReader(uri, this.xmlBodyReader, true);
146 final InputStream inputStream = TestXmlBodyReader.class
147 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
148 final NormalizedNodeContext returnValue = this.xmlBodyReader
149 .readFrom(null, null, null, this.mediaType, null, inputStream);
150 checkNormalizedNodeContext(returnValue);
151 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
155 public void moduleSubContainerAugmentDataPostTest() throws Exception {
156 final DataSchemaNode dataSchemaNode =
157 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
158 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
159 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
160 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
161 Sets.newHashSet(contAugmentQName));
162 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
163 .node(augII).node(contAugmentQName);
164 final String uri = "instance-identifier-module:cont";
165 mockBodyReader(uri, this.xmlBodyReader, true);
166 final InputStream inputStream = TestXmlBodyReader.class
167 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
168 final NormalizedNodeContext returnValue = this.xmlBodyReader
169 .readFrom(null, null, null, this.mediaType, null, inputStream);
170 checkNormalizedNodeContext(returnValue);
171 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
175 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
176 final DataSchemaNode dataSchemaNode =
177 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
178 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
179 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
180 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
181 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
182 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
183 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
184 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
185 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
186 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
187 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
188 .node(containerQName);
189 final String uri = "instance-identifier-module:cont";
190 mockBodyReader(uri, this.xmlBodyReader, true);
191 final InputStream inputStream = TestXmlBodyReader.class
192 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
193 final NormalizedNodeContext returnValue = this.xmlBodyReader
194 .readFrom(null, null, null, this.mediaType, null, inputStream);
195 checkNormalizedNodeContext(returnValue);
196 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
200 public void rpcModuleInputTest() throws Exception {
201 final String uri = "invoke-rpc-module:rpc-test";
202 mockBodyReader(uri, this.xmlBodyReader, true);
203 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
204 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
206 checkNormalizedNodeContext(returnValue);
207 final ContainerNode contNode = (ContainerNode) returnValue.getData();
208 final Optional<DataContainerChild> contDataNodePotential = contNode.findChildByArg(new NodeIdentifier(
209 QName.create(contNode.getIdentifier().getNodeType(), "cont")));
210 assertTrue(contDataNodePotential.isPresent());
211 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
212 final Optional<DataContainerChild> leafDataNode = contDataNode.findChildByArg(new NodeIdentifier(
213 QName.create(contDataNode.getIdentifier().getNodeType(), "lf")));
214 assertTrue(leafDataNode.isPresent());
215 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().body().toString()));
218 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
219 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
220 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
221 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
222 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
226 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
227 * used to distinguish between them to find correct one. Check if container was found not only according to its name
228 * but also by correct namespace used in payload.
231 public void findFooContainerUsingNamespaceTest() throws Exception {
232 mockBodyReader("", this.xmlBodyReader, true);
233 final InputStream inputStream = TestXmlBodyReader.class
234 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
235 final NormalizedNodeContext returnValue = this.xmlBodyReader
236 .readFrom(null, null, null, this.mediaType, null, inputStream);
238 // check return value
239 checkNormalizedNodeContext(returnValue);
240 // check if container was found both according to its name and namespace
241 assertEquals("Not correct container found, name was ignored",
242 "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
243 assertEquals("Not correct container found, namespace was ignored",
244 "foo:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
248 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
249 * used to distinguish between them to find correct one. Check if container was found not only according to its name
250 * but also by correct namespace used in payload.
253 public void findBarContainerUsingNamespaceTest() throws Exception {
254 mockBodyReader("", this.xmlBodyReader, true);
255 final InputStream inputStream = TestXmlBodyReader.class
256 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
257 final NormalizedNodeContext returnValue = this.xmlBodyReader
258 .readFrom(null, null, null, this.mediaType, null, inputStream);
260 // check return value
261 checkNormalizedNodeContext(returnValue);
262 // check if container was found both according to its name and namespace
263 assertEquals("Not correct container found, name was ignored",
264 "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
265 assertEquals("Not correct container found, namespace was ignored",
266 "bar:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
270 * Test PUT operation when message root element is not the same as the last element in request URI.
271 * PUT operation message should always start with schema node from URI otherwise exception should be
275 public void wrongRootElementTest() throws Exception {
276 mockBodyReader("instance-identifier-module:cont", this.xmlBodyReader, false);
277 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
278 "/instanceidentifier/xml/bug7933.xml");
280 this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
281 Assert.fail("Test should fail due to malformed PUT operation message");
282 } catch (final RestconfDocumentedException exception) {
283 final RestconfError restconfError = exception.getErrors().get(0);
284 assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
285 assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());