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;
16 import java.io.InputStream;
18 import java.util.Collection;
20 import javax.ws.rs.core.MediaType;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
26 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
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;
38 import com.google.common.base.Optional;
39 import com.google.common.collect.Sets;
43 * org.opendaylight.controller.sal.rest.impl.test.providers
47 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
49 * Created: Mar 7, 2015
51 public class TestXmlBodyReader extends AbstractBodyReaderTest {
53 private final XmlNormalizedNodeBodyReader xmlBodyReader;
54 private static SchemaContext schemaContext;
56 public TestXmlBodyReader () throws NoSuchFieldException, SecurityException {
58 xmlBodyReader = new XmlNormalizedNodeBodyReader();
62 protected MediaType getMediaType() {
63 return new MediaType(MediaType.APPLICATION_XML, null);
67 public static void initialization() throws Exception {
68 Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
69 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
70 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
71 schemaContext = TestRestconfUtils.parseYangSources(testFiles);
72 controllerContext.setSchemas(schemaContext);
76 public void moduleDataTest() throws Exception {
77 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
78 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
79 final String uri = "instance-identifier-module:cont";
80 mockBodyReader(uri, xmlBodyReader, false);
81 final InputStream inputStream = TestXmlBodyReader.class
82 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
83 final NormalizedNodeContext returnValue = xmlBodyReader
84 .readFrom(null, null, null, mediaType, null, inputStream);
85 checkNormalizedNodeContext(returnValue);
86 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
90 public void moduleSubContainerDataPutTest() throws Exception {
91 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
92 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, xmlBodyReader, false);
97 final InputStream inputStream = TestXmlBodyReader.class
98 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
99 final NormalizedNodeContext returnValue = xmlBodyReader
100 .readFrom(null, null, null, mediaType, null, inputStream);
101 checkNormalizedNodeContext(returnValue);
102 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
106 public void moduleSubContainerDataPostTest() throws Exception {
107 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
108 QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
109 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
110 final String uri = "instance-identifier-module:cont";
111 mockBodyReader(uri, xmlBodyReader, true);
112 final InputStream inputStream = TestXmlBodyReader.class
113 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
114 final NormalizedNodeContext returnValue = xmlBodyReader
115 .readFrom(null, null, null, mediaType, null, inputStream);
116 checkNormalizedNodeContext(returnValue);
117 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
121 public void moduleSubContainerAugmentDataPostTest() throws Exception {
122 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
123 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
124 QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
125 YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
126 Sets.newHashSet(contAugmentQName));
127 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
128 .node(augII).node(contAugmentQName);
129 final String uri = "instance-identifier-module:cont";
130 mockBodyReader(uri, xmlBodyReader, true);
131 final InputStream inputStream = TestXmlBodyReader.class
132 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
133 final NormalizedNodeContext returnValue = xmlBodyReader
134 .readFrom(null, null, null, mediaType, null, inputStream);
135 checkNormalizedNodeContext(returnValue);
136 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
140 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
141 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
142 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
143 QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
144 QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
145 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
146 YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
147 Sets.newHashSet(augmentChoice1QName));
148 YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
149 Sets.newHashSet(augmentChoice2QName));
150 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
151 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
152 .node(containerQName);
153 final String uri = "instance-identifier-module:cont";
154 mockBodyReader(uri, xmlBodyReader, true);
155 final InputStream inputStream = TestXmlBodyReader.class
156 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
157 final NormalizedNodeContext returnValue = xmlBodyReader
158 .readFrom(null, null, null, mediaType, null, inputStream);
159 checkNormalizedNodeContext(returnValue);
160 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
164 public void rpcModuleInputTest() throws Exception {
165 final String uri = "invoke-rpc-module:rpc-test";
166 mockBodyReader(uri, xmlBodyReader, true);
167 final InputStream inputStream = TestXmlBodyReader.class
168 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
169 final NormalizedNodeContext returnValue = xmlBodyReader
170 .readFrom(null, null, null, mediaType, null, inputStream);
171 checkNormalizedNodeContext(returnValue);
172 final ContainerNode contNode = (ContainerNode) returnValue.getData();
173 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
174 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(yangCont
175 .getLastPathArgument());
176 assertTrue(contDataNodePotential.isPresent());
177 final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
178 final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
179 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(yangLeaf
180 .getLastPathArgument());
181 assertTrue(leafDataNode.isPresent());
182 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
185 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
186 final NormalizedNodeContext nnContext) {
187 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
190 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
191 final NormalizedNodeContext nnContext,
192 final YangInstanceIdentifier dataNodeIdent) {
193 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
194 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
195 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));