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 com.google.common.base.Optional;
15 import com.google.common.collect.Sets;
16 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.sal.rest.impl.XmlNormalizedNodeBodyReader;
22 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
29 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 * org.opendaylight.controller.sal.rest.impl.test.providers
40 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
42 * Created: Mar 7, 2015
44 public class TestXmlBodyReader extends AbstractBodyReaderTest {
46 private final XmlNormalizedNodeBodyReader xmlBodyReader;
47 private static SchemaContext schemaContext;
49 public TestXmlBodyReader () throws NoSuchFieldException, SecurityException {
51 xmlBodyReader = new XmlNormalizedNodeBodyReader();
55 protected MediaType getMediaType() {
56 return new MediaType(MediaType.APPLICATION_XML, null);
60 public static void initialization() throws NoSuchFieldException, SecurityException {
61 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
62 schemaContext = schemaContextLoader("/modules", schemaContext);
63 schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
64 controllerContext.setSchemas(schemaContext);
68 public void moduleDataTest() throws Exception {
69 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
70 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
71 final String uri = "instance-identifier-module:cont";
72 mockBodyReader(uri, xmlBodyReader, false);
73 final InputStream inputStream = TestXmlBodyReader.class
74 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
75 final NormalizedNodeContext returnValue = xmlBodyReader
76 .readFrom(null, null, null, mediaType, null, inputStream);
77 checkNormalizedNodeContext(returnValue);
78 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
82 public void moduleSubContainerDataPutTest() throws Exception {
83 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
84 QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
85 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
86 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
87 final String uri = "instance-identifier-module:cont/cont1";
88 mockBodyReader(uri, xmlBodyReader, false);
89 final InputStream inputStream = TestXmlBodyReader.class
90 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
91 final NormalizedNodeContext returnValue = xmlBodyReader
92 .readFrom(null, null, null, mediaType, null, inputStream);
93 checkNormalizedNodeContext(returnValue);
94 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
98 public void moduleSubContainerDataPostTest() throws Exception {
99 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
100 QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
101 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
102 final String uri = "instance-identifier-module:cont";
103 mockBodyReader(uri, xmlBodyReader, true);
104 final InputStream inputStream = TestXmlBodyReader.class
105 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
106 final NormalizedNodeContext returnValue = xmlBodyReader
107 .readFrom(null, null, null, mediaType, null, inputStream);
108 checkNormalizedNodeContext(returnValue);
109 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
113 public void moduleSubContainerAugmentDataPostTest() throws Exception {
114 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
115 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
116 QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
117 YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
118 Sets.newHashSet(contAugmentQName));
119 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
120 .node(augII).node(contAugmentQName);
121 final String uri = "instance-identifier-module:cont";
122 mockBodyReader(uri, xmlBodyReader, true);
123 final InputStream inputStream = TestXmlBodyReader.class
124 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
125 final NormalizedNodeContext returnValue = xmlBodyReader
126 .readFrom(null, null, null, mediaType, null, inputStream);
127 checkNormalizedNodeContext(returnValue);
128 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
132 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
133 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
134 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
135 QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
136 QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
137 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
138 YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
139 Sets.newHashSet(augmentChoice1QName));
140 YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
141 Sets.newHashSet(augmentChoice2QName));
142 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
143 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
144 .node(containerQName);
145 final String uri = "instance-identifier-module:cont";
146 mockBodyReader(uri, xmlBodyReader, true);
147 final InputStream inputStream = TestXmlBodyReader.class
148 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
149 final NormalizedNodeContext returnValue = xmlBodyReader
150 .readFrom(null, null, null, mediaType, null, inputStream);
151 checkNormalizedNodeContext(returnValue);
152 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
156 public void rpcModuleInputTest() throws Exception {
157 final String uri = "invoke-rpc-module:rpc-test";
158 mockBodyReader(uri, xmlBodyReader, true);
159 final InputStream inputStream = TestXmlBodyReader.class
160 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
161 final NormalizedNodeContext returnValue = xmlBodyReader
162 .readFrom(null, null, null, mediaType, null, inputStream);
163 checkNormalizedNodeContext(returnValue);
164 final ContainerNode contNode = (ContainerNode) returnValue.getData();
165 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "lf"));
166 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contNode.getChild(yangleaf.getLastPathArgument());
167 assertTrue(leafDataNode.isPresent());
168 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
171 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
172 final NormalizedNodeContext nnContext) {
173 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
176 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
177 final NormalizedNodeContext nnContext,
178 final YangInstanceIdentifier dataNodeIdent) {
179 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
180 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
181 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));