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.util.Collection;
23 import javax.ws.rs.core.MediaType;
24 import org.junit.Assert;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
28 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
29 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
30 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
31 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
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.Revision;
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 TestXmlBodyReaderMountPoint extends AbstractBodyReaderTest {
49 private final XmlNormalizedNodeBodyReader xmlBodyReader;
50 private static SchemaContext schemaContext;
52 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
53 URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
55 public TestXmlBodyReaderMountPoint() throws Exception {
56 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
60 protected MediaType getMediaType() {
61 return new MediaType(MediaType.APPLICATION_XML, null);
65 public static void initialization() throws Exception {
66 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
67 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
68 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
70 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
71 when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
72 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
73 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
74 .thenReturn(Optional.of(mountInstance));
76 ControllerContext.getInstance().setMountService(mockMountService);
77 CONTROLLER_CONTEXT.setSchemas(schemaContext);
81 public void moduleDataTest() throws Exception {
82 final DataSchemaNode dataSchemaNode = schemaContext
83 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
84 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
85 mockBodyReader(uri, this.xmlBodyReader, false);
86 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
87 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
88 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
89 null, null, this.mediaType, null, inputStream);
90 checkMountPointNormalizedNodeContext(returnValue);
91 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
95 public void moduleSubContainerDataPutTest() 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/cont1";
99 mockBodyReader(uri, this.xmlBodyReader, false);
100 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
101 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
102 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
103 null, null, this.mediaType, null, inputStream);
104 checkMountPointNormalizedNodeContext(returnValue);
105 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
106 QName.create(dataSchemaNode.getQName(), "cont1"));
110 public void moduleSubContainerDataPostTest() throws Exception {
111 final DataSchemaNode dataSchemaNode = schemaContext
112 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
113 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
114 mockBodyReader(uri, this.xmlBodyReader, true);
115 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
116 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
117 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
118 null, null, this.mediaType, null, inputStream);
119 checkMountPointNormalizedNodeContext(returnValue);
120 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
124 public void rpcModuleInputTest() throws Exception {
125 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
126 mockBodyReader(uri, this.xmlBodyReader, true);
127 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
128 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
129 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
130 null, null, this.mediaType, null, inputStream);
131 checkNormalizedNodeContext(returnValue);
132 final ContainerNode contNode = (ContainerNode) returnValue.getData();
133 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
134 final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode
135 .getChild(yangCont.getLastPathArgument());
136 assertTrue(contDataNodePotential.isPresent());
137 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
138 final YangInstanceIdentifier yangLeaf =
139 YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
140 final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(
141 yangLeaf.getLastPathArgument());
142 assertTrue(leafDataNode.isPresent());
143 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
146 private void checkExpectValueNormalizeNodeContext(
147 final DataSchemaNode dataSchemaNode,
148 final NormalizedNodeContext nnContext) {
149 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
152 protected void checkExpectValueNormalizeNodeContext(
153 final DataSchemaNode dataSchemaNode,
154 final NormalizedNodeContext nnContext, final QName qualifiedName) {
155 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
156 .of(dataSchemaNode.getQName());
157 final DOMMountPoint mountPoint = nnContext
158 .getInstanceIdentifierContext().getMountPoint();
159 final DataSchemaNode mountDataSchemaNode = mountPoint
160 .getSchemaContext().getDataChildByName(
161 dataSchemaNode.getQName());
162 assertNotNull(mountDataSchemaNode);
163 if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
164 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
165 .getDataChildByName(qualifiedName);
166 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
167 .node(child.getQName()).build();
168 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
171 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
173 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
178 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
179 * used to distinguish between them to find correct one. Check if container was found not only according to its name
180 * but also by correct namespace used in payload.
183 public void findFooContainerUsingNamespaceTest() throws Exception {
184 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
185 final InputStream inputStream = TestXmlBodyReader.class
186 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
187 final NormalizedNodeContext returnValue = this.xmlBodyReader
188 .readFrom(null, null, null, this.mediaType, null, inputStream);
190 // check return value
191 checkMountPointNormalizedNodeContext(returnValue);
192 // check if container was found both according to its name and namespace
193 assertEquals("Not correct container found, name was ignored",
194 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
195 assertEquals("Not correct container found, namespace was ignored",
196 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
200 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
201 * used to distinguish between them to find correct one. Check if container was found not only according to its name
202 * but also by correct namespace used in payload.
205 public void findBarContainerUsingNamespaceTest() throws Exception {
206 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
207 final InputStream inputStream = TestXmlBodyReader.class
208 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
209 final NormalizedNodeContext returnValue = this.xmlBodyReader
210 .readFrom(null, null, null, this.mediaType, null, inputStream);
212 // check return value
213 checkMountPointNormalizedNodeContext(returnValue);
214 // check if container was found both according to its name and namespace
215 assertEquals("Not correct container found, name was ignored",
216 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
217 assertEquals("Not correct container found, namespace was ignored",
218 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
222 * Test PUT operation when message root element is not the same as the last element in request URI.
223 * PUT operation message should always start with schema node from URI otherwise exception should be
227 public void wrongRootElementTest() throws Exception {
228 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, false);
229 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
230 "/instanceidentifier/xml/bug7933.xml");
232 this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
233 Assert.fail("Test should fail due to malformed PUT operation message");
234 } catch (final RestconfDocumentedException exception) {
235 final RestconfError restconfError = exception.getErrors().get(0);
236 Assert.assertEquals(RestconfError.ErrorType.PROTOCOL, restconfError.getErrorType());
237 Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());