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;
24 import javax.ws.rs.core.MediaType;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
28 import org.opendaylight.restconf.common.errors.RestconfError;
29 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
30 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.XmlNormalizedNodeBodyReader;
31 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
32 import org.opendaylight.yangtools.yang.common.ErrorTag;
33 import org.opendaylight.yangtools.yang.common.ErrorType;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.common.QNameModule;
36 import org.opendaylight.yangtools.yang.common.Revision;
37 import org.opendaylight.yangtools.yang.common.XMLNamespace;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.ActionDefinition;
43 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
44 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
46 import org.opendaylight.yangtools.yang.model.api.Module;
47 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
49 public class XmlBodyReaderTest extends AbstractBodyReaderTest {
50 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
51 XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
53 private static EffectiveModelContext schemaContext;
55 private final XmlNormalizedNodeBodyReader xmlBodyReader;
57 public XmlBodyReaderTest() throws Exception {
59 xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler, mountPointService);
63 protected MediaType getMediaType() {
64 return new MediaType(MediaType.APPLICATION_XML, null);
68 public static void initialization() throws Exception {
69 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
70 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
71 testFiles.addAll(TestRestconfUtils.loadFiles("/foo-xml-test/yang"));
72 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
76 public void putXmlTest() throws Exception {
77 runXmlTest(false, "foo:top-level-list=key-value");
81 public void postXmlTest() throws Exception {
85 private void runXmlTest(final boolean isPost, final String path) throws Exception {
86 mockBodyReader(path, xmlBodyReader, isPost);
87 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
88 XmlBodyReaderTest.class.getResourceAsStream("/foo-xml-test/foo.xml"));
89 assertNotNull(payload);
91 assertThat(payload.getData(), instanceOf(MapEntryNode.class));
92 final MapEntryNode data = (MapEntryNode) payload.getData();
93 assertEquals(2, data.size());
94 for (final DataContainerChild child : data.body()) {
95 switch (child.getIdentifier().getNodeType().getLocalName()) {
97 assertEquals("key-value", child.body());
100 assertEquals("leaf-value", child.body());
109 public void moduleDataTest() throws Exception {
110 final DataSchemaNode dataSchemaNode = schemaContext
111 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
112 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
113 final String uri = "instance-identifier-module:cont";
114 mockBodyReader(uri, xmlBodyReader, false);
115 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
116 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmldata.xml"));
117 checkNormalizedNodePayload(payload);
118 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
122 public void moduleSubContainerDataPutTest() throws Exception {
123 final DataSchemaNode dataSchemaNode = schemaContext
124 .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, xmlBodyReader, false);
130 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
131 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
132 checkNormalizedNodePayload(payload);
133 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, payload, dataII);
137 public void moduleSubContainerDataPostTest() throws Exception {
138 final DataSchemaNode dataSchemaNode = schemaContext
139 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
140 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
141 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
142 final String uri = "instance-identifier-module:cont";
143 mockBodyReader(uri, xmlBodyReader, true);
144 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
145 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
146 checkNormalizedNodePayload(payload);
147 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
151 public void moduleSubContainerDataPostActionTest() throws Exception {
152 final Optional<DataSchemaNode> dataSchemaNode = schemaContext
153 .findDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
154 final QName cont1QName = QName.create(dataSchemaNode.get().getQName(), "cont1");
155 final QName actionQName = QName.create(dataSchemaNode.get().getQName(), "reset");
156 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.get().getQName())
157 .node(cont1QName).node(actionQName);
158 final String uri = "instance-identifier-module:cont/cont1/reset";
159 mockBodyReader(uri, xmlBodyReader, true);
160 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
161 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_cont_action.xml"));
162 checkNormalizedNodePayload(payload);
163 assertThat(payload.getInstanceIdentifierContext().getSchemaNode(), instanceOf(ActionDefinition.class));
167 public void moduleSubContainerAugmentDataPostTest() throws Exception {
168 final DataSchemaNode dataSchemaNode = schemaContext
169 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
170 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
171 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
172 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
173 Sets.newHashSet(contAugmentQName));
174 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
175 .node(contAugmentQName);
176 final String uri = "instance-identifier-module:cont";
177 mockBodyReader(uri, xmlBodyReader, true);
178 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
179 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml"));
180 checkNormalizedNodePayload(payload);
181 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
185 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
186 final DataSchemaNode dataSchemaNode = schemaContext
187 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
188 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
189 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
190 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
191 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
192 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
193 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
194 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
195 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
196 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
197 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
198 final String uri = "instance-identifier-module:cont";
199 mockBodyReader(uri, xmlBodyReader, true);
200 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
201 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml"));
202 checkNormalizedNodePayload(payload);
203 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
206 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
207 final NormalizedNodePayload nnContext, final YangInstanceIdentifier dataNodeIdent) {
208 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
209 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
210 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
214 * Test when container with the same name is placed in two modules
215 * (foo-module and bar-module). Namespace must be used to distinguish
216 * between them to find correct one. Check if container was found not only
217 * according to its name but also by correct namespace used in payload.
220 public void findFooContainerUsingNamespaceTest() throws Exception {
221 mockBodyReader("", xmlBodyReader, true);
222 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
223 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml"));
225 // check return value
226 checkNormalizedNodePayload(payload);
227 // check if container was found both according to its name and namespace
228 final var payloadNodeType = payload.getData().getIdentifier().getNodeType();
229 assertEquals("foo-bar-container", payloadNodeType.getLocalName());
230 assertEquals("foo:module", payloadNodeType.getNamespace().toString());
234 * Test when container with the same name is placed in two modules
235 * (foo-module and bar-module). Namespace must be used to distinguish
236 * between them to find correct one. Check if container was found not only
237 * according to its name but also by correct namespace used in payload.
240 public void findBarContainerUsingNamespaceTest() throws Exception {
241 mockBodyReader("", xmlBodyReader, true);
242 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
243 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml"));
245 // check return value
246 checkNormalizedNodePayload(payload);
247 // check if container was found both according to its name and namespace
248 final var payloadNodeType = payload.getData().getIdentifier().getNodeType();
249 assertEquals("foo-bar-container", payloadNodeType.getLocalName());
250 assertEquals("bar:module", payloadNodeType.getNamespace().toString());
254 * Test PUT operation when message root element is not the same as the last element in request URI.
255 * PUT operation message should always start with schema node from URI otherwise exception should be
259 public void wrongRootElementTest() throws Exception {
260 mockBodyReader("instance-identifier-module:cont", xmlBodyReader, false);
261 final InputStream inputStream =
262 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/bug7933.xml");
264 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
265 () -> xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
267 final RestconfError restconfError = ex.getErrors().get(0);
268 assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
269 assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
273 public void testRangeViolation() throws Exception {
274 mockBodyReader("netconf786:foo", xmlBodyReader, false);
276 final InputStream inputStream = new ByteArrayInputStream(
277 "<foo xmlns=\"netconf786\"><bar>100</bar></foo>".getBytes(StandardCharsets.UTF_8));
279 assertRangeViolation(() -> xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream));