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.restconf.nb.rfc8040.jersey.providers;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
16 import java.io.InputStream;
17 import java.util.Collection;
18 import java.util.Optional;
19 import javax.ws.rs.core.MediaType;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
23 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
24 import org.opendaylight.restconf.common.errors.RestconfError;
25 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
26 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
27 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
28 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
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.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
39 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
40 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
42 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44 public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
45 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
46 XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
48 private static EffectiveModelContext schemaContext;
50 private final XmlNormalizedNodeBodyReader xmlBodyReader;
52 public XmlBodyReaderMountPointTest() throws Exception {
54 xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler, mountPointService);
58 protected MediaType getMediaType() {
59 return new MediaType(MediaType.APPLICATION_XML, null);
63 public static void initialization() throws Exception {
64 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
65 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
66 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
70 public void moduleDataTest() throws Exception {
71 final DataSchemaNode dataSchemaNode = schemaContext
72 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
73 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
74 mockBodyReader(uri, xmlBodyReader, false);
75 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
76 XmlBodyReaderMountPointTest.class.getResourceAsStream("/instanceidentifier/xml/xmldata.xml"));
77 checkMountPointNormalizedNodePayload(payload);
78 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload);
82 public void moduleSubContainerDataPutTest() throws Exception {
83 final DataSchemaNode dataSchemaNode = schemaContext
84 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
85 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
86 mockBodyReader(uri, xmlBodyReader, false);
87 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
88 XmlBodyReaderMountPointTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
89 checkMountPointNormalizedNodePayload(payload);
90 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload,
91 QName.create(dataSchemaNode.getQName(), "cont1"));
95 public void moduleSubContainerDataPostTest() throws Exception {
96 final DataSchemaNode dataSchemaNode = schemaContext
97 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
98 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
99 mockBodyReader(uri, xmlBodyReader, true);
100 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
101 XmlBodyReaderMountPointTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
102 checkMountPointNormalizedNodePayload(payload);
103 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload);
107 public void moduleSubContainerDataPostActionTest() throws Exception {
108 final Optional<DataSchemaNode> dataSchemaNode = schemaContext
109 .findDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
110 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1/reset";
111 mockBodyReader(uri, xmlBodyReader, true);
112 final NormalizedNodePayload pyaload = xmlBodyReader.readFrom(null,null, null, mediaType, null,
113 XmlBodyReaderMountPointTest.class.getResourceAsStream("/instanceidentifier/xml/xml_cont_action.xml"));
114 checkMountPointNormalizedNodePayload(pyaload);
115 checkExpectValueNormalizeNodeContext(dataSchemaNode.get(), pyaload);
119 public void rpcModuleInputTest() throws Exception {
120 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
121 mockBodyReader(uri, xmlBodyReader, true);
122 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
123 XmlBodyReaderMountPointTest.class.getResourceAsStream("/invoke-rpc/xml/rpc-input.xml"));
124 checkNormalizedNodePayload(payload);
125 final ContainerNode contNode = (ContainerNode) payload.getData();
126 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(
127 QName.create(contNode.getIdentifier().getNodeType(), "cont"));
128 final Optional<DataContainerChild> contDataNodePotential =
129 contNode.findChildByArg(yangCont.getLastPathArgument());
130 assertTrue(contDataNodePotential.isPresent());
131 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
132 final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(
133 QName.create(contDataNode.getIdentifier().getNodeType(), "lf"));
134 final Optional<DataContainerChild> leafDataNode = contDataNode.findChildByArg(
135 yangLeaf.getLastPathArgument());
136 assertTrue(leafDataNode.isPresent());
137 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().body().toString()));
140 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
141 final NormalizedNodePayload nnContext) {
142 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
145 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
146 final NormalizedNodePayload nnContext, final QName qualifiedName) {
147 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier.of(dataSchemaNode.getQName());
148 final DOMMountPoint mountPoint = nnContext.getInstanceIdentifierContext().getMountPoint();
149 final DataSchemaNode mountDataSchemaNode = modelContext(mountPoint)
150 .getDataChildByName(dataSchemaNode.getQName());
151 assertNotNull(mountDataSchemaNode);
152 if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
153 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode).getDataChildByName(qualifiedName);
154 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent).node(child.getQName()).build();
155 assertEquals(nnContext.getInstanceIdentifierContext().getSchemaNode(), child);
157 assertEquals(mountDataSchemaNode, dataSchemaNode);
159 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
163 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
164 * used to distinguish between them to find correct one. Check if container was found not only according to its name
165 * but also by correct namespace used in payload.
168 public void findFooContainerUsingNamespaceTest() throws Exception {
169 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, true);
170 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
171 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml"));
173 // check return value
174 checkMountPointNormalizedNodePayload(payload);
175 // check if container was found both according to its name and namespace
176 final var dataNodeType = payload.getData().getIdentifier().getNodeType();
177 assertEquals("foo-bar-container", dataNodeType.getLocalName());
178 assertEquals("foo:module", dataNodeType.getNamespace().toString());
182 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
183 * used to distinguish between them to find correct one. Check if container was found not only according to its name
184 * but also by correct namespace used in payload.
187 public void findBarContainerUsingNamespaceTest() throws Exception {
188 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, true);
189 final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
190 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml"));
192 // check return value
193 checkMountPointNormalizedNodePayload(payload);
194 // check if container was found both according to its name and namespace
195 final var dataNodeType = payload.getData().getIdentifier().getNodeType();
196 assertEquals("foo-bar-container", dataNodeType.getLocalName());
197 assertEquals("bar:module", dataNodeType.getNamespace().toString());
201 * Test PUT operation when message root element is not the same as the last element in request URI.
202 * PUT operation message should always start with schema node from URI otherwise exception should be
206 public void wrongRootElementTest() throws Exception {
207 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, false);
208 final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
209 "/instanceidentifier/xml/bug7933.xml");
211 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
212 () -> xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
213 final RestconfError restconfError = ex.getErrors().get(0);
214 assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
215 assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());