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.collect.Sets;
17 import java.io.InputStream;
19 import java.util.Collection;
20 import java.util.Optional;
21 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.JsonNormalizedNodeBodyReader;
26 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.QNameModule;
29 import org.opendaylight.yangtools.yang.common.Revision;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
35 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
41 public class TestJsonBodyReader extends AbstractBodyReaderTest {
43 private final JsonNormalizedNodeBodyReader jsonBodyReader;
44 private static SchemaContext schemaContext;
46 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
47 URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
49 public TestJsonBodyReader() throws NoSuchFieldException, SecurityException {
50 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
54 protected MediaType getMediaType() {
55 return new MediaType(MediaType.APPLICATION_XML, null);
59 public static void initialization()
61 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
62 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
63 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
64 CONTROLLER_CONTEXT.setSchemas(schemaContext);
68 public void moduleDataTest() throws Exception {
69 final DataSchemaNode dataSchemaNode =
70 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
71 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
72 final String uri = "instance-identifier-module:cont";
73 mockBodyReader(uri, this.jsonBodyReader, false);
74 final InputStream inputStream = TestJsonBodyReader.class
75 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
76 final NormalizedNodeContext returnValue = this.jsonBodyReader
77 .readFrom(null, null, null, this.mediaType, null, inputStream);
78 checkNormalizedNodeContext(returnValue);
79 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
83 public void moduleSubContainerDataPutTest() throws Exception {
84 final DataSchemaNode dataSchemaNode =
85 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
86 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
87 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
88 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
89 final String uri = "instance-identifier-module:cont/cont1";
90 mockBodyReader(uri, this.jsonBodyReader, false);
91 final InputStream inputStream = TestJsonBodyReader.class
92 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
93 final NormalizedNodeContext returnValue = this.jsonBodyReader
94 .readFrom(null, null, null, this.mediaType, null, inputStream);
95 checkNormalizedNodeContext(returnValue);
96 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
100 public void moduleSubContainerDataPostTest() throws Exception {
101 final DataSchemaNode dataSchemaNode =
102 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
103 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
104 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
105 final String uri = "instance-identifier-module:cont";
106 mockBodyReader(uri, this.jsonBodyReader, true);
107 final InputStream inputStream = TestJsonBodyReader.class
108 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
109 final NormalizedNodeContext returnValue = this.jsonBodyReader
110 .readFrom(null, null, null, this.mediaType, null, inputStream);
111 checkNormalizedNodeContext(returnValue);
112 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
116 public void moduleSubContainerAugmentDataPostTest() throws Exception {
117 final DataSchemaNode dataSchemaNode =
118 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
119 final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
120 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
121 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
122 Sets.newHashSet(contAugmentQName));
123 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
124 .node(augII).node(contAugmentQName);
125 final String uri = "instance-identifier-module:cont";
126 mockBodyReader(uri, this.jsonBodyReader, true);
127 final InputStream inputStream = TestXmlBodyReader.class
128 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
129 final NormalizedNodeContext returnValue = this.jsonBodyReader
130 .readFrom(null, null, null, this.mediaType, null, inputStream);
131 checkNormalizedNodeContext(returnValue);
132 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
135 //FIXME: Uncomment this when JsonParserStream works correctly with case augmentation with choice
137 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
138 final DataSchemaNode dataSchemaNode =
139 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
140 final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
141 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
142 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
143 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
144 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
145 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
146 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
147 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
148 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
149 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
150 .node(containerQName);
151 final String uri = "instance-identifier-module:cont";
152 mockBodyReader(uri, this.jsonBodyReader, true);
153 final InputStream inputStream = TestXmlBodyReader.class
154 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
155 final NormalizedNodeContext returnValue = this.jsonBodyReader
156 .readFrom(null, null, null, this.mediaType, null, inputStream);
157 checkNormalizedNodeContext(returnValue);
158 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
162 public void rpcModuleInputTest() throws Exception {
163 final String uri = "invoke-rpc-module:rpc-test";
164 mockBodyReader(uri, this.jsonBodyReader, true);
165 final InputStream inputStream = TestJsonBodyReader.class
166 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
167 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
168 null, null, this.mediaType, null, inputStream);
169 checkNormalizedNodeContext(returnValue);
170 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
171 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
172 .create(inputNode.getNodeType(), "cont"));
173 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
174 .getChild(yangCont.getLastPathArgument());
175 assertTrue(contDataNode.isPresent());
176 assertTrue(contDataNode.get() instanceof ContainerNode);
177 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
178 .create(inputNode.getNodeType(), "lf"));
179 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
180 .get()).getChild(yangleaf.getLastPathArgument());
181 assertTrue(leafDataNode.isPresent());
182 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
186 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
187 final NormalizedNodeContext nnContext) {
188 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
191 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
192 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
193 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
194 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
195 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));