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