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;
17 import java.io.InputStream;
19 import javax.ws.rs.core.MediaType;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
24 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
25 import org.opendaylight.controller.sal.rest.impl.XmlNormalizedNodeBodyReader;
26 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
27 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
34 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import com.google.common.base.Optional;
41 * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
45 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
47 * Created: Mar 9, 2015
49 public class TestXmlBodyReaderMountPoint extends AbstractBodyReaderTest {
51 private final XmlNormalizedNodeBodyReader xmlBodyReader;
52 private static SchemaContext schemaContext;
54 public TestXmlBodyReaderMountPoint() throws NoSuchFieldException,
57 xmlBodyReader = new XmlNormalizedNodeBodyReader();
61 protected MediaType getMediaType() {
62 return new MediaType(MediaType.APPLICATION_XML, null);
66 public static void initialization() throws NoSuchFieldException,
68 schemaContext = schemaContextLoader("/instanceidentifier/yang",
70 schemaContext = schemaContextLoader("/modules", schemaContext);
71 schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
72 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
73 when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
74 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
75 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
76 .thenReturn(Optional.of(mountInstance));
78 ControllerContext.getInstance().setMountService(mockMountService);
79 controllerContext.setSchemas(schemaContext);
83 public void moduleDataTest() throws Exception {
84 final DataSchemaNode dataSchemaNode = schemaContext
85 .getDataChildByName("cont");
86 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
87 mockBodyReader(uri, xmlBodyReader, false);
88 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
89 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
90 final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
91 null, null, mediaType, null, inputStream);
92 checkMountPointNormalizedNodeContext(returnValue);
93 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
97 public void moduleSubContainerDataPutTest() throws Exception {
98 final DataSchemaNode dataSchemaNode = schemaContext
99 .getDataChildByName("cont");
100 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
101 mockBodyReader(uri, xmlBodyReader, false);
102 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
103 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
104 final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
105 null, null, mediaType, null, inputStream);
106 checkMountPointNormalizedNodeContext(returnValue);
107 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
112 public void moduleSubContainerDataPostTest() throws Exception {
113 final DataSchemaNode dataSchemaNode = schemaContext
114 .getDataChildByName("cont");
115 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
116 mockBodyReader(uri, xmlBodyReader, true);
117 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
118 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
119 final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
120 null, null, mediaType, null, inputStream);
121 checkMountPointNormalizedNodeContext(returnValue);
122 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
126 public void rpcModuleInputTest() throws Exception {
127 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
128 mockBodyReader(uri, xmlBodyReader, true);
129 final InputStream inputStream = TestXmlBodyReaderMountPoint.class
130 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
131 final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
132 null, null, mediaType, null, inputStream);
133 checkNormalizedNodeContext(returnValue);
134 final ContainerNode contNode = (ContainerNode) returnValue.getData();
135 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
136 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(yangCont
137 .getLastPathArgument());
138 assertTrue(contDataNodePotential.isPresent());
139 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
140 final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
141 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(yangLeaf
142 .getLastPathArgument());
143 assertTrue(leafDataNode.isPresent());
144 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
147 private void checkExpectValueNormalizeNodeContext(
148 final DataSchemaNode dataSchemaNode,
149 final NormalizedNodeContext nnContext) {
150 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
153 protected void checkExpectValueNormalizeNodeContext(
154 final DataSchemaNode dataSchemaNode,
155 final NormalizedNodeContext nnContext, final String localQname) {
156 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
157 .of(dataSchemaNode.getQName());
158 final DOMMountPoint mountPoint = nnContext
159 .getInstanceIdentifierContext().getMountPoint();
160 final DataSchemaNode mountDataSchemaNode = mountPoint
161 .getSchemaContext().getDataChildByName(
162 dataSchemaNode.getQName());
163 assertNotNull(mountDataSchemaNode);
164 if (localQname != null && dataSchemaNode instanceof DataNodeContainer) {
165 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
166 .getDataChildByName(localQname);
167 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
168 .node(child.getQName()).build();
169 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
172 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
174 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),