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;
15 import com.google.common.base.Optional;
16 import com.google.common.collect.Sets;
18 import java.io.InputStream;
20 import java.text.ParseException;
21 import java.text.SimpleDateFormat;
22 import java.util.Collection;
23 import javax.ws.rs.core.MediaType;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
27 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
28 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.QNameModule;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
36 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 * org.opendaylight.controller.sal.rest.impl.test.providers
47 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
49 * Created: Mar 7, 2015
51 public class TestXmlBodyReader extends AbstractBodyReaderTest {
53 private final XmlNormalizedNodeBodyReader xmlBodyReader;
54 private static SchemaContext schemaContext;
55 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
57 private static QNameModule initializeInstanceIdentifierModule() {
59 return QNameModule.create(URI.create("instance:identifier:module"),
60 new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-17"));
61 } catch (final ParseException e) {
66 public TestXmlBodyReader () throws NoSuchFieldException, SecurityException {
68 xmlBodyReader = new XmlNormalizedNodeBodyReader();
72 protected MediaType getMediaType() {
73 return new MediaType(MediaType.APPLICATION_XML, null);
77 public static void initialization() throws Exception {
78 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
79 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
80 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
81 schemaContext = TestRestconfUtils.parseYangSources(testFiles);
82 controllerContext.setSchemas(schemaContext);
86 public void moduleDataTest() throws Exception {
87 final DataSchemaNode dataSchemaNode =
88 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
89 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
90 final String uri = "instance-identifier-module:cont";
91 mockBodyReader(uri, xmlBodyReader, false);
92 final InputStream inputStream = TestXmlBodyReader.class
93 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
94 final NormalizedNodeContext returnValue = xmlBodyReader
95 .readFrom(null, null, null, mediaType, null, inputStream);
96 checkNormalizedNodeContext(returnValue);
97 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
101 public void moduleSubContainerDataPutTest() throws Exception {
102 final DataSchemaNode dataSchemaNode =
103 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
104 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
105 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
106 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
107 final String uri = "instance-identifier-module:cont/cont1";
108 mockBodyReader(uri, xmlBodyReader, false);
109 final InputStream inputStream = TestXmlBodyReader.class
110 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
111 final NormalizedNodeContext returnValue = xmlBodyReader
112 .readFrom(null, null, null, mediaType, null, inputStream);
113 checkNormalizedNodeContext(returnValue);
114 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
118 public void moduleSubContainerDataPostTest() throws Exception {
119 final DataSchemaNode dataSchemaNode =
120 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
121 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
122 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
123 final String uri = "instance-identifier-module:cont";
124 mockBodyReader(uri, xmlBodyReader, true);
125 final InputStream inputStream = TestXmlBodyReader.class
126 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
127 final NormalizedNodeContext returnValue = xmlBodyReader
128 .readFrom(null, null, null, mediaType, null, inputStream);
129 checkNormalizedNodeContext(returnValue);
130 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
134 public void moduleSubContainerAugmentDataPostTest() throws Exception {
135 final DataSchemaNode dataSchemaNode =
136 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
137 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
138 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
139 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
140 Sets.newHashSet(contAugmentQName));
141 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
142 .node(augII).node(contAugmentQName);
143 final String uri = "instance-identifier-module:cont";
144 mockBodyReader(uri, xmlBodyReader, true);
145 final InputStream inputStream = TestXmlBodyReader.class
146 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
147 final NormalizedNodeContext returnValue = xmlBodyReader
148 .readFrom(null, null, null, mediaType, null, inputStream);
149 checkNormalizedNodeContext(returnValue);
150 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
154 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
155 final DataSchemaNode dataSchemaNode =
156 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
157 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
158 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
159 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
160 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
161 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
162 Sets.newHashSet(augmentChoice1QName));
163 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
164 Sets.newHashSet(augmentChoice2QName));
165 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
166 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
167 .node(containerQName);
168 final String uri = "instance-identifier-module:cont";
169 mockBodyReader(uri, xmlBodyReader, true);
170 final InputStream inputStream = TestXmlBodyReader.class
171 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
172 final NormalizedNodeContext returnValue = xmlBodyReader
173 .readFrom(null, null, null, mediaType, null, inputStream);
174 checkNormalizedNodeContext(returnValue);
175 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
179 public void rpcModuleInputTest() throws Exception {
180 final String uri = "invoke-rpc-module:rpc-test";
181 mockBodyReader(uri, xmlBodyReader, true);
182 final InputStream inputStream = TestXmlBodyReader.class
183 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
184 final NormalizedNodeContext returnValue = xmlBodyReader
185 .readFrom(null, null, null, mediaType, null, inputStream);
186 checkNormalizedNodeContext(returnValue);
187 final ContainerNode contNode = (ContainerNode) returnValue.getData();
188 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
189 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(yangCont
190 .getLastPathArgument());
191 assertTrue(contDataNodePotential.isPresent());
192 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
193 final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
194 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(yangLeaf
195 .getLastPathArgument());
196 assertTrue(leafDataNode.isPresent());
197 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
200 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
201 final NormalizedNodeContext nnContext) {
202 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
205 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
206 final NormalizedNodeContext nnContext,
207 final YangInstanceIdentifier dataNodeIdent) {
208 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
209 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
210 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));