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.restconf.common.context.NormalizedNodeContext;
34 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
35 import org.opendaylight.restconf.common.errors.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;
49 public class TestXmlBodyReaderMountPoint extends AbstractBodyReaderTest {
50 private final XmlNormalizedNodeBodyReader xmlBodyReader;
51 private static SchemaContext schemaContext;
53 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
55 private static QNameModule initializeInstanceIdentifierModule() {
57 return QNameModule.create(URI.create("instance:identifier:module"),
58 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
59 } catch (final ParseException e) {
64 public TestXmlBodyReaderMountPoint() throws Exception {
66 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
70 protected MediaType getMediaType() {
71 return new MediaType(MediaType.APPLICATION_XML, null);
75 public static void initialization() throws Exception {
76 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
77 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
78 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
80 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
81 when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
82 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
83 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
84 .thenReturn(Optional.of(mountInstance));
86 ControllerContext.getInstance().setMountService(mockMountService);
87 CONTROLLER_CONTEXT.setSchemas(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 = TestXmlBodyReaderMountPoint.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 = TestXmlBodyReaderMountPoint.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 = TestXmlBodyReaderMountPoint.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 = TestXmlBodyReaderMountPoint.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(yangCont
145 .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(yangLeaf
151 .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 protected void checkExpectValueNormalizeNodeContext(
163 final DataSchemaNode dataSchemaNode,
164 final NormalizedNodeContext nnContext, final QName qualifiedName) {
165 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
166 .of(dataSchemaNode.getQName());
167 final DOMMountPoint mountPoint = nnContext
168 .getInstanceIdentifierContext().getMountPoint();
169 final DataSchemaNode mountDataSchemaNode = mountPoint
170 .getSchemaContext().getDataChildByName(
171 dataSchemaNode.getQName());
172 assertNotNull(mountDataSchemaNode);
173 if ((qualifiedName != null) && (dataSchemaNode instanceof DataNodeContainer)) {
174 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
175 .getDataChildByName(qualifiedName);
176 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
177 .node(child.getQName()).build();
178 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
181 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
183 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
188 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
189 * used to distinguish between them to find correct one. Check if container was found not only according to its name
190 * but also by correct namespace used in payload.
193 public void findFooContainerUsingNamespaceTest() throws Exception {
194 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
195 final InputStream inputStream = TestXmlBodyReader.class
196 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
197 final NormalizedNodeContext returnValue = this.xmlBodyReader
198 .readFrom(null, null, null, this.mediaType, null, inputStream);
200 // check return value
201 checkMountPointNormalizedNodeContext(returnValue);
202 // check if container was found both according to its name and namespace
203 assertEquals("Not correct container found, name was ignored",
204 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
205 assertEquals("Not correct container found, namespace was ignored",
206 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
210 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
211 * used to distinguish between them to find correct one. Check if container was found not only according to its name
212 * but also by correct namespace used in payload.
215 public void findBarContainerUsingNamespaceTest() throws Exception {
216 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
217 final InputStream inputStream = TestXmlBodyReader.class
218 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
219 final NormalizedNodeContext returnValue = this.xmlBodyReader
220 .readFrom(null, null, null, this.mediaType, null, inputStream);
222 // check return value
223 checkMountPointNormalizedNodeContext(returnValue);
224 // check if container was found both according to its name and namespace
225 assertEquals("Not correct container found, name was ignored",
226 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
227 assertEquals("Not correct container found, namespace was ignored",
228 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
232 * Test PUT operation when message root element is not the same as the last element in request URI.
233 * PUT operation message should always start with schema node from URI otherwise exception should be
237 public void wrongRootElementTest() throws Exception {
238 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, false);
239 final InputStream inputStream = TestXmlBodyReader.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());