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.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16 import com.google.common.base.Optional;
17 import java.io.InputStream;
18 import javax.ws.rs.core.MediaType;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
23 import org.opendaylight.controller.sal.rest.impl.XmlNormalizedNodeBodyReader;
24 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
25 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
32 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 * org.opendaylight.controller.sal.rest.impl.test.providers
42 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
44 * Created: Mar 9, 2015
46 public class TestXmlBodyReaderMountPoint extends AbstractBodyReaderTest {
48 private final XmlNormalizedNodeBodyReader xmlBodyReader;
49 private static SchemaContext schemaContext;
51 public TestXmlBodyReaderMountPoint () throws NoSuchFieldException, SecurityException {
53 xmlBodyReader = new XmlNormalizedNodeBodyReader();
57 MediaType getMediaType() {
58 return new MediaType(MediaType.APPLICATION_XML, null);
62 public static void initialization() throws NoSuchFieldException, SecurityException {
63 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
64 schemaContext = schemaContextLoader("/modules", schemaContext);
65 schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
66 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
67 when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
68 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
69 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
71 ControllerContext.getInstance().setMountService(mockMountService);
72 controllerContext.setSchemas(schemaContext);
76 public void moduleDataTest() throws Exception {
77 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
78 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
79 mockBodyReader(uri, xmlBodyReader, false);
80 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
81 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
82 final NormalizedNodeContext returnValue = xmlBodyReader
83 .readFrom(null, null, null, mediaType, null, inputStream);
84 checkMountPointNormalizedNodeContext(returnValue);
85 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
89 public void moduleSubContainerDataPutTest() throws Exception {
90 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
91 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
92 mockBodyReader(uri, xmlBodyReader, false);
93 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
94 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
95 final NormalizedNodeContext returnValue = xmlBodyReader
96 .readFrom(null, null, null, mediaType, null, inputStream);
97 checkMountPointNormalizedNodeContext(returnValue);
98 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, "cont1");
102 public void moduleSubContainerDataPostTest() throws Exception {
103 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
104 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
105 mockBodyReader(uri, xmlBodyReader, true);
106 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
107 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
108 final NormalizedNodeContext returnValue = xmlBodyReader
109 .readFrom(null, null, null, mediaType, null, inputStream);
110 checkMountPointNormalizedNodeContext(returnValue);
111 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
115 public void rpcModuleInputTest() throws Exception {
116 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
117 mockBodyReader(uri, xmlBodyReader, true);
118 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
119 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
120 final NormalizedNodeContext returnValue = xmlBodyReader
121 .readFrom(null, null, null, mediaType, null, inputStream);
122 checkNormalizedNodeContext(returnValue);
123 final ContainerNode contNode = (ContainerNode) returnValue.getData();
124 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "lf"));
125 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contNode.getChild(yangleaf.getLastPathArgument());
126 assertTrue(leafDataNode.isPresent());
127 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
130 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
131 final NormalizedNodeContext nnContext) {
132 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
135 protected void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
136 final NormalizedNodeContext nnContext, final String localQname) {
137 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier.of(dataSchemaNode.getQName());
138 final DOMMountPoint mountPoint = nnContext.getInstanceIdentifierContext().getMountPoint();
139 final DataSchemaNode mountDataSchemaNode =
140 mountPoint.getSchemaContext().getDataChildByName(dataSchemaNode.getQName());
141 assertNotNull(mountDataSchemaNode);
142 if (localQname != null && dataSchemaNode instanceof DataNodeContainer) {
143 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode).getDataChildByName(localQname);
144 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent).node(child.getQName()).build();
145 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode().equals(child));
147 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
149 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));