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.text.SimpleDateFormat;
24 import java.util.Collection;
25 import javax.ws.rs.core.MediaType;
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.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.common.QNameModule;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
41 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
42 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47 * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
51 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
53 * Created: Mar 9, 2015
55 public class TestXmlBodyReaderMountPoint extends AbstractBodyReaderTest {
56 private final XmlNormalizedNodeBodyReader xmlBodyReader;
57 private static SchemaContext schemaContext;
59 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
61 private static QNameModule initializeInstanceIdentifierModule() {
63 return QNameModule.create(URI.create("instance:identifier:module"),
64 new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-17"));
65 } catch (final ParseException e) {
71 public TestXmlBodyReaderMountPoint() throws NoSuchFieldException,
74 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
78 protected MediaType getMediaType() {
79 return new MediaType(MediaType.APPLICATION_XML, null);
83 public static void initialization() throws Exception {
84 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
85 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
86 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
88 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
89 when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
90 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
91 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
92 .thenReturn(Optional.of(mountInstance));
94 ControllerContext.getInstance().setMountService(mockMountService);
95 controllerContext.setSchemas(schemaContext);
99 public void moduleDataTest() throws Exception {
100 final DataSchemaNode dataSchemaNode = schemaContext
101 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
102 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
103 mockBodyReader(uri, this.xmlBodyReader, false);
104 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
105 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
106 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
107 null, null, this.mediaType, null, inputStream);
108 checkMountPointNormalizedNodeContext(returnValue);
109 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
113 public void moduleSubContainerDataPutTest() throws Exception {
114 final DataSchemaNode dataSchemaNode = schemaContext
115 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
116 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
117 mockBodyReader(uri, this.xmlBodyReader, false);
118 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
119 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
120 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
121 null, null, this.mediaType, null, inputStream);
122 checkMountPointNormalizedNodeContext(returnValue);
123 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
124 QName.create(dataSchemaNode.getQName(), "cont1"));
128 public void moduleSubContainerDataPostTest() throws Exception {
129 final DataSchemaNode dataSchemaNode = schemaContext
130 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
131 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
132 mockBodyReader(uri, this.xmlBodyReader, true);
133 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
134 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
135 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
136 null, null, this.mediaType, null, inputStream);
137 checkMountPointNormalizedNodeContext(returnValue);
138 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
142 public void rpcModuleInputTest() throws Exception {
143 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
144 mockBodyReader(uri, this.xmlBodyReader, true);
145 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
146 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
147 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
148 null, null, this.mediaType, null, inputStream);
149 checkNormalizedNodeContext(returnValue);
150 final ContainerNode contNode = (ContainerNode) returnValue.getData();
151 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
152 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(yangCont
153 .getLastPathArgument());
154 assertTrue(contDataNodePotential.isPresent());
155 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
156 final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
157 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(yangLeaf
158 .getLastPathArgument());
159 assertTrue(leafDataNode.isPresent());
160 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
163 private void checkExpectValueNormalizeNodeContext(
164 final DataSchemaNode dataSchemaNode,
165 final NormalizedNodeContext nnContext) {
166 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
169 protected void checkExpectValueNormalizeNodeContext(
170 final DataSchemaNode dataSchemaNode,
171 final NormalizedNodeContext nnContext, final QName qName) {
172 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
173 .of(dataSchemaNode.getQName());
174 final DOMMountPoint mountPoint = nnContext
175 .getInstanceIdentifierContext().getMountPoint();
176 final DataSchemaNode mountDataSchemaNode = mountPoint
177 .getSchemaContext().getDataChildByName(
178 dataSchemaNode.getQName());
179 assertNotNull(mountDataSchemaNode);
180 if ((qName != null) && (dataSchemaNode instanceof DataNodeContainer)) {
181 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
182 .getDataChildByName(qName);
183 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
184 .node(child.getQName()).build();
185 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
188 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
190 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
195 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
196 * used to distinguish between them to find correct one. Check if container was found not only according to its name
197 * but also by correct namespace used in payload.
200 public void findFooContainerUsingNamespaceTest() throws Exception {
201 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
202 final InputStream inputStream = TestXmlBodyReader.class
203 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
204 final NormalizedNodeContext returnValue = this.xmlBodyReader
205 .readFrom(null, null, null, this.mediaType, null, inputStream);
207 // check return value
208 checkMountPointNormalizedNodeContext(returnValue);
209 // check if container was found both according to its name and namespace
210 assertEquals("Not correct container found, name was ignored",
211 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
212 assertEquals("Not correct container found, namespace was ignored",
213 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
217 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
218 * used to distinguish between them to find correct one. Check if container was found not only according to its name
219 * but also by correct namespace used in payload.
222 public void findBarContainerUsingNamespaceTest() throws Exception {
223 mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
224 final InputStream inputStream = TestXmlBodyReader.class
225 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
226 final NormalizedNodeContext returnValue = this.xmlBodyReader
227 .readFrom(null, null, null, this.mediaType, null, inputStream);
229 // check return value
230 checkMountPointNormalizedNodeContext(returnValue);
231 // check if container was found both according to its name and namespace
232 assertEquals("Not correct container found, name was ignored",
233 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
234 assertEquals("Not correct container found, namespace was ignored",
235 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());