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.InputStream;
20 import java.text.ParseException;
21 import java.util.Collection;
22 import javax.ws.rs.core.MediaType;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
26 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
27 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.QNameModule;
30 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
36 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
43 public class TestJsonBodyReader extends AbstractBodyReaderTest {
45 private final JsonNormalizedNodeBodyReader jsonBodyReader;
46 private static SchemaContext schemaContext;
48 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
50 private static QNameModule initializeInstanceIdentifierModule() {
52 return QNameModule.create(URI.create("instance:identifier:module"),
53 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
54 } catch (final ParseException e) {
60 public TestJsonBodyReader() throws NoSuchFieldException, SecurityException {
61 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
65 protected MediaType getMediaType() {
66 return new MediaType(MediaType.APPLICATION_XML, null);
70 public static void initialization()
72 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
73 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
74 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
75 CONTROLLER_CONTEXT.setSchemas(schemaContext);
79 public void moduleDataTest() throws Exception {
80 final DataSchemaNode dataSchemaNode =
81 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
82 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
83 final String uri = "instance-identifier-module:cont";
84 mockBodyReader(uri, this.jsonBodyReader, false);
85 final InputStream inputStream = TestJsonBodyReader.class
86 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
87 final NormalizedNodeContext returnValue = this.jsonBodyReader
88 .readFrom(null, null, null, this.mediaType, null, inputStream);
89 checkNormalizedNodeContext(returnValue);
90 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
94 public void moduleSubContainerDataPutTest() throws Exception {
95 final DataSchemaNode dataSchemaNode =
96 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
97 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
98 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
99 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
100 final String uri = "instance-identifier-module:cont/cont1";
101 mockBodyReader(uri, this.jsonBodyReader, false);
102 final InputStream inputStream = TestJsonBodyReader.class
103 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
104 final NormalizedNodeContext returnValue = this.jsonBodyReader
105 .readFrom(null, null, null, this.mediaType, null, inputStream);
106 checkNormalizedNodeContext(returnValue);
107 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
111 public void moduleSubContainerDataPostTest() throws Exception {
112 final DataSchemaNode dataSchemaNode =
113 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
114 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
115 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
116 final String uri = "instance-identifier-module:cont";
117 mockBodyReader(uri, this.jsonBodyReader, true);
118 final InputStream inputStream = TestJsonBodyReader.class
119 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
120 final NormalizedNodeContext returnValue = this.jsonBodyReader
121 .readFrom(null, null, null, this.mediaType, null, inputStream);
122 checkNormalizedNodeContext(returnValue);
123 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
127 public void moduleSubContainerAugmentDataPostTest() throws Exception {
128 final DataSchemaNode dataSchemaNode =
129 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
130 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
131 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
132 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
133 Sets.newHashSet(contAugmentQName));
134 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
135 .node(augII).node(contAugmentQName);
136 final String uri = "instance-identifier-module:cont";
137 mockBodyReader(uri, this.jsonBodyReader, true);
138 final InputStream inputStream = TestXmlBodyReader.class
139 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
140 final NormalizedNodeContext returnValue = this.jsonBodyReader
141 .readFrom(null, null, null, this.mediaType, null, inputStream);
142 checkNormalizedNodeContext(returnValue);
143 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
146 //FIXME: Uncomment this when JsonParserStream works correctly with case augmentation with choice
148 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
149 final DataSchemaNode dataSchemaNode =
150 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
151 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
152 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
153 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
154 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
155 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
156 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
157 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
158 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
159 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
160 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
161 .node(containerQName);
162 final String uri = "instance-identifier-module:cont";
163 mockBodyReader(uri, this.jsonBodyReader, true);
164 final InputStream inputStream = TestXmlBodyReader.class
165 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
166 final NormalizedNodeContext returnValue = this.jsonBodyReader
167 .readFrom(null, null, null, this.mediaType, null, inputStream);
168 checkNormalizedNodeContext(returnValue);
169 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
173 public void rpcModuleInputTest() throws Exception {
174 final String uri = "invoke-rpc-module:rpc-test";
175 mockBodyReader(uri, this.jsonBodyReader, true);
176 final InputStream inputStream = TestJsonBodyReader.class
177 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
178 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
179 null, null, this.mediaType, null, inputStream);
180 checkNormalizedNodeContext(returnValue);
181 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
182 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
183 .create(inputNode.getNodeType(), "cont"));
184 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
185 .getChild(yangCont.getLastPathArgument());
186 assertTrue(contDataNode.isPresent());
187 assertTrue(contDataNode.get() instanceof ContainerNode);
188 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
189 .create(inputNode.getNodeType(), "lf"));
190 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
191 .get()).getChild(yangleaf.getLastPathArgument());
192 assertTrue(leafDataNode.isPresent());
193 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
197 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
198 final NormalizedNodeContext nnContext) {
199 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
202 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
203 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
204 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
205 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
206 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));