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;
14 import java.io.InputStream;
16 import javax.ws.rs.core.MediaType;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.sal.rest.impl.XmlNormalizedNodeBodyReader;
21 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import com.google.common.base.Optional;
35 * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
39 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
41 * Created: Mar 7, 2015
43 public class TestXmlBodyReader extends AbstractBodyReaderTest {
45 private final XmlNormalizedNodeBodyReader xmlBodyReader;
46 private static SchemaContext schemaContext;
48 public TestXmlBodyReader() throws NoSuchFieldException, SecurityException {
50 xmlBodyReader = new XmlNormalizedNodeBodyReader();
54 protected MediaType getMediaType() {
55 return new MediaType(MediaType.APPLICATION_XML, null);
59 public static void initialization() throws NoSuchFieldException,
61 schemaContext = schemaContextLoader("/instanceidentifier/yang",
63 schemaContext = schemaContextLoader("/modules", schemaContext);
64 schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
65 controllerContext.setSchemas(schemaContext);
69 public void moduleDataTest() throws Exception {
70 final DataSchemaNode dataSchemaNode = schemaContext
71 .getDataChildByName("cont");
72 final String uri = "instance-identifier-module:cont";
73 mockBodyReader(uri, xmlBodyReader, false);
74 final InputStream inputStream = TestXmlBodyReader.class
75 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
76 final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
77 null, null, mediaType, null, inputStream);
78 checkNormalizedNodeContext(returnValue);
79 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
83 public void moduleSubContainerDataPutTest() throws Exception {
84 final DataSchemaNode dataSchemaNode = schemaContext
85 .getDataChildByName("cont");
86 final String uri = "instance-identifier-module:cont/cont1";
87 mockBodyReader(uri, xmlBodyReader, false);
88 final InputStream inputStream = TestXmlBodyReader.class
89 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
90 final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
91 null, null, mediaType, null, inputStream);
92 checkNormalizedNodeContext(returnValue);
93 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
98 public void moduleSubContainerDataPostTest() throws Exception {
99 final DataSchemaNode dataSchemaNode = schemaContext
100 .getDataChildByName("cont");
101 final String uri = "instance-identifier-module:cont";
102 mockBodyReader(uri, xmlBodyReader, true);
103 final InputStream inputStream = TestXmlBodyReader.class
104 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
105 final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
106 null, null, mediaType, null, inputStream);
107 checkNormalizedNodeContext(returnValue);
108 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
112 public void rpcModuleInputTest() throws Exception {
113 final String uri = "invoke-rpc-module:rpc-test";
114 mockBodyReader(uri, xmlBodyReader, true);
115 final InputStream inputStream = TestXmlBodyReader.class
116 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
117 final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
118 null, null, mediaType, null, inputStream);
119 checkNormalizedNodeContext(returnValue);
120 final ContainerNode contNode = (ContainerNode) returnValue.getData();
121 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
122 .create(contNode.getNodeType(), "lf"));
123 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contNode
124 .getChild(yangleaf.getLastPathArgument());
125 assertTrue(leafDataNode.isPresent());
126 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
130 private void checkExpectValueNormalizeNodeContext(
131 final DataSchemaNode dataSchemaNode,
132 final NormalizedNodeContext nnContext) {
133 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
136 private void checkExpectValueNormalizeNodeContext(
137 final DataSchemaNode dataSchemaNode,
138 final NormalizedNodeContext nnContext, final String localQname) {
139 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
140 .of(dataSchemaNode.getQName());
142 if (localQname != null && dataSchemaNode instanceof DataNodeContainer) {
143 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
144 .getDataChildByName(localQname);
145 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
146 .node(child.getQName()).build();
147 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
150 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
151 .equals(dataSchemaNode));
153 assertTrue(nnContext.getInstanceIdentifierContext()
154 .getInstanceIdentifier().equals(dataNodeIdent));
155 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),