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.restconf.nb.rfc8040.jersey.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.mockito.Matchers.any;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
18 import com.google.common.base.Optional;
20 import java.io.InputStream;
22 import java.text.ParseException;
23 import java.util.Collection;
24 import javax.ws.rs.core.MediaType;
25 import org.junit.Assert;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
29 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
30 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
31 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
32 import org.opendaylight.restconf.common.errors.RestconfError;
33 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
34 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
35 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
36 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.common.QNameModule;
39 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
42 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
44 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
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.SchemaContext;
48 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
50 public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
51 private final XmlNormalizedNodeBodyReader xmlBodyReader;
52 private static SchemaContext schemaContext;
54 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
56 private static QNameModule initializeInstanceIdentifierModule() {
58 return QNameModule.create(URI.create("instance:identifier:module"),
59 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
60 } catch (final ParseException e) {
65 public XmlBodyReaderMountPointTest() throws Exception {
67 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
71 protected MediaType getMediaType() {
72 return new MediaType(MediaType.APPLICATION_XML, null);
76 public static void initialization() throws Exception {
77 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
78 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
79 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
81 final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
82 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
84 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mountPointService);
85 when(mountPointService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountPoint));
86 when(mountPoint.getSchemaContext()).thenReturn(schemaContext);
87 SchemaContextHandler.setActualSchemaContext(schemaContext);
91 public void moduleDataTest() throws Exception {
92 final DataSchemaNode dataSchemaNode = schemaContext
93 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
94 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
95 mockBodyReader(uri, this.xmlBodyReader, false);
96 final InputStream inputStream = XmlBodyReaderMountPointTest.class
97 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
98 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
99 null, null, this.mediaType, null, inputStream);
100 checkMountPointNormalizedNodeContext(returnValue);
101 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
105 public void moduleSubContainerDataPutTest() throws Exception {
106 final DataSchemaNode dataSchemaNode = schemaContext
107 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
108 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
109 mockBodyReader(uri, this.xmlBodyReader, false);
110 final InputStream inputStream = XmlBodyReaderMountPointTest.class
111 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
112 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
113 null, null, this.mediaType, null, inputStream);
114 checkMountPointNormalizedNodeContext(returnValue);
115 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
116 QName.create(dataSchemaNode.getQName(), "cont1"));
120 public void moduleSubContainerDataPostTest() throws Exception {
121 final DataSchemaNode dataSchemaNode = schemaContext
122 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
123 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
124 mockBodyReader(uri, this.xmlBodyReader, true);
125 final InputStream inputStream = XmlBodyReaderMountPointTest.class
126 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
127 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
128 null, null, this.mediaType, null, inputStream);
129 checkMountPointNormalizedNodeContext(returnValue);
130 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
134 public void rpcModuleInputTest() throws Exception {
135 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
136 mockBodyReader(uri, this.xmlBodyReader, true);
137 final InputStream inputStream = XmlBodyReaderMountPointTest.class
138 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
139 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
140 null, null, this.mediaType, null, inputStream);
141 checkNormalizedNodeContext(returnValue);
142 final ContainerNode contNode = (ContainerNode) returnValue.getData();
143 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
144 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(
145 yangCont.getLastPathArgument());
146 assertTrue(contDataNodePotential.isPresent());
147 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
148 final YangInstanceIdentifier yangLeaf =
149 YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
150 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(
151 yangLeaf.getLastPathArgument());
152 assertTrue(leafDataNode.isPresent());
153 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
156 private void checkExpectValueNormalizeNodeContext(
157 final DataSchemaNode dataSchemaNode,
158 final NormalizedNodeContext nnContext) {
159 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
162 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
163 final NormalizedNodeContext nnContext, final QName qualifiedName) {
164 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
165 .of(dataSchemaNode.getQName());
166 final DOMMountPoint mountPoint = nnContext
167 .getInstanceIdentifierContext().getMountPoint();
168 final DataSchemaNode mountDataSchemaNode = mountPoint
169 .getSchemaContext().getDataChildByName(
170 dataSchemaNode.getQName());
171 assertNotNull(mountDataSchemaNode);
172 if ((qualifiedName != null) && (dataSchemaNode instanceof DataNodeContainer)) {
173 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
174 .getDataChildByName(qualifiedName);
175 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
176 .node(child.getQName()).build();
177 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
180 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
182 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
187 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
188 * used to distinguish between them to find correct one. Check if container was found not only according to its name
189 * but also by correct namespace used in payload.
192 public void findFooContainerUsingNamespaceTest() throws Exception {
193 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
194 final InputStream inputStream = XmlBodyReaderTest.class
195 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
196 final NormalizedNodeContext returnValue = this.xmlBodyReader
197 .readFrom(null, null, null, this.mediaType, null, inputStream);
199 // check return value
200 checkMountPointNormalizedNodeContext(returnValue);
201 // check if container was found both according to its name and namespace
202 assertEquals("Not correct container found, name was ignored",
203 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
204 assertEquals("Not correct container found, namespace was ignored",
205 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
209 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
210 * used to distinguish between them to find correct one. Check if container was found not only according to its name
211 * but also by correct namespace used in payload.
214 public void findBarContainerUsingNamespaceTest() throws Exception {
215 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
216 final InputStream inputStream = XmlBodyReaderTest.class
217 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
218 final NormalizedNodeContext returnValue = this.xmlBodyReader
219 .readFrom(null, null, null, this.mediaType, null, inputStream);
221 // check return value
222 checkMountPointNormalizedNodeContext(returnValue);
223 // check if container was found both according to its name and namespace
224 assertEquals("Not correct container found, name was ignored",
225 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
226 assertEquals("Not correct container found, namespace was ignored",
227 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
231 * Test PUT operation when message root element is not the same as the last element in request URI.
232 * PUT operation message should always start with schema node from URI otherwise exception should be
236 public void wrongRootElementTest() throws Exception {
237 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, false);
238 final InputStream inputStream =
239 XmlBodyReaderTest.class.getResourceAsStream(
240 "/instanceidentifier/xml/bug7933.xml");
242 this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
243 Assert.fail("Test should fail due to malformed PUT operation message");
244 } catch (final RestconfDocumentedException exception) {
245 final RestconfError restconfError = exception.getErrors().get(0);
246 Assert.assertEquals(RestconfError.ErrorType.PROTOCOL, restconfError.getErrorType());
247 Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());