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.FileNotFoundException;
19 import java.io.InputStream;
21 import java.text.ParseException;
22 import java.text.SimpleDateFormat;
23 import java.util.Collection;
24 import javax.ws.rs.core.MediaType;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
28 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
29 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.QNameModule;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
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.parser.spi.meta.ReactorException;
42 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47 * org.opendaylight.controller.sal.rest.impl.test.providers
51 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
53 * Created: Mar 11, 2015
55 public class TestJsonBodyReader extends AbstractBodyReaderTest {
57 private final JsonNormalizedNodeBodyReader jsonBodyReader;
58 private static SchemaContext schemaContext;
60 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
62 private static QNameModule initializeInstanceIdentifierModule() {
64 return QNameModule.create(URI.create("instance:identifier:module"),
65 new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-17"));
66 } catch (final ParseException e) {
72 public TestJsonBodyReader () throws NoSuchFieldException, SecurityException {
74 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
78 protected MediaType getMediaType() {
79 return new MediaType(MediaType.APPLICATION_XML, null);
83 public static void initialization() throws NoSuchFieldException, SecurityException, FileNotFoundException, SourceException, ReactorException {
84 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
85 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
86 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
87 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
88 controllerContext.setSchemas(schemaContext);
92 public void moduleDataTest() throws Exception {
93 final DataSchemaNode dataSchemaNode =
94 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
95 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
96 final String uri = "instance-identifier-module:cont";
97 mockBodyReader(uri, this.jsonBodyReader, false);
98 final InputStream inputStream = TestJsonBodyReader.class
99 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
100 final NormalizedNodeContext returnValue = this.jsonBodyReader
101 .readFrom(null, null, null, this.mediaType, null, inputStream);
102 checkNormalizedNodeContext(returnValue);
103 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
107 public void moduleSubContainerDataPutTest() throws Exception {
108 final DataSchemaNode dataSchemaNode =
109 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
110 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
111 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
112 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
113 final String uri = "instance-identifier-module:cont/cont1";
114 mockBodyReader(uri, this.jsonBodyReader, false);
115 final InputStream inputStream = TestJsonBodyReader.class
116 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
117 final NormalizedNodeContext returnValue = this.jsonBodyReader
118 .readFrom(null, null, null, this.mediaType, null, inputStream);
119 checkNormalizedNodeContext(returnValue);
120 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
124 public void moduleSubContainerDataPostTest() throws Exception {
125 final DataSchemaNode dataSchemaNode =
126 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
127 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
128 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
129 final String uri = "instance-identifier-module:cont";
130 mockBodyReader(uri, this.jsonBodyReader, true);
131 final InputStream inputStream = TestJsonBodyReader.class
132 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
133 final NormalizedNodeContext returnValue = this.jsonBodyReader
134 .readFrom(null, null, null, this.mediaType, null, inputStream);
135 checkNormalizedNodeContext(returnValue);
136 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
140 public void moduleSubContainerAugmentDataPostTest() throws Exception {
141 final DataSchemaNode dataSchemaNode =
142 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
143 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
144 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
145 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
146 Sets.newHashSet(contAugmentQName));
147 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
148 .node(augII).node(contAugmentQName);
149 final String uri = "instance-identifier-module:cont";
150 mockBodyReader(uri, this.jsonBodyReader, true);
151 final InputStream inputStream = TestXmlBodyReader.class
152 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
153 final NormalizedNodeContext returnValue = this.jsonBodyReader
154 .readFrom(null, null, null, this.mediaType, null, inputStream);
155 checkNormalizedNodeContext(returnValue);
156 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
159 //FIXME: Uncomment this when JsonParserStream works correctly with case augmentation with choice
161 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
162 final DataSchemaNode dataSchemaNode =
163 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
164 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
165 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
166 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
167 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
168 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
169 Sets.newHashSet(augmentChoice1QName));
170 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
171 Sets.newHashSet(augmentChoice2QName));
172 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
173 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
174 .node(containerQName);
175 final String uri = "instance-identifier-module:cont";
176 mockBodyReader(uri, this.jsonBodyReader, true);
177 final InputStream inputStream = TestXmlBodyReader.class
178 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
179 final NormalizedNodeContext returnValue = this.jsonBodyReader
180 .readFrom(null, null, null, this.mediaType, null, inputStream);
181 checkNormalizedNodeContext(returnValue);
182 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
186 public void rpcModuleInputTest() throws Exception {
187 final String uri = "invoke-rpc-module:rpc-test";
188 mockBodyReader(uri, this.jsonBodyReader, true);
189 final InputStream inputStream = TestJsonBodyReader.class
190 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
191 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
192 null, null, this.mediaType, null, inputStream);
193 checkNormalizedNodeContext(returnValue);
194 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
195 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
196 .create(inputNode.getNodeType(), "cont"));
197 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
198 .getChild(yangCont.getLastPathArgument());
199 assertTrue(contDataNode.isPresent());
200 assertTrue(contDataNode.get() instanceof ContainerNode);
201 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
202 .create(inputNode.getNodeType(), "lf"));
203 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
204 .get()).getChild(yangleaf.getLastPathArgument());
205 assertTrue(leafDataNode.isPresent());
206 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
210 private void checkExpectValueNormalizeNodeContext(
211 final DataSchemaNode dataSchemaNode,
212 final NormalizedNodeContext nnContext) {
213 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
216 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
217 final NormalizedNodeContext nnContext,
218 final YangInstanceIdentifier dataNodeIdent) {
219 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
220 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
221 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));