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.Assert;
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.netconf.sal.restconf.impl.RestconfDocumentedException;
30 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.common.QNameModule;
33 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
39 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
40 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.Module;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45 public class TestXmlBodyReader extends AbstractBodyReaderTest {
47 private final XmlNormalizedNodeBodyReader xmlBodyReader;
48 private static SchemaContext schemaContext;
49 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
51 private static QNameModule initializeInstanceIdentifierModule() {
53 return QNameModule.create(URI.create("instance:identifier:module"),
54 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
55 } catch (final ParseException e) {
60 public TestXmlBodyReader() throws Exception {
62 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
66 protected MediaType getMediaType() {
67 return new MediaType(MediaType.APPLICATION_XML, null);
71 public static void initialization() throws Exception {
72 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
73 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
74 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
75 CONTROLLER_CONTEXT.setSchemas(schemaContext);
79 public void moduleDataTest() throws Exception {
80 final DataSchemaNode dataSchemaNode =
81 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
82 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
83 final String uri = "instance-identifier-module:cont";
84 mockBodyReader(uri, this.xmlBodyReader, false);
85 final InputStream inputStream = TestXmlBodyReader.class
86 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
87 final NormalizedNodeContext returnValue = this.xmlBodyReader
88 .readFrom(null, null, null, this.mediaType, null, inputStream);
89 checkNormalizedNodeContext(returnValue);
90 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
94 public void moduleSubContainerDataPutTest() throws Exception {
95 final DataSchemaNode dataSchemaNode =
96 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
97 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
98 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
99 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
100 final String uri = "instance-identifier-module:cont/cont1";
101 mockBodyReader(uri, this.xmlBodyReader, false);
102 final InputStream inputStream = TestXmlBodyReader.class
103 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
104 final NormalizedNodeContext returnValue = this.xmlBodyReader
105 .readFrom(null, null, null, this.mediaType, null, inputStream);
106 checkNormalizedNodeContext(returnValue);
107 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
111 public void moduleSubContainerDataPostTest() throws Exception {
112 final DataSchemaNode dataSchemaNode =
113 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
114 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
115 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
116 final String uri = "instance-identifier-module:cont";
117 mockBodyReader(uri, this.xmlBodyReader, true);
118 final InputStream inputStream = TestXmlBodyReader.class
119 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
120 final NormalizedNodeContext returnValue = this.xmlBodyReader
121 .readFrom(null, null, null, this.mediaType, null, inputStream);
122 checkNormalizedNodeContext(returnValue);
123 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
127 public void moduleSubContainerAugmentDataPostTest() throws Exception {
128 final DataSchemaNode dataSchemaNode =
129 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
130 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
131 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
132 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
133 Sets.newHashSet(contAugmentQName));
134 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
135 .node(augII).node(contAugmentQName);
136 final String uri = "instance-identifier-module:cont";
137 mockBodyReader(uri, this.xmlBodyReader, true);
138 final InputStream inputStream = TestXmlBodyReader.class
139 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
140 final NormalizedNodeContext returnValue = this.xmlBodyReader
141 .readFrom(null, null, null, this.mediaType, null, inputStream);
142 checkNormalizedNodeContext(returnValue);
143 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
147 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
148 final DataSchemaNode dataSchemaNode =
149 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
150 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
151 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
152 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
153 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
154 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
155 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
156 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
157 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
158 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
159 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
160 .node(containerQName);
161 final String uri = "instance-identifier-module:cont";
162 mockBodyReader(uri, this.xmlBodyReader, true);
163 final InputStream inputStream = TestXmlBodyReader.class
164 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
165 final NormalizedNodeContext returnValue = this.xmlBodyReader
166 .readFrom(null, null, null, this.mediaType, null, inputStream);
167 checkNormalizedNodeContext(returnValue);
168 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
172 public void rpcModuleInputTest() throws Exception {
173 final String uri = "invoke-rpc-module:rpc-test";
174 mockBodyReader(uri, this.xmlBodyReader, true);
175 final InputStream inputStream = TestXmlBodyReader.class
176 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
177 final NormalizedNodeContext returnValue = this.xmlBodyReader
178 .readFrom(null, null, null, this.mediaType, null, inputStream);
179 checkNormalizedNodeContext(returnValue);
180 final ContainerNode contNode = (ContainerNode) returnValue.getData();
181 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
182 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(yangCont
183 .getLastPathArgument());
184 assertTrue(contDataNodePotential.isPresent());
185 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
186 final YangInstanceIdentifier yangLeaf =
187 YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
188 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(yangLeaf
189 .getLastPathArgument());
190 assertTrue(leafDataNode.isPresent());
191 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
194 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
195 final NormalizedNodeContext nnContext) {
196 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
199 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
200 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
201 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
202 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
203 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
207 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
208 * used to distinguish between them to find correct one. Check if container was found not only according to its name
209 * but also by correct namespace used in payload.
212 public void findFooContainerUsingNamespaceTest() throws Exception {
213 mockBodyReader("", this.xmlBodyReader, true);
214 final InputStream inputStream = TestXmlBodyReader.class
215 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
216 final NormalizedNodeContext returnValue = this.xmlBodyReader
217 .readFrom(null, null, null, this.mediaType, null, inputStream);
219 // check return value
220 checkNormalizedNodeContext(returnValue);
221 // check if container was found both according to its name and namespace
222 assertEquals("Not correct container found, name was ignored",
223 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
224 assertEquals("Not correct container found, namespace was ignored",
225 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
229 * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
230 * used to distinguish between them to find correct one. Check if container was found not only according to its name
231 * but also by correct namespace used in payload.
234 public void findBarContainerUsingNamespaceTest() throws Exception {
235 mockBodyReader("", this.xmlBodyReader, true);
236 final InputStream inputStream = TestXmlBodyReader.class
237 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
238 final NormalizedNodeContext returnValue = this.xmlBodyReader
239 .readFrom(null, null, null, this.mediaType, null, inputStream);
241 // check return value
242 checkNormalizedNodeContext(returnValue);
243 // check if container was found both according to its name and namespace
244 assertEquals("Not correct container found, name was ignored",
245 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
246 assertEquals("Not correct container found, namespace was ignored",
247 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
251 * Test PUT operation when message root element is not the same as the last element in request URI.
252 * PUT operation message should always start with schema node from URI otherwise exception should be
256 public void wrongRootElementTest() throws Exception {
257 mockBodyReader("instance-identifier-module:cont", this.xmlBodyReader, false);
258 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
259 "/instanceidentifier/xml/bug7933.xml");
261 this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
262 Assert.fail("Test should fail due to malformed PUT operation message");
263 } catch (final RestconfDocumentedException exception) {
264 final RestconfError restconfError = exception.getErrors().get(0);
265 Assert.assertEquals(RestconfError.ErrorType.PROTOCOL, restconfError.getErrorType());
266 Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());