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;
16 import java.io.InputStream;
17 import java.util.Collection;
18 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.AugmentationIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
42 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
43 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
45 import org.opendaylight.yangtools.yang.model.api.Module;
46 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
48 public class TestXmlBodyReader extends AbstractBodyReaderTest {
50 private final XmlNormalizedNodeBodyReader xmlBodyReader;
51 private static EffectiveModelContext schemaContext;
52 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
53 XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
55 public TestXmlBodyReader() {
56 super(schemaContext, null);
57 xmlBodyReader = new XmlNormalizedNodeBodyReader(controllerContext);
61 protected MediaType getMediaType() {
62 return new MediaType(MediaType.APPLICATION_XML, null);
66 public static void initialization() throws Exception {
67 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
68 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
69 testFiles.addAll(TestRestconfUtils.loadFiles("/foo-xml-test/yang"));
70 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
74 public void putXmlTest() throws Exception {
75 runXmlTest(false, "foo:top-level-list/key-value");
79 public void postXmlTest() throws Exception {
83 private void runXmlTest(final boolean isPost, final String path) throws Exception {
84 mockBodyReader(path, xmlBodyReader, isPost);
85 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream("/foo-xml-test/foo.xml");
86 final NormalizedNodeContext nnc = xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
89 assertTrue(nnc.getData() instanceof MapEntryNode);
90 final MapEntryNode data = (MapEntryNode) nnc.getData();
91 assertEquals(2, data.size());
92 for (final DataContainerChild child : data.body()) {
93 switch (child.getIdentifier().getNodeType().getLocalName()) {
95 assertEquals("key-value", child.body());
99 assertEquals("leaf-value", child.body());
108 public void moduleDataTest() throws Exception {
109 final DataSchemaNode dataSchemaNode =
110 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
111 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
112 final String uri = "instance-identifier-module:cont";
113 mockBodyReader(uri, xmlBodyReader, false);
114 final InputStream inputStream = TestXmlBodyReader.class
115 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
116 final NormalizedNodeContext returnValue = xmlBodyReader
117 .readFrom(null, null, null, mediaType, null, inputStream);
118 checkNormalizedNodeContext(returnValue);
119 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
123 public void moduleSubContainerDataPutTest() throws Exception {
124 final DataSchemaNode dataSchemaNode =
125 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
126 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
127 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
128 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
129 final String uri = "instance-identifier-module:cont/cont1";
130 mockBodyReader(uri, xmlBodyReader, false);
131 final InputStream inputStream = TestXmlBodyReader.class
132 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
133 final NormalizedNodeContext returnValue = xmlBodyReader
134 .readFrom(null, null, null, mediaType, null, inputStream);
135 checkNormalizedNodeContext(returnValue);
136 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
140 public void moduleSubContainerDataPostTest() throws Exception {
141 final DataSchemaNode dataSchemaNode =
142 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
143 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
144 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
145 final String uri = "instance-identifier-module:cont";
146 mockBodyReader(uri, xmlBodyReader, true);
147 final InputStream inputStream = TestXmlBodyReader.class
148 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
149 final NormalizedNodeContext returnValue = xmlBodyReader
150 .readFrom(null, null, null, mediaType, null, inputStream);
151 checkNormalizedNodeContext(returnValue);
152 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
156 public void moduleSubContainerAugmentDataPostTest() throws Exception {
157 final DataSchemaNode dataSchemaNode =
158 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
159 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
160 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
161 final AugmentationIdentifier augII = new AugmentationIdentifier(Set.of(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, xmlBodyReader, true);
166 final InputStream inputStream = TestXmlBodyReader.class
167 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
168 final NormalizedNodeContext returnValue = xmlBodyReader
169 .readFrom(null, null, null, 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 YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
182 .node(new AugmentationIdentifier(Set.of(augmentChoice1QName)))
183 .node(augmentChoice1QName)
184 // FIXME: DataSchemaTreeNode intepretation seems to have a bug
185 //.node(new AugmentationIdentifier(Set.of(augmentChoice2QName)))
186 .node(augmentChoice2QName)
187 .node(QName.create(augmentChoice1QName, "case-choice-case-container1"));
188 final String uri = "instance-identifier-module:cont";
189 mockBodyReader(uri, xmlBodyReader, true);
190 final InputStream inputStream = TestXmlBodyReader.class
191 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
192 final NormalizedNodeContext returnValue = xmlBodyReader
193 .readFrom(null, null, null, 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, xmlBodyReader, true);
202 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
203 final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null, null, null, mediaType, null,
205 checkNormalizedNodeContextRpc(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("", xmlBodyReader, true);
232 final InputStream inputStream = TestXmlBodyReader.class
233 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
234 final NormalizedNodeContext returnValue = xmlBodyReader
235 .readFrom(null, null, null, 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("", xmlBodyReader, true);
254 final InputStream inputStream = TestXmlBodyReader.class
255 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
256 final NormalizedNodeContext returnValue = xmlBodyReader
257 .readFrom(null, null, null, 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", xmlBodyReader, false);
276 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
277 "/instanceidentifier/xml/bug7933.xml");
279 xmlBodyReader.readFrom(null, null, null, 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 assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
284 assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());