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.util.Collection;
22 import javax.ws.rs.core.MediaType;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
26 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
27 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.QNameModule;
30 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
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;
42 public class TestXmlBodyReader extends AbstractBodyReaderTest {
44 private final XmlNormalizedNodeBodyReader xmlBodyReader;
45 private static SchemaContext schemaContext;
46 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
48 private static QNameModule initializeInstanceIdentifierModule() {
50 return QNameModule.create(URI.create("instance:identifier:module"),
51 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
52 } catch (final ParseException e) {
57 public TestXmlBodyReader() throws NoSuchFieldException, SecurityException {
59 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
63 protected MediaType getMediaType() {
64 return new MediaType(MediaType.APPLICATION_XML, null);
68 public static void initialization() throws Exception {
69 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
70 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
71 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
72 CONTROLLER_CONTEXT.setSchemas(schemaContext);
76 public void moduleDataTest() throws Exception {
77 final DataSchemaNode dataSchemaNode =
78 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
79 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
80 final String uri = "instance-identifier-module:cont";
81 mockBodyReader(uri, this.xmlBodyReader, false);
82 final InputStream inputStream = TestXmlBodyReader.class
83 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
84 final NormalizedNodeContext returnValue = this.xmlBodyReader
85 .readFrom(null, null, null, this.mediaType, null, inputStream);
86 checkNormalizedNodeContext(returnValue);
87 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
91 public void moduleSubContainerDataPutTest() throws Exception {
92 final DataSchemaNode dataSchemaNode =
93 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
94 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
95 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
96 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
97 final String uri = "instance-identifier-module:cont/cont1";
98 mockBodyReader(uri, this.xmlBodyReader, false);
99 final InputStream inputStream = TestXmlBodyReader.class
100 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
101 final NormalizedNodeContext returnValue = this.xmlBodyReader
102 .readFrom(null, null, null, this.mediaType, null, inputStream);
103 checkNormalizedNodeContext(returnValue);
104 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
108 public void moduleSubContainerDataPostTest() throws Exception {
109 final DataSchemaNode dataSchemaNode =
110 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
111 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
112 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
113 final String uri = "instance-identifier-module:cont";
114 mockBodyReader(uri, this.xmlBodyReader, true);
115 final InputStream inputStream = TestXmlBodyReader.class
116 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
117 final NormalizedNodeContext returnValue = this.xmlBodyReader
118 .readFrom(null, null, null, this.mediaType, null, inputStream);
119 checkNormalizedNodeContext(returnValue);
120 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
124 public void moduleSubContainerAugmentDataPostTest() throws Exception {
125 final DataSchemaNode dataSchemaNode =
126 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
127 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
128 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
129 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
130 Sets.newHashSet(contAugmentQName));
131 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
132 .node(augII).node(contAugmentQName);
133 final String uri = "instance-identifier-module:cont";
134 mockBodyReader(uri, this.xmlBodyReader, true);
135 final InputStream inputStream = TestXmlBodyReader.class
136 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
137 final NormalizedNodeContext returnValue = this.xmlBodyReader
138 .readFrom(null, null, null, this.mediaType, null, inputStream);
139 checkNormalizedNodeContext(returnValue);
140 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
144 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
145 final DataSchemaNode dataSchemaNode =
146 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
147 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
148 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
149 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
150 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
151 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
152 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
153 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
154 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
155 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
156 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
157 .node(containerQName);
158 final String uri = "instance-identifier-module:cont";
159 mockBodyReader(uri, this.xmlBodyReader, true);
160 final InputStream inputStream = TestXmlBodyReader.class
161 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
162 final NormalizedNodeContext returnValue = this.xmlBodyReader
163 .readFrom(null, null, null, this.mediaType, null, inputStream);
164 checkNormalizedNodeContext(returnValue);
165 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
169 public void rpcModuleInputTest() throws Exception {
170 final String uri = "invoke-rpc-module:rpc-test";
171 mockBodyReader(uri, this.xmlBodyReader, true);
172 final InputStream inputStream = TestXmlBodyReader.class
173 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
174 final NormalizedNodeContext returnValue = this.xmlBodyReader
175 .readFrom(null, null, null, this.mediaType, null, inputStream);
176 checkNormalizedNodeContext(returnValue);
177 final ContainerNode contNode = (ContainerNode) returnValue.getData();
178 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
179 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(yangCont
180 .getLastPathArgument());
181 assertTrue(contDataNodePotential.isPresent());
182 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
183 final YangInstanceIdentifier yangLeaf =
184 YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
185 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(yangLeaf
186 .getLastPathArgument());
187 assertTrue(leafDataNode.isPresent());
188 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
191 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
192 final NormalizedNodeContext nnContext) {
193 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
196 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
197 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
198 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
199 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
200 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
204 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
205 * used to distinguish between them to find correct one. Check if container was found not only according to its name
206 * but also by correct namespace used in payload.
209 public void findFooContainerUsingNamespaceTest() throws Exception {
210 mockBodyReader("", this.xmlBodyReader, true);
211 final InputStream inputStream = TestXmlBodyReader.class
212 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
213 final NormalizedNodeContext returnValue = this.xmlBodyReader
214 .readFrom(null, null, null, this.mediaType, null, inputStream);
216 // check return value
217 checkNormalizedNodeContext(returnValue);
218 // check if container was found both according to its name and namespace
219 assertEquals("Not correct container found, name was ignored",
220 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
221 assertEquals("Not correct container found, namespace was ignored",
222 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
226 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
227 * used to distinguish between them to find correct one. Check if container was found not only according to its name
228 * but also by correct namespace used in payload.
231 public void findBarContainerUsingNamespaceTest() throws Exception {
232 mockBodyReader("", this.xmlBodyReader, true);
233 final InputStream inputStream = TestXmlBodyReader.class
234 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
235 final NormalizedNodeContext returnValue = this.xmlBodyReader
236 .readFrom(null, null, null, this.mediaType, null, inputStream);
238 // check return value
239 checkNormalizedNodeContext(returnValue);
240 // check if container was found both according to its name and namespace
241 assertEquals("Not correct container found, name was ignored",
242 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
243 assertEquals("Not correct container found, namespace was ignored",
244 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());