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.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.dom.api.DOMMountPointService;
27 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
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.schema.NormalizedNodes;
33 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
39 public class XmlBodyReaderTest extends AbstractBodyReaderTest {
41 private final XmlNormalizedNodeBodyReader xmlBodyReader;
42 private static SchemaContext schemaContext;
43 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
45 private static QNameModule initializeInstanceIdentifierModule() {
47 return QNameModule.create(URI.create("instance:identifier:module"),
48 new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-17"));
49 } catch (final ParseException e) {
54 public XmlBodyReaderTest() throws Exception {
56 this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
60 protected MediaType getMediaType() {
61 return new MediaType(MediaType.APPLICATION_XML, null);
65 public static void initialization() throws Exception {
66 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
67 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
68 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
69 controllerContext.setSchemas(schemaContext);
70 when(mountPointServiceHandler.get()).thenReturn(mock(DOMMountPointService.class));
74 public void moduleDataTest() throws Exception {
75 final DataSchemaNode dataSchemaNode = schemaContext
76 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
77 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
78 final String uri = "instance-identifier-module:cont";
79 mockBodyReader(uri, this.xmlBodyReader, false);
80 final InputStream inputStream = XmlBodyReaderTest.class
81 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
82 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
84 checkNormalizedNodeContext(returnValue);
85 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
89 public void moduleSubContainerDataPutTest() throws Exception {
90 final DataSchemaNode dataSchemaNode = schemaContext
91 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
92 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
93 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
94 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
95 final String uri = "instance-identifier-module:cont/cont1";
96 mockBodyReader(uri, this.xmlBodyReader, false);
97 final InputStream inputStream = XmlBodyReaderTest.class
98 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
99 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
101 checkNormalizedNodeContext(returnValue);
102 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
106 public void moduleSubContainerDataPostTest() throws Exception {
107 final DataSchemaNode dataSchemaNode = schemaContext
108 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
109 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
110 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
111 final String uri = "instance-identifier-module:cont";
112 mockBodyReader(uri, this.xmlBodyReader, true);
113 final InputStream inputStream = XmlBodyReaderTest.class
114 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
115 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
117 checkNormalizedNodeContext(returnValue);
118 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
122 public void moduleSubContainerAugmentDataPostTest() throws Exception {
123 final DataSchemaNode dataSchemaNode = schemaContext
124 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
125 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
126 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
127 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
128 Sets.newHashSet(contAugmentQName));
129 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
130 .node(contAugmentQName);
131 final String uri = "instance-identifier-module:cont";
132 mockBodyReader(uri, this.xmlBodyReader, true);
133 final InputStream inputStream = XmlBodyReaderTest.class
134 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
135 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
137 checkNormalizedNodeContext(returnValue);
138 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
142 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
143 final DataSchemaNode dataSchemaNode = schemaContext
144 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
145 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
146 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
147 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
148 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
149 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
150 Sets.newHashSet(augmentChoice1QName));
151 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
152 Sets.newHashSet(augmentChoice2QName));
153 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
154 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
155 final String uri = "instance-identifier-module:cont";
156 mockBodyReader(uri, this.xmlBodyReader, true);
157 final InputStream inputStream = XmlBodyReaderTest.class
158 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
159 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
161 checkNormalizedNodeContext(returnValue);
162 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
165 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
166 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
167 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
168 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
169 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
173 * Test when container with the same name is placed in two modules
174 * (foo-module and bar-module). Namespace must be used to distinguish
175 * between them to find correct one. Check if container was found not only
176 * according to its name but also by correct namespace used in payload.
179 public void findFooContainerUsingNamespaceTest() throws Exception {
180 mockBodyReader("", this.xmlBodyReader, true);
181 final InputStream inputStream = XmlBodyReaderTest.class
182 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
183 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
186 // check return value
187 checkNormalizedNodeContext(returnValue);
188 // check if container was found both according to its name and namespace
189 assertEquals("Not correct container found, name was ignored", "foo-bar-container",
190 returnValue.getData().getNodeType().getLocalName());
191 assertEquals("Not correct container found, namespace was ignored", "foo:module",
192 returnValue.getData().getNodeType().getNamespace().toString());
196 * Test when container with the same name is placed in two modules
197 * (foo-module and bar-module). Namespace must be used to distinguish
198 * between them to find correct one. Check if container was found not only
199 * according to its name but also by correct namespace used in payload.
202 public void findBarContainerUsingNamespaceTest() throws Exception {
203 mockBodyReader("", this.xmlBodyReader, true);
204 final InputStream inputStream = XmlBodyReaderTest.class
205 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
206 final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
209 // check return value
210 checkNormalizedNodeContext(returnValue);
211 // check if container was found both according to its name and namespace
212 assertEquals("Not correct container found, name was ignored", "foo-bar-container",
213 returnValue.getData().getNodeType().getLocalName());
214 assertEquals("Not correct container found, namespace was ignored", "bar:module",
215 returnValue.getData().getNodeType().getNamespace().toString());