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.base.Optional;
17 import com.google.common.collect.Sets;
19 import java.io.InputStream;
21 import java.text.ParseException;
22 import java.util.Collection;
23 import javax.ws.rs.core.MediaType;
24 import org.junit.Assert;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
28 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
29 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
30 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
31 import org.opendaylight.restconf.common.errors.RestconfError;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
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.Module;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47 public class TestXmlBodyReader extends AbstractBodyReaderTest {
49 private final XmlNormalizedNodeBodyReader xmlBodyReader;
50 private static SchemaContext schemaContext;
51 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
53 private static QNameModule initializeInstanceIdentifierModule() {
55 return QNameModule.create(URI.create("instance:identifier:module"),
56 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
57 } catch (final ParseException e) {
62 public TestXmlBodyReader() throws Exception {
64 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
68 protected MediaType getMediaType() {
69 return new MediaType(MediaType.APPLICATION_XML, null);
73 public static void initialization() throws Exception {
74 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
75 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
76 testFiles.addAll(TestRestconfUtils.loadFiles("/foo-xml-test/yang"));
77 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
78 CONTROLLER_CONTEXT.setSchemas(schemaContext);
82 public void putXmlTest() throws Exception {
83 runXmlTest(false, "foo:top-level-list/key-value");
87 public void postXmlTest() throws Exception {
91 private void runXmlTest(final boolean isPost, final String path) throws Exception {
92 mockBodyReader(path, xmlBodyReader, isPost);
93 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream("/foo-xml-test/foo.xml");
94 final NormalizedNodeContext nnc = xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
97 assertTrue(nnc.getData() instanceof MapEntryNode);
98 final MapEntryNode data = (MapEntryNode) nnc.getData();
99 assertTrue(data.getValue().size() == 2);
100 for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
101 switch (child.getNodeType().getLocalName()) {
103 assertEquals("key-value", child.getValue());
106 case "ordinary-leaf":
107 assertEquals("leaf-value", child.getValue());
116 public void moduleDataTest() throws Exception {
117 final DataSchemaNode dataSchemaNode =
118 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
119 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
120 final String uri = "instance-identifier-module:cont";
121 mockBodyReader(uri, this.xmlBodyReader, false);
122 final InputStream inputStream = TestXmlBodyReader.class
123 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
124 final NormalizedNodeContext returnValue = this.xmlBodyReader
125 .readFrom(null, null, null, this.mediaType, null, inputStream);
126 checkNormalizedNodeContext(returnValue);
127 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
131 public void moduleSubContainerDataPutTest() throws Exception {
132 final DataSchemaNode dataSchemaNode =
133 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
134 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
135 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
136 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
137 final String uri = "instance-identifier-module:cont/cont1";
138 mockBodyReader(uri, this.xmlBodyReader, false);
139 final InputStream inputStream = TestXmlBodyReader.class
140 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
141 final NormalizedNodeContext returnValue = this.xmlBodyReader
142 .readFrom(null, null, null, this.mediaType, null, inputStream);
143 checkNormalizedNodeContext(returnValue);
144 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
148 public void moduleSubContainerDataPostTest() throws Exception {
149 final DataSchemaNode dataSchemaNode =
150 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
151 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
152 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
153 final String uri = "instance-identifier-module:cont";
154 mockBodyReader(uri, this.xmlBodyReader, true);
155 final InputStream inputStream = TestXmlBodyReader.class
156 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
157 final NormalizedNodeContext returnValue = this.xmlBodyReader
158 .readFrom(null, null, null, this.mediaType, null, inputStream);
159 checkNormalizedNodeContext(returnValue);
160 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
164 public void moduleSubContainerAugmentDataPostTest() throws Exception {
165 final DataSchemaNode dataSchemaNode =
166 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
167 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
168 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
169 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
170 Sets.newHashSet(contAugmentQName));
171 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
172 .node(augII).node(contAugmentQName);
173 final String uri = "instance-identifier-module:cont";
174 mockBodyReader(uri, this.xmlBodyReader, true);
175 final InputStream inputStream = TestXmlBodyReader.class
176 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
177 final NormalizedNodeContext returnValue = this.xmlBodyReader
178 .readFrom(null, null, null, this.mediaType, null, inputStream);
179 checkNormalizedNodeContext(returnValue);
180 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
184 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
185 final DataSchemaNode dataSchemaNode =
186 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
187 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
188 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
189 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
190 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
191 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
192 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
193 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
194 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
195 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
196 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
197 .node(containerQName);
198 final String uri = "instance-identifier-module:cont";
199 mockBodyReader(uri, this.xmlBodyReader, true);
200 final InputStream inputStream = TestXmlBodyReader.class
201 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
202 final NormalizedNodeContext returnValue = this.xmlBodyReader
203 .readFrom(null, null, null, this.mediaType, null, inputStream);
204 checkNormalizedNodeContext(returnValue);
205 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
209 public void rpcModuleInputTest() throws Exception {
210 final String uri = "invoke-rpc-module:rpc-test";
211 mockBodyReader(uri, this.xmlBodyReader, true);
212 final InputStream inputStream = TestXmlBodyReader.class
213 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
214 final NormalizedNodeContext returnValue = this.xmlBodyReader
215 .readFrom(null, null, null, this.mediaType, null, inputStream);
216 checkNormalizedNodeContext(returnValue);
217 final ContainerNode contNode = (ContainerNode) returnValue.getData();
218 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
219 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(yangCont
220 .getLastPathArgument());
221 assertTrue(contDataNodePotential.isPresent());
222 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
223 final YangInstanceIdentifier yangLeaf =
224 YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
225 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(yangLeaf
226 .getLastPathArgument());
227 assertTrue(leafDataNode.isPresent());
228 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
231 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
232 final NormalizedNodeContext nnContext) {
233 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
236 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
237 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
238 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
239 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
240 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
244 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
245 * used to distinguish between them to find correct one. Check if container was found not only according to its name
246 * but also by correct namespace used in payload.
249 public void findFooContainerUsingNamespaceTest() throws Exception {
250 mockBodyReader("", this.xmlBodyReader, true);
251 final InputStream inputStream = TestXmlBodyReader.class
252 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
253 final NormalizedNodeContext returnValue = this.xmlBodyReader
254 .readFrom(null, null, null, this.mediaType, null, inputStream);
256 // check return value
257 checkNormalizedNodeContext(returnValue);
258 // check if container was found both according to its name and namespace
259 assertEquals("Not correct container found, name was ignored",
260 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
261 assertEquals("Not correct container found, namespace was ignored",
262 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
266 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
267 * used to distinguish between them to find correct one. Check if container was found not only according to its name
268 * but also by correct namespace used in payload.
271 public void findBarContainerUsingNamespaceTest() throws Exception {
272 mockBodyReader("", this.xmlBodyReader, true);
273 final InputStream inputStream = TestXmlBodyReader.class
274 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
275 final NormalizedNodeContext returnValue = this.xmlBodyReader
276 .readFrom(null, null, null, this.mediaType, null, inputStream);
278 // check return value
279 checkNormalizedNodeContext(returnValue);
280 // check if container was found both according to its name and namespace
281 assertEquals("Not correct container found, name was ignored",
282 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
283 assertEquals("Not correct container found, namespace was ignored",
284 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
288 * Test PUT operation when message root element is not the same as the last element in request URI.
289 * PUT operation message should always start with schema node from URI otherwise exception should be
293 public void wrongRootElementTest() throws Exception {
294 mockBodyReader("instance-identifier-module:cont", this.xmlBodyReader, false);
295 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
296 "/instanceidentifier/xml/bug7933.xml");
298 this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
299 Assert.fail("Test should fail due to malformed PUT operation message");
300 } catch (final RestconfDocumentedException exception) {
301 final RestconfError restconfError = exception.getErrors().get(0);
302 Assert.assertEquals(RestconfError.ErrorType.PROTOCOL, restconfError.getErrorType());
303 Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());