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;
17 import java.util.Collection;
18 import java.util.Optional;
20 import javax.ws.rs.core.MediaType;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
24 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
25 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.common.Revision;
29 import org.opendaylight.yangtools.yang.common.XMLNamespace;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
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.EffectiveModelContext;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
41 public class TestJsonBodyReader extends AbstractBodyReaderTest {
43 private final JsonNormalizedNodeBodyReader jsonBodyReader;
44 private static EffectiveModelContext schemaContext;
46 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
47 XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
49 public TestJsonBodyReader() {
50 super(schemaContext, null);
51 jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext);
55 protected MediaType getMediaType() {
56 return new MediaType(MediaType.APPLICATION_XML, null);
60 public static void initialization()
62 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
63 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
64 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
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, jsonBodyReader, false);
74 final InputStream inputStream = TestJsonBodyReader.class
75 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
76 final NormalizedNodeContext returnValue = jsonBodyReader
77 .readFrom(null, null, null, 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, jsonBodyReader, false);
91 final InputStream inputStream = TestJsonBodyReader.class
92 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
93 final NormalizedNodeContext returnValue = jsonBodyReader
94 .readFrom(null, null, null, 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, jsonBodyReader, true);
107 final InputStream inputStream = TestJsonBodyReader.class
108 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
109 final NormalizedNodeContext returnValue = jsonBodyReader
110 .readFrom(null, null, null, 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(XMLNamespace.of("augment:module")).iterator().next();
120 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
121 final AugmentationIdentifier augII = new AugmentationIdentifier(Set.of(contAugmentQName));
122 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
123 .node(augII).node(contAugmentQName);
124 final String uri = "instance-identifier-module:cont";
125 mockBodyReader(uri, jsonBodyReader, true);
126 final InputStream inputStream = TestXmlBodyReader.class
127 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
128 final NormalizedNodeContext returnValue = jsonBodyReader
129 .readFrom(null, null, null, mediaType, null, inputStream);
130 checkNormalizedNodeContext(returnValue);
131 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
134 //FIXME: Uncomment this when JsonParserStream works correctly with case augmentation with choice
136 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
137 final DataSchemaNode dataSchemaNode =
138 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
139 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
140 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
141 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
142 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
143 final AugmentationIdentifier augChoice1II = new AugmentationIdentifier(Set.of(augmentChoice1QName));
144 final AugmentationIdentifier augChoice2II = new AugmentationIdentifier(Set.of(augmentChoice2QName));
145 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
146 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
147 .node(containerQName);
148 final String uri = "instance-identifier-module:cont";
149 mockBodyReader(uri, jsonBodyReader, true);
150 final InputStream inputStream = TestXmlBodyReader.class
151 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
152 final NormalizedNodeContext returnValue = jsonBodyReader
153 .readFrom(null, null, null, mediaType, null, inputStream);
154 checkNormalizedNodeContext(returnValue);
155 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
159 public void rpcModuleInputTest() throws Exception {
160 final String uri = "invoke-rpc-module:rpc-test";
161 mockBodyReader(uri, jsonBodyReader, true);
162 final InputStream inputStream = TestJsonBodyReader.class.getResourceAsStream("/invoke-rpc/json/rpc-input.json");
163 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null, null, null, mediaType, null,
165 checkNormalizedNodeContextRpc(returnValue);
166 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
167 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(
168 QName.create(inputNode.getIdentifier().getNodeType(), "cont"));
169 final Optional<DataContainerChild> contDataNode = inputNode.findChildByArg(yangCont.getLastPathArgument());
170 assertTrue(contDataNode.isPresent());
171 assertTrue(contDataNode.get() instanceof ContainerNode);
172 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(
173 QName.create(inputNode.getIdentifier().getNodeType(), "lf"));
174 final Optional<DataContainerChild> leafDataNode = ((ContainerNode) contDataNode.get())
175 .findChildByArg(yangleaf.getLastPathArgument());
176 assertTrue(leafDataNode.isPresent());
177 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().body().toString()));
180 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
181 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
182 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
183 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
184 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));