2 * Copyright (c) 2016 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.restconf.nb.rfc8040.jersey.providers.test;
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;
16 import java.io.ByteArrayInputStream;
18 import java.io.FileNotFoundException;
19 import java.io.InputStream;
20 import java.nio.charset.StandardCharsets;
21 import java.util.Collection;
22 import java.util.Optional;
23 import javax.ws.rs.core.MediaType;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
27 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
28 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyReader;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.QNameModule;
31 import org.opendaylight.yangtools.yang.common.Revision;
32 import org.opendaylight.yangtools.yang.common.XMLNamespace;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
35 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
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.EffectiveModelContext;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
41 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
42 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44 public class JsonBodyReaderTest extends AbstractBodyReaderTest {
45 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
46 XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
48 private static EffectiveModelContext schemaContext;
50 private final JsonNormalizedNodeBodyReader jsonBodyReader;
52 public JsonBodyReaderTest() throws Exception {
54 this.jsonBodyReader = new JsonNormalizedNodeBodyReader(schemaContextHandler, mountPointService);
58 protected MediaType getMediaType() {
59 return new MediaType(MediaType.APPLICATION_XML, null);
63 public static void initialization()
64 throws NoSuchFieldException, SecurityException, FileNotFoundException, SourceException, ReactorException {
65 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
66 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
67 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
71 public void moduleDataTest() throws Exception {
72 final DataSchemaNode dataSchemaNode = schemaContext
73 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
74 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
75 final String uri = "instance-identifier-module:cont";
76 mockBodyReader(uri, this.jsonBodyReader, false);
77 final InputStream inputStream = JsonBodyReaderTest.class
78 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
79 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
81 checkNormalizedNodeContext(returnValue);
82 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
86 public void moduleSubContainerDataPutTest() throws Exception {
87 final DataSchemaNode dataSchemaNode = schemaContext
88 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
89 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
90 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
91 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
92 final String uri = "instance-identifier-module:cont/cont1";
93 mockBodyReader(uri, this.jsonBodyReader, false);
94 final InputStream inputStream = JsonBodyReaderTest.class
95 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
96 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
98 checkNormalizedNodeContext(returnValue);
99 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
103 public void moduleSubContainerDataPostTest() throws Exception {
104 final DataSchemaNode dataSchemaNode = schemaContext
105 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
106 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
107 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
108 final String uri = "instance-identifier-module:cont";
109 mockBodyReader(uri, this.jsonBodyReader, true);
110 final InputStream inputStream = JsonBodyReaderTest.class
111 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
112 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
114 checkNormalizedNodeContext(returnValue);
115 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
119 public void moduleSubContainerDataPostActionTest() throws Exception {
120 final Optional<DataSchemaNode> dataSchemaNode = schemaContext
121 .findDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
122 final QName cont1QName = QName.create(dataSchemaNode.get().getQName(), "cont1");
123 final QName actionQName = QName.create(dataSchemaNode.get().getQName(), "reset");
124 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.get().getQName())
125 .node(cont1QName).node(actionQName);
126 final String uri = "instance-identifier-module:cont/cont1/reset";
127 mockBodyReader(uri, this.jsonBodyReader, true);
128 final InputStream inputStream = JsonBodyReaderTest.class
129 .getResourceAsStream("/instanceidentifier/json/json_cont_action.json");
130 final NormalizedNodeContext returnValue = this.jsonBodyReader
131 .readFrom(null, null, null, this.mediaType, null, inputStream);
132 checkNormalizedNodeContext(returnValue);
133 assertTrue(returnValue.getInstanceIdentifierContext().getSchemaNode() instanceof ActionDefinition);
137 public void moduleSubContainerAugmentDataPostTest() throws Exception {
138 final DataSchemaNode dataSchemaNode = schemaContext
139 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
140 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
141 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
142 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
143 Sets.newHashSet(contAugmentQName));
144 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
145 .node(contAugmentQName);
146 final String uri = "instance-identifier-module:cont";
147 mockBodyReader(uri, this.jsonBodyReader, true);
148 final InputStream inputStream =
149 XmlBodyReaderTest.class
150 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
151 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
153 checkNormalizedNodeContext(returnValue);
154 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
158 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
159 final DataSchemaNode dataSchemaNode = schemaContext
160 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
161 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
162 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
163 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
164 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
165 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
166 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
167 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
168 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
169 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
170 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
171 final String uri = "instance-identifier-module:cont";
172 mockBodyReader(uri, this.jsonBodyReader, true);
173 final InputStream inputStream = XmlBodyReaderTest.class
174 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
175 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
177 checkNormalizedNodeContext(returnValue);
178 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
182 public void testRangeViolation() throws Exception {
183 mockBodyReader("netconf786:foo", this.jsonBodyReader, false);
185 final InputStream inputStream = new ByteArrayInputStream(("{\n"
186 + " \"netconf786:foo\": {\n"
189 + "}").getBytes(StandardCharsets.UTF_8));
191 assertRangeViolation(() -> jsonBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream));
194 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
195 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
196 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
197 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
198 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));