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;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44 * org.opendaylight.controller.sal.rest.impl.test.providers
48 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
50 * Created: Mar 7, 2015
52 public class TestXmlBodyReader extends AbstractBodyReaderTest {
54 private final XmlNormalizedNodeBodyReader xmlBodyReader;
55 private static SchemaContext schemaContext;
56 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
58 private static QNameModule initializeInstanceIdentifierModule() {
60 return QNameModule.create(URI.create("instance:identifier:module"),
61 new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-17"));
62 } catch (final ParseException e) {
67 public TestXmlBodyReader () throws NoSuchFieldException, SecurityException {
69 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
73 protected MediaType getMediaType() {
74 return new MediaType(MediaType.APPLICATION_XML, null);
78 public static void initialization() throws Exception {
79 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
80 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
81 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
82 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
83 controllerContext.setSchemas(schemaContext);
87 public void moduleDataTest() throws Exception {
88 final DataSchemaNode dataSchemaNode =
89 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
90 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
91 final String uri = "instance-identifier-module:cont";
92 mockBodyReader(uri, this.xmlBodyReader, false);
93 final InputStream inputStream = TestXmlBodyReader.class
94 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
95 final NormalizedNodeContext returnValue = this.xmlBodyReader
96 .readFrom(null, null, null, this.mediaType, null, inputStream);
97 checkNormalizedNodeContext(returnValue);
98 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
102 public void moduleSubContainerDataPutTest() throws Exception {
103 final DataSchemaNode dataSchemaNode =
104 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
105 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
106 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
107 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
108 final String uri = "instance-identifier-module:cont/cont1";
109 mockBodyReader(uri, this.xmlBodyReader, false);
110 final InputStream inputStream = TestXmlBodyReader.class
111 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
112 final NormalizedNodeContext returnValue = this.xmlBodyReader
113 .readFrom(null, null, null, this.mediaType, null, inputStream);
114 checkNormalizedNodeContext(returnValue);
115 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
119 public void moduleSubContainerDataPostTest() throws Exception {
120 final DataSchemaNode dataSchemaNode =
121 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
122 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
123 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
124 final String uri = "instance-identifier-module:cont";
125 mockBodyReader(uri, this.xmlBodyReader, true);
126 final InputStream inputStream = TestXmlBodyReader.class
127 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
128 final NormalizedNodeContext returnValue = this.xmlBodyReader
129 .readFrom(null, null, null, this.mediaType, null, inputStream);
130 checkNormalizedNodeContext(returnValue);
131 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
135 public void moduleSubContainerAugmentDataPostTest() throws Exception {
136 final DataSchemaNode dataSchemaNode =
137 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
138 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
139 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
140 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
141 Sets.newHashSet(contAugmentQName));
142 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
143 .node(augII).node(contAugmentQName);
144 final String uri = "instance-identifier-module:cont";
145 mockBodyReader(uri, this.xmlBodyReader, true);
146 final InputStream inputStream = TestXmlBodyReader.class
147 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
148 final NormalizedNodeContext returnValue = this.xmlBodyReader
149 .readFrom(null, null, null, this.mediaType, null, inputStream);
150 checkNormalizedNodeContext(returnValue);
151 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
155 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
156 final DataSchemaNode dataSchemaNode =
157 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
158 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
159 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
160 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
161 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
162 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
163 Sets.newHashSet(augmentChoice1QName));
164 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
165 Sets.newHashSet(augmentChoice2QName));
166 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
167 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
168 .node(containerQName);
169 final String uri = "instance-identifier-module:cont";
170 mockBodyReader(uri, this.xmlBodyReader, true);
171 final InputStream inputStream = TestXmlBodyReader.class
172 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
173 final NormalizedNodeContext returnValue = this.xmlBodyReader
174 .readFrom(null, null, null, this.mediaType, null, inputStream);
175 checkNormalizedNodeContext(returnValue);
176 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
180 public void rpcModuleInputTest() throws Exception {
181 final String uri = "invoke-rpc-module:rpc-test";
182 mockBodyReader(uri, this.xmlBodyReader, true);
183 final InputStream inputStream = TestXmlBodyReader.class
184 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
185 final NormalizedNodeContext returnValue = this.xmlBodyReader
186 .readFrom(null, null, null, this.mediaType, null, inputStream);
187 checkNormalizedNodeContext(returnValue);
188 final ContainerNode contNode = (ContainerNode) returnValue.getData();
189 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
190 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(yangCont
191 .getLastPathArgument());
192 assertTrue(contDataNodePotential.isPresent());
193 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
194 final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
195 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(yangLeaf
196 .getLastPathArgument());
197 assertTrue(leafDataNode.isPresent());
198 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
201 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
202 final NormalizedNodeContext nnContext) {
203 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
206 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
207 final NormalizedNodeContext nnContext,
208 final YangInstanceIdentifier dataNodeIdent) {
209 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
210 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
211 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
215 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
216 * used to distinguish between them to find correct one. Check if container was found not only according to its name
217 * but also by correct namespace used in payload.
220 public void findFooContainerUsingNamespaceTest() throws Exception {
221 mockBodyReader("", this.xmlBodyReader, true);
222 final InputStream inputStream = TestXmlBodyReader.class
223 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
224 final NormalizedNodeContext returnValue = this.xmlBodyReader
225 .readFrom(null, null, null, this.mediaType, null, inputStream);
227 // check return value
228 checkNormalizedNodeContext(returnValue);
229 // check if container was found both according to its name and namespace
230 assertEquals("Not correct container found, name was ignored",
231 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
232 assertEquals("Not correct container found, namespace was ignored",
233 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
237 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
238 * used to distinguish between them to find correct one. Check if container was found not only according to its name
239 * but also by correct namespace used in payload.
242 public void findBarContainerUsingNamespaceTest() throws Exception {
243 mockBodyReader("", this.xmlBodyReader, true);
244 final InputStream inputStream = TestXmlBodyReader.class
245 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
246 final NormalizedNodeContext returnValue = this.xmlBodyReader
247 .readFrom(null, null, null, this.mediaType, null, inputStream);
249 // check return value
250 checkNormalizedNodeContext(returnValue);
251 // check if container was found both according to its name and namespace
252 assertEquals("Not correct container found, name was ignored",
253 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
254 assertEquals("Not correct container found, namespace was ignored",
255 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());