2 * Copyright (c) 2016 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.restconf.jersey.providers;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
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.dom.api.DOMMountPointService;
27 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
28 import org.opendaylight.controller.sal.rest.impl.test.providers.TestXmlBodyReader;
29 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
30 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
37 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
38 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
43 public class XmlBodyReaderTest extends AbstractBodyReaderTest {
45 private final XmlNormalizedNodeBodyReader xmlBodyReader;
46 private static SchemaContext schemaContext;
47 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
49 private static QNameModule initializeInstanceIdentifierModule() {
51 return QNameModule.create(URI.create("instance:identifier:module"),
52 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
53 } catch (final ParseException e) {
58 public XmlBodyReaderTest() throws Exception {
60 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
64 protected MediaType getMediaType() {
65 return new MediaType(MediaType.APPLICATION_XML, null);
69 public static void initialization() throws Exception {
70 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
71 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
72 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
73 CONTROLLER_CONTEXT.setSchemas(schemaContext);
74 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
78 public void moduleDataTest() throws Exception {
79 final DataSchemaNode dataSchemaNode = schemaContext
80 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
81 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
82 final String uri = "instance-identifier-module:cont";
83 mockBodyReader(uri, this.xmlBodyReader, false);
84 final InputStream inputStream = XmlBodyReaderTest.class
85 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
86 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
88 checkNormalizedNodeContext(returnValue);
89 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
93 public void moduleSubContainerDataPutTest() throws Exception {
94 final DataSchemaNode dataSchemaNode = schemaContext
95 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
96 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
97 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
98 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
99 final String uri = "instance-identifier-module:cont/cont1";
100 mockBodyReader(uri, this.xmlBodyReader, false);
101 final InputStream inputStream = XmlBodyReaderTest.class
102 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
103 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
105 checkNormalizedNodeContext(returnValue);
106 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
110 public void moduleSubContainerDataPostTest() throws Exception {
111 final DataSchemaNode dataSchemaNode = schemaContext
112 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
113 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
114 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
115 final String uri = "instance-identifier-module:cont";
116 mockBodyReader(uri, this.xmlBodyReader, true);
117 final InputStream inputStream = XmlBodyReaderTest.class
118 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
119 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
121 checkNormalizedNodeContext(returnValue);
122 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
126 public void moduleSubContainerAugmentDataPostTest() throws Exception {
127 final DataSchemaNode dataSchemaNode = schemaContext
128 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
129 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
130 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
131 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
132 Sets.newHashSet(contAugmentQName));
133 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
134 .node(contAugmentQName);
135 final String uri = "instance-identifier-module:cont";
136 mockBodyReader(uri, this.xmlBodyReader, true);
137 final InputStream inputStream = XmlBodyReaderTest.class
138 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
139 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
141 checkNormalizedNodeContext(returnValue);
142 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
146 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
147 final DataSchemaNode dataSchemaNode = schemaContext
148 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
149 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
150 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
151 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
152 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
153 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
154 Sets.newHashSet(augmentChoice1QName));
155 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
156 Sets.newHashSet(augmentChoice2QName));
157 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
158 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
159 final String uri = "instance-identifier-module:cont";
160 mockBodyReader(uri, this.xmlBodyReader, true);
161 final InputStream inputStream = XmlBodyReaderTest.class
162 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
163 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
165 checkNormalizedNodeContext(returnValue);
166 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
169 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
170 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
171 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
172 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
173 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
177 * Test when container with the same name is placed in two modules
178 * (foo-module and bar-module). Namespace must be used to distinguish
179 * between them to find correct one. Check if container was found not only
180 * according to its name but also by correct namespace used in payload.
183 public void findFooContainerUsingNamespaceTest() throws Exception {
184 mockBodyReader("", this.xmlBodyReader, true);
185 final InputStream inputStream = XmlBodyReaderTest.class
186 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
187 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
190 // check return value
191 checkNormalizedNodeContext(returnValue);
192 // check if container was found both according to its name and namespace
193 assertEquals("Not correct container found, name was ignored", "foo-bar-container",
194 returnValue.getData().getNodeType().getLocalName());
195 assertEquals("Not correct container found, namespace was ignored", "foo:module",
196 returnValue.getData().getNodeType().getNamespace().toString());
200 * Test when container with the same name is placed in two modules
201 * (foo-module and bar-module). Namespace must be used to distinguish
202 * between them to find correct one. Check if container was found not only
203 * according to its name but also by correct namespace used in payload.
206 public void findBarContainerUsingNamespaceTest() throws Exception {
207 mockBodyReader("", this.xmlBodyReader, true);
208 final InputStream inputStream = XmlBodyReaderTest.class
209 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
210 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
213 // check return value
214 checkNormalizedNodeContext(returnValue);
215 // check if container was found both according to its name and namespace
216 assertEquals("Not correct container found, name was ignored", "foo-bar-container",
217 returnValue.getData().getNodeType().getLocalName());
218 assertEquals("Not correct container found, namespace was ignored", "bar:module",
219 returnValue.getData().getNodeType().getNamespace().toString());
223 * Test PUT operation when message root element is not the same as the last element in request URI.
224 * PUT operation message should always start with schema node from URI otherwise exception should be
228 public void wrongRootElementTest() throws Exception {
229 mockBodyReader("instance-identifier-module:cont", this.xmlBodyReader, false);
230 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
231 "/instanceidentifier/xml/bug7933.xml");
233 this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
234 Assert.fail("Test should fail due to malformed PUT operation message");
235 } catch (final RestconfDocumentedException exception) {
236 final RestconfError restconfError = exception.getErrors().get(0);
237 Assert.assertEquals(RestconfError.ErrorType.PROTOCOL, restconfError.getErrorType());
238 Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());