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.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.controller.md.sal.rest.common.TestRestconfUtils;
31 import org.opendaylight.controller.sal.rest.impl.test.providers.TestXmlBodyReader;
32 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
33 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
34 import org.opendaylight.restconf.common.errors.RestconfError;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.common.QNameModule;
37 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
40 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
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.SchemaContext;
46 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
48 public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
49 private final XmlNormalizedNodeBodyReader xmlBodyReader;
50 private static SchemaContext schemaContext;
52 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
54 private static QNameModule initializeInstanceIdentifierModule() {
56 return QNameModule.create(URI.create("instance:identifier:module"),
57 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
58 } catch (final ParseException e) {
63 public XmlBodyReaderMountPointTest() throws Exception {
65 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
69 protected MediaType getMediaType() {
70 return new MediaType(MediaType.APPLICATION_XML, null);
74 public static void initialization() throws Exception {
75 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
76 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
77 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
79 final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
80 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
82 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mountPointService);
83 when(mountPointService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountPoint));
84 when(mountPoint.getSchemaContext()).thenReturn(schemaContext);
86 CONTROLLER_CONTEXT.setSchemas(schemaContext);
90 public void moduleDataTest() throws Exception {
91 final DataSchemaNode dataSchemaNode = schemaContext
92 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
93 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
94 mockBodyReader(uri, this.xmlBodyReader, false);
95 final InputStream inputStream = XmlBodyReaderMountPointTest.class
96 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
97 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
98 null, null, this.mediaType, null, inputStream);
99 checkMountPointNormalizedNodeContext(returnValue);
100 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
104 public void moduleSubContainerDataPutTest() throws Exception {
105 final DataSchemaNode dataSchemaNode = schemaContext
106 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
107 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
108 mockBodyReader(uri, this.xmlBodyReader, false);
109 final InputStream inputStream = XmlBodyReaderMountPointTest.class
110 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
111 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
112 null, null, this.mediaType, null, inputStream);
113 checkMountPointNormalizedNodeContext(returnValue);
114 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
115 QName.create(dataSchemaNode.getQName(), "cont1"));
119 public void moduleSubContainerDataPostTest() throws Exception {
120 final DataSchemaNode dataSchemaNode = schemaContext
121 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
122 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
123 mockBodyReader(uri, this.xmlBodyReader, true);
124 final InputStream inputStream = XmlBodyReaderMountPointTest.class
125 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
126 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
127 null, null, this.mediaType, null, inputStream);
128 checkMountPointNormalizedNodeContext(returnValue);
129 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
133 public void rpcModuleInputTest() throws Exception {
134 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
135 mockBodyReader(uri, this.xmlBodyReader, true);
136 final InputStream inputStream = XmlBodyReaderMountPointTest.class
137 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
138 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
139 null, null, this.mediaType, null, inputStream);
140 checkNormalizedNodeContext(returnValue);
141 final ContainerNode contNode = (ContainerNode) returnValue.getData();
142 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
143 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(
144 yangCont.getLastPathArgument());
145 assertTrue(contDataNodePotential.isPresent());
146 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
147 final YangInstanceIdentifier yangLeaf =
148 YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
149 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(
150 yangLeaf.getLastPathArgument());
151 assertTrue(leafDataNode.isPresent());
152 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
155 private void checkExpectValueNormalizeNodeContext(
156 final DataSchemaNode dataSchemaNode,
157 final NormalizedNodeContext nnContext) {
158 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
161 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
162 final NormalizedNodeContext nnContext, final QName qualifiedName) {
163 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
164 .of(dataSchemaNode.getQName());
165 final DOMMountPoint mountPoint = nnContext
166 .getInstanceIdentifierContext().getMountPoint();
167 final DataSchemaNode mountDataSchemaNode = mountPoint
168 .getSchemaContext().getDataChildByName(
169 dataSchemaNode.getQName());
170 assertNotNull(mountDataSchemaNode);
171 if ((qualifiedName != null) && (dataSchemaNode instanceof DataNodeContainer)) {
172 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
173 .getDataChildByName(qualifiedName);
174 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
175 .node(child.getQName()).build();
176 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
179 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
181 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
186 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
187 * used to distinguish between them to find correct one. Check if container was found not only according to its name
188 * but also by correct namespace used in payload.
191 public void findFooContainerUsingNamespaceTest() throws Exception {
192 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
193 final InputStream inputStream = TestXmlBodyReader.class
194 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
195 final NormalizedNodeContext returnValue = this.xmlBodyReader
196 .readFrom(null, null, null, this.mediaType, null, inputStream);
198 // check return value
199 checkMountPointNormalizedNodeContext(returnValue);
200 // check if container was found both according to its name and namespace
201 assertEquals("Not correct container found, name was ignored",
202 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
203 assertEquals("Not correct container found, namespace was ignored",
204 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
208 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
209 * used to distinguish between them to find correct one. Check if container was found not only according to its name
210 * but also by correct namespace used in payload.
213 public void findBarContainerUsingNamespaceTest() throws Exception {
214 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
215 final InputStream inputStream = TestXmlBodyReader.class
216 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
217 final NormalizedNodeContext returnValue = this.xmlBodyReader
218 .readFrom(null, null, null, this.mediaType, null, inputStream);
220 // check return value
221 checkMountPointNormalizedNodeContext(returnValue);
222 // check if container was found both according to its name and namespace
223 assertEquals("Not correct container found, name was ignored",
224 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
225 assertEquals("Not correct container found, namespace was ignored",
226 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
230 * Test PUT operation when message root element is not the same as the last element in request URI.
231 * PUT operation message should always start with schema node from URI otherwise exception should be
235 public void wrongRootElementTest() throws Exception {
236 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, false);
237 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
238 "/instanceidentifier/xml/bug7933.xml");
240 this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
241 Assert.fail("Test should fail due to malformed PUT operation message");
242 } catch (final RestconfDocumentedException exception) {
243 final RestconfError restconfError = exception.getErrors().get(0);
244 Assert.assertEquals(RestconfError.ErrorType.PROTOCOL, restconfError.getErrorType());
245 Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());