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;
15 import java.io.InputStream;
17 import java.text.ParseException;
18 import java.text.SimpleDateFormat;
19 import java.util.Collection;
20 import javax.ws.rs.core.MediaType;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
24 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
25 import org.opendaylight.restconf.jersey.providers.XmlNormalizedNodeBodyReader;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import com.google.common.collect.Sets;
36 public class Draft17XmlBodyReaderTest extends Draft17AbstractBodyReaderTest {
38 private final XmlNormalizedNodeBodyReader xmlBodyReader;
39 private static SchemaContext schemaContext;
40 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
42 private static QNameModule initializeInstanceIdentifierModule() {
44 return QNameModule.create(URI.create("instance:identifier:module"),
45 new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-17"));
46 } catch (final ParseException e) {
51 public Draft17XmlBodyReaderTest() throws NoSuchFieldException, SecurityException {
53 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
57 protected MediaType getMediaType() {
58 return new MediaType(MediaType.APPLICATION_XML, null);
62 public static void initialization() throws Exception {
63 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
64 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
65 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
66 schemaContext = TestRestconfUtils.parseYangSources(testFiles);
67 controllerContext.setSchemas(schemaContext);
71 public void moduleDataTest() throws Exception {
72 final DataSchemaNode dataSchemaNode = schemaContext
73 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
74 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
75 final String uri = "instance-identifier-module:cont";
76 mockBodyReader(uri, this.xmlBodyReader, false);
77 final InputStream inputStream = Draft17XmlBodyReaderTest.class
78 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
79 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
81 checkNormalizedNodeContext(returnValue);
82 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
86 public void moduleSubContainerDataPutTest() throws Exception {
87 final DataSchemaNode dataSchemaNode = schemaContext
88 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
89 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
90 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
91 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
92 final String uri = "instance-identifier-module:cont/cont1";
93 mockBodyReader(uri, this.xmlBodyReader, false);
94 final InputStream inputStream = Draft17XmlBodyReaderTest.class
95 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
96 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
98 checkNormalizedNodeContext(returnValue);
99 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
103 public void moduleSubContainerDataPostTest() throws Exception {
104 final DataSchemaNode dataSchemaNode = schemaContext
105 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
106 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
107 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
108 final String uri = "instance-identifier-module:cont";
109 mockBodyReader(uri, this.xmlBodyReader, true);
110 final InputStream inputStream = Draft17XmlBodyReaderTest.class
111 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
112 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
114 checkNormalizedNodeContext(returnValue);
115 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
119 public void moduleSubContainerAugmentDataPostTest() throws Exception {
120 final DataSchemaNode dataSchemaNode = schemaContext
121 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
122 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
123 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
124 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
125 Sets.newHashSet(contAugmentQName));
126 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
127 .node(contAugmentQName);
128 final String uri = "instance-identifier-module:cont";
129 mockBodyReader(uri, this.xmlBodyReader, true);
130 final InputStream inputStream = Draft17XmlBodyReaderTest.class
131 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
132 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
134 checkNormalizedNodeContext(returnValue);
135 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
139 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
140 final DataSchemaNode dataSchemaNode = schemaContext
141 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
142 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
143 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
144 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
145 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
146 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
147 Sets.newHashSet(augmentChoice1QName));
148 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
149 Sets.newHashSet(augmentChoice2QName));
150 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
151 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
152 final String uri = "instance-identifier-module:cont";
153 mockBodyReader(uri, this.xmlBodyReader, true);
154 final InputStream inputStream = Draft17XmlBodyReaderTest.class
155 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
156 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
158 checkNormalizedNodeContext(returnValue);
159 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
162 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
163 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
164 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
165 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
166 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
170 * Test when container with the same name is placed in two modules
171 * (foo-module and bar-module). Namespace must be used to distinguish
172 * between them to find correct one. Check if container was found not only
173 * according to its name but also by correct namespace used in payload.
176 public void findFooContainerUsingNamespaceTest() throws Exception {
177 mockBodyReader("", this.xmlBodyReader, true);
178 final InputStream inputStream = Draft17XmlBodyReaderTest.class
179 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
180 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
183 // check return value
184 checkNormalizedNodeContext(returnValue);
185 // check if container was found both according to its name and namespace
186 assertEquals("Not correct container found, name was ignored", "foo-bar-container",
187 returnValue.getData().getNodeType().getLocalName());
188 assertEquals("Not correct container found, namespace was ignored", "foo:module",
189 returnValue.getData().getNodeType().getNamespace().toString());
193 * Test when container with the same name is placed in two modules
194 * (foo-module and bar-module). Namespace must be used to distinguish
195 * between them to find correct one. Check if container was found not only
196 * according to its name but also by correct namespace used in payload.
199 public void findBarContainerUsingNamespaceTest() throws Exception {
200 mockBodyReader("", this.xmlBodyReader, true);
201 final InputStream inputStream = Draft17XmlBodyReaderTest.class
202 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
203 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
206 // check return value
207 checkNormalizedNodeContext(returnValue);
208 // check if container was found both according to its name and namespace
209 assertEquals("Not correct container found, name was ignored", "foo-bar-container",
210 returnValue.getData().getNodeType().getLocalName());
211 assertEquals("Not correct container found, namespace was ignored", "bar:module",
212 returnValue.getData().getNodeType().getNamespace().toString());