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("/invoke-rpc"));
86 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
87 controllerContext.setSchemas(schemaContext);
91 public void moduleDataTest() throws Exception {
92 final DataSchemaNode dataSchemaNode =
93 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
94 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
95 final String uri = "instance-identifier-module:cont";
96 mockBodyReader(uri, this.jsonBodyReader, false);
97 final InputStream inputStream = TestJsonBodyReader.class
98 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
99 final NormalizedNodeContext returnValue = this.jsonBodyReader
100 .readFrom(null, null, null, this.mediaType, null, inputStream);
101 checkNormalizedNodeContext(returnValue);
102 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
106 public void moduleSubContainerDataPutTest() throws Exception {
107 final DataSchemaNode dataSchemaNode =
108 schemaContext.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 DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
112 final String uri = "instance-identifier-module:cont/cont1";
113 mockBodyReader(uri, this.jsonBodyReader, false);
114 final InputStream inputStream = TestJsonBodyReader.class
115 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
116 final NormalizedNodeContext returnValue = this.jsonBodyReader
117 .readFrom(null, null, null, this.mediaType, null, inputStream);
118 checkNormalizedNodeContext(returnValue);
119 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
123 public void moduleSubContainerDataPostTest() throws Exception {
124 final DataSchemaNode dataSchemaNode =
125 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
126 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
127 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
128 final String uri = "instance-identifier-module:cont";
129 mockBodyReader(uri, this.jsonBodyReader, true);
130 final InputStream inputStream = TestJsonBodyReader.class
131 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
132 final NormalizedNodeContext returnValue = this.jsonBodyReader
133 .readFrom(null, null, null, this.mediaType, null, inputStream);
134 checkNormalizedNodeContext(returnValue);
135 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
139 public void moduleSubContainerAugmentDataPostTest() throws Exception {
140 final DataSchemaNode dataSchemaNode =
141 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
142 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
143 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
144 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
145 Sets.newHashSet(contAugmentQName));
146 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
147 .node(augII).node(contAugmentQName);
148 final String uri = "instance-identifier-module:cont";
149 mockBodyReader(uri, this.jsonBodyReader, true);
150 final InputStream inputStream = TestXmlBodyReader.class
151 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
152 final NormalizedNodeContext returnValue = this.jsonBodyReader
153 .readFrom(null, null, null, this.mediaType, null, inputStream);
154 checkNormalizedNodeContext(returnValue);
155 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
158 //FIXME: Uncomment this when JsonParserStream works correctly with case augmentation with choice
160 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
161 final DataSchemaNode dataSchemaNode =
162 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
163 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
164 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
165 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
166 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
167 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
168 Sets.newHashSet(augmentChoice1QName));
169 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
170 Sets.newHashSet(augmentChoice2QName));
171 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
172 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
173 .node(containerQName);
174 final String uri = "instance-identifier-module:cont";
175 mockBodyReader(uri, this.jsonBodyReader, true);
176 final InputStream inputStream = TestXmlBodyReader.class
177 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
178 final NormalizedNodeContext returnValue = this.jsonBodyReader
179 .readFrom(null, null, null, this.mediaType, null, inputStream);
180 checkNormalizedNodeContext(returnValue);
181 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
185 public void rpcModuleInputTest() throws Exception {
186 final String uri = "invoke-rpc-module:rpc-test";
187 mockBodyReader(uri, this.jsonBodyReader, true);
188 final InputStream inputStream = TestJsonBodyReader.class
189 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
190 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
191 null, null, this.mediaType, null, inputStream);
192 checkNormalizedNodeContext(returnValue);
193 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
194 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
195 .create(inputNode.getNodeType(), "cont"));
196 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
197 .getChild(yangCont.getLastPathArgument());
198 assertTrue(contDataNode.isPresent());
199 assertTrue(contDataNode.get() instanceof ContainerNode);
200 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
201 .create(inputNode.getNodeType(), "lf"));
202 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
203 .get()).getChild(yangleaf.getLastPathArgument());
204 assertTrue(leafDataNode.isPresent());
205 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
209 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
210 final NormalizedNodeContext nnContext) {
211 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
214 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
215 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
216 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
217 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
218 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));