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.controller.sal.rest.impl.test.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.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
32 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
33 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
34 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
35 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.common.QNameModule;
38 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
41 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
44 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
45 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
50 * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
54 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
56 * Created: Mar 9, 2015
58 public class TestXmlBodyReaderMountPoint extends AbstractBodyReaderTest {
59 private final XmlNormalizedNodeBodyReader xmlBodyReader;
60 private static SchemaContext schemaContext;
62 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
64 private static QNameModule initializeInstanceIdentifierModule() {
66 return QNameModule.create(URI.create("instance:identifier:module"),
67 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
68 } catch (final ParseException e) {
73 public TestXmlBodyReaderMountPoint() throws Exception {
75 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
79 protected MediaType getMediaType() {
80 return new MediaType(MediaType.APPLICATION_XML, null);
84 public static void initialization() throws Exception {
85 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
86 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
87 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
89 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
90 when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
91 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
92 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
93 .thenReturn(Optional.of(mountInstance));
95 ControllerContext.getInstance().setMountService(mockMountService);
96 controllerContext.setSchemas(schemaContext);
100 public void moduleDataTest() throws Exception {
101 final DataSchemaNode dataSchemaNode = schemaContext
102 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
103 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
104 mockBodyReader(uri, this.xmlBodyReader, false);
105 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
106 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
107 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
108 null, null, this.mediaType, null, inputStream);
109 checkMountPointNormalizedNodeContext(returnValue);
110 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
114 public void moduleSubContainerDataPutTest() throws Exception {
115 final DataSchemaNode dataSchemaNode = schemaContext
116 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
117 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
118 mockBodyReader(uri, this.xmlBodyReader, false);
119 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
120 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
121 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
122 null, null, this.mediaType, null, inputStream);
123 checkMountPointNormalizedNodeContext(returnValue);
124 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
125 QName.create(dataSchemaNode.getQName(), "cont1"));
129 public void moduleSubContainerDataPostTest() throws Exception {
130 final DataSchemaNode dataSchemaNode = schemaContext
131 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
132 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
133 mockBodyReader(uri, this.xmlBodyReader, true);
134 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
135 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
136 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
137 null, null, this.mediaType, null, inputStream);
138 checkMountPointNormalizedNodeContext(returnValue);
139 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
143 public void rpcModuleInputTest() throws Exception {
144 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
145 mockBodyReader(uri, this.xmlBodyReader, true);
146 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
147 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
148 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
149 null, null, this.mediaType, null, inputStream);
150 checkNormalizedNodeContext(returnValue);
151 final ContainerNode contNode = (ContainerNode) returnValue.getData();
152 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
153 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(yangCont
154 .getLastPathArgument());
155 assertTrue(contDataNodePotential.isPresent());
156 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
157 final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
158 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(yangLeaf
159 .getLastPathArgument());
160 assertTrue(leafDataNode.isPresent());
161 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
164 private void checkExpectValueNormalizeNodeContext(
165 final DataSchemaNode dataSchemaNode,
166 final NormalizedNodeContext nnContext) {
167 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
170 protected void checkExpectValueNormalizeNodeContext(
171 final DataSchemaNode dataSchemaNode,
172 final NormalizedNodeContext nnContext, final QName qName) {
173 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
174 .of(dataSchemaNode.getQName());
175 final DOMMountPoint mountPoint = nnContext
176 .getInstanceIdentifierContext().getMountPoint();
177 final DataSchemaNode mountDataSchemaNode = mountPoint
178 .getSchemaContext().getDataChildByName(
179 dataSchemaNode.getQName());
180 assertNotNull(mountDataSchemaNode);
181 if ((qName != null) && (dataSchemaNode instanceof DataNodeContainer)) {
182 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
183 .getDataChildByName(qName);
184 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
185 .node(child.getQName()).build();
186 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
189 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
191 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
196 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
197 * used to distinguish between them to find correct one. Check if container was found not only according to its name
198 * but also by correct namespace used in payload.
201 public void findFooContainerUsingNamespaceTest() throws Exception {
202 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
203 final InputStream inputStream = TestXmlBodyReader.class
204 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
205 final NormalizedNodeContext returnValue = this.xmlBodyReader
206 .readFrom(null, null, null, this.mediaType, null, inputStream);
208 // check return value
209 checkMountPointNormalizedNodeContext(returnValue);
210 // check if container was found both according to its name and namespace
211 assertEquals("Not correct container found, name was ignored",
212 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
213 assertEquals("Not correct container found, namespace was ignored",
214 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
218 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
219 * used to distinguish between them to find correct one. Check if container was found not only according to its name
220 * but also by correct namespace used in payload.
223 public void findBarContainerUsingNamespaceTest() throws Exception {
224 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
225 final InputStream inputStream = TestXmlBodyReader.class
226 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
227 final NormalizedNodeContext returnValue = this.xmlBodyReader
228 .readFrom(null, null, null, this.mediaType, null, inputStream);
230 // check return value
231 checkMountPointNormalizedNodeContext(returnValue);
232 // check if container was found both according to its name and namespace
233 assertEquals("Not correct container found, name was ignored",
234 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
235 assertEquals("Not correct container found, namespace was ignored",
236 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
240 * Test PUT operation when message root element is not the same as the last element in request URI.
241 * PUT operation message should always start with schema node from URI otherwise exception should be
245 public void wrongRootElementTest() throws Exception {
246 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, false);
247 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
248 "/instanceidentifier/xml/bug7933.xml");
250 this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
251 Assert.fail("Test should fail due to malformed PUT operation message");
252 } catch (final RestconfDocumentedException exception) {
253 final RestconfError restconfError = exception.getErrors().get(0);
254 Assert.assertEquals(RestconfError.ErrorType.PROTOCOL, restconfError.getErrorType());
255 Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());