2 * Copyright (c) 2016 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.restconf.nb.rfc8040.jersey.providers.test;
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThrows;
15 import static org.junit.Assert.fail;
17 import com.google.common.collect.Sets;
18 import java.io.ByteArrayInputStream;
20 import java.io.InputStream;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Collection;
23 import java.util.Optional;
25 import javax.ws.rs.core.MediaType;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
29 import org.opendaylight.restconf.common.errors.RestconfError;
30 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
31 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.XmlNormalizedNodeBodyReader;
32 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
33 import org.opendaylight.yangtools.yang.common.ErrorTag;
34 import org.opendaylight.yangtools.yang.common.ErrorType;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.common.QNameModule;
37 import org.opendaylight.yangtools.yang.common.Revision;
38 import org.opendaylight.yangtools.yang.common.XMLNamespace;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
42 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
44 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
45 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
46 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
48 import org.opendaylight.yangtools.yang.model.api.Module;
49 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
51 public class XmlBodyReaderTest extends AbstractBodyReaderTest {
52 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
53 XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
55 private static EffectiveModelContext schemaContext;
57 private final XmlNormalizedNodeBodyReader xmlBodyReader;
59 public XmlBodyReaderTest() throws Exception {
61 xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler, mountPointService);
65 protected MediaType getMediaType() {
66 return new MediaType(MediaType.APPLICATION_XML, null);
70 public static void initialization() throws Exception {
71 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
72 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
73 testFiles.addAll(TestRestconfUtils.loadFiles("/foo-xml-test/yang"));
74 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
78 public void putXmlTest() throws Exception {
79 runXmlTest(false, "foo:top-level-list=key-value");
83 public void postXmlTest() throws Exception {
87 private void runXmlTest(final boolean isPost, final String path) throws Exception {
88 mockBodyReader(path, xmlBodyReader, isPost);
89 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
90 XmlBodyReaderTest.class.getResourceAsStream("/foo-xml-test/foo.xml"));
91 assertNotNull(payload);
93 assertThat(payload.getData(), instanceOf(MapEntryNode.class));
94 final MapEntryNode data = (MapEntryNode) payload.getData();
95 assertEquals(2, data.size());
96 for (final DataContainerChild child : data.body()) {
97 switch (child.getIdentifier().getNodeType().getLocalName()) {
99 assertEquals("key-value", child.body());
101 case "ordinary-leaf":
102 assertEquals("leaf-value", child.body());
111 public void moduleDataTest() throws Exception {
112 final DataSchemaNode dataSchemaNode = schemaContext
113 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
114 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
115 final String uri = "instance-identifier-module:cont";
116 mockBodyReader(uri, xmlBodyReader, false);
117 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
118 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmldata.xml"));
119 checkNormalizedNodePayload(payload);
120 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
124 public void moduleSubContainerDataPutTest() throws Exception {
125 final DataSchemaNode dataSchemaNode = schemaContext
126 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
127 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
128 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
129 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
130 final String uri = "instance-identifier-module:cont/cont1";
131 mockBodyReader(uri, xmlBodyReader, false);
132 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
133 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
134 checkNormalizedNodePayload(payload);
135 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, payload, dataII);
139 public void moduleSubContainerDataPostTest() throws Exception {
140 final DataSchemaNode dataSchemaNode = schemaContext
141 .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, xmlBodyReader, true);
146 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
147 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
148 checkNormalizedNodePayload(payload);
149 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
153 public void moduleSubContainerDataPostActionTest() throws Exception {
154 final Optional<DataSchemaNode> dataSchemaNode = schemaContext
155 .findDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
156 final QName cont1QName = QName.create(dataSchemaNode.get().getQName(), "cont1");
157 final QName actionQName = QName.create(dataSchemaNode.get().getQName(), "reset");
158 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.get().getQName())
159 .node(cont1QName).node(actionQName);
160 final String uri = "instance-identifier-module:cont/cont1/reset";
161 mockBodyReader(uri, xmlBodyReader, true);
162 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
163 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_cont_action.xml"));
164 checkNormalizedNodePayload(payload);
165 assertThat(payload.getInstanceIdentifierContext().getSchemaNode(), instanceOf(ActionDefinition.class));
169 public void moduleSubContainerAugmentDataPostTest() throws Exception {
170 final DataSchemaNode dataSchemaNode = schemaContext
171 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
172 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
173 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
174 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
175 Sets.newHashSet(contAugmentQName));
176 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
177 .node(contAugmentQName);
178 final String uri = "instance-identifier-module:cont";
179 mockBodyReader(uri, xmlBodyReader, true);
180 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
181 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml"));
182 checkNormalizedNodePayload(payload);
183 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
187 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
188 final DataSchemaNode dataSchemaNode = schemaContext
189 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
190 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
191 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
192 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
193 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
194 .node(new AugmentationIdentifier(Set.of(augmentChoice1QName)))
195 .node(augmentChoice1QName)
196 // FIXME: DataSchemaContextTree bug? case children seem to ignore augments
197 // .node(new AugmentationIdentifier(Set.of(augmentChoice2QName)))
198 .node(augmentChoice2QName)
199 .node(QName.create(augmentChoice1QName, "case-choice-case-container1"));
200 final String uri = "instance-identifier-module:cont";
201 mockBodyReader(uri, xmlBodyReader, true);
202 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
203 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml"));
204 checkNormalizedNodePayload(payload);
205 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
208 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
209 final NormalizedNodePayload nnContext, final YangInstanceIdentifier dataNodeIdent) {
210 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
211 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
212 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
216 * Test when container with the same name is placed in two modules
217 * (foo-module and bar-module). Namespace must be used to distinguish
218 * between them to find correct one. Check if container was found not only
219 * according to its name but also by correct namespace used in payload.
222 public void findFooContainerUsingNamespaceTest() throws Exception {
223 mockBodyReader("", xmlBodyReader, true);
224 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
225 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml"));
227 // check return value
228 checkNormalizedNodePayload(payload);
229 // check if container was found both according to its name and namespace
230 final var payloadNodeType = payload.getData().getIdentifier().getNodeType();
231 assertEquals("foo-bar-container", payloadNodeType.getLocalName());
232 assertEquals("foo:module", payloadNodeType.getNamespace().toString());
236 * Test when container with the same name is placed in two modules
237 * (foo-module and bar-module). Namespace must be used to distinguish
238 * between them to find correct one. Check if container was found not only
239 * according to its name but also by correct namespace used in payload.
242 public void findBarContainerUsingNamespaceTest() throws Exception {
243 mockBodyReader("", xmlBodyReader, true);
244 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
245 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml"));
247 // check return value
248 checkNormalizedNodePayload(payload);
249 // check if container was found both according to its name and namespace
250 final var payloadNodeType = payload.getData().getIdentifier().getNodeType();
251 assertEquals("foo-bar-container", payloadNodeType.getLocalName());
252 assertEquals("bar:module", payloadNodeType.getNamespace().toString());
256 * Test PUT operation when message root element is not the same as the last element in request URI.
257 * PUT operation message should always start with schema node from URI otherwise exception should be
261 public void wrongRootElementTest() throws Exception {
262 mockBodyReader("instance-identifier-module:cont", xmlBodyReader, false);
263 final InputStream inputStream =
264 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/bug7933.xml");
266 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
267 () -> xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
269 final RestconfError restconfError = ex.getErrors().get(0);
270 assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
271 assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
275 public void testRangeViolation() throws Exception {
276 mockBodyReader("netconf786:foo", xmlBodyReader, false);
278 final InputStream inputStream = new ByteArrayInputStream(
279 "<foo xmlns=\"netconf786\"><bar>100</bar></foo>".getBytes(StandardCharsets.UTF_8));
281 assertRangeViolation(() -> xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream));