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;
14 import com.google.common.collect.Sets;
16 import java.io.FileNotFoundException;
17 import java.io.InputStream;
19 import java.util.Collection;
20 import javax.ws.rs.core.MediaType;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
24 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
25 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyReader;
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.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
36 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
37 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
39 public class JsonBodyReaderTest extends AbstractBodyReaderTest {
41 private final JsonNormalizedNodeBodyReader jsonBodyReader;
42 private static SchemaContext schemaContext;
44 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
45 URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
47 public JsonBodyReaderTest() throws Exception {
49 this.jsonBodyReader = new JsonNormalizedNodeBodyReader(schemaContextHandler, mountPointServiceHandler);
53 protected MediaType getMediaType() {
54 return new MediaType(MediaType.APPLICATION_XML, null);
58 public static void initialization()
59 throws NoSuchFieldException, SecurityException, FileNotFoundException, SourceException, ReactorException {
60 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
61 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
62 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
66 public void moduleDataTest() throws Exception {
67 final DataSchemaNode dataSchemaNode = schemaContext
68 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
69 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
70 final String uri = "instance-identifier-module:cont";
71 mockBodyReader(uri, this.jsonBodyReader, false);
72 final InputStream inputStream = JsonBodyReaderTest.class
73 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
74 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
76 checkNormalizedNodeContext(returnValue);
77 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
81 public void moduleSubContainerDataPutTest() throws Exception {
82 final DataSchemaNode dataSchemaNode = schemaContext
83 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
84 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
85 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
86 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
87 final String uri = "instance-identifier-module:cont/cont1";
88 mockBodyReader(uri, this.jsonBodyReader, false);
89 final InputStream inputStream = JsonBodyReaderTest.class
90 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
91 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
93 checkNormalizedNodeContext(returnValue);
94 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
98 public void moduleSubContainerDataPostTest() throws Exception {
99 final DataSchemaNode dataSchemaNode = schemaContext
100 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
101 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
102 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
103 final String uri = "instance-identifier-module:cont";
104 mockBodyReader(uri, this.jsonBodyReader, true);
105 final InputStream inputStream = JsonBodyReaderTest.class
106 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
107 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
109 checkNormalizedNodeContext(returnValue);
110 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
114 public void moduleSubContainerAugmentDataPostTest() throws Exception {
115 final DataSchemaNode dataSchemaNode = schemaContext
116 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
117 final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
118 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
119 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
120 Sets.newHashSet(contAugmentQName));
121 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
122 .node(contAugmentQName);
123 final String uri = "instance-identifier-module:cont";
124 mockBodyReader(uri, this.jsonBodyReader, true);
125 final InputStream inputStream =
126 XmlBodyReaderTest.class
127 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
128 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
130 checkNormalizedNodeContext(returnValue);
131 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
135 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
136 final DataSchemaNode dataSchemaNode = schemaContext
137 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
138 final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
139 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
140 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
141 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
142 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
143 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
144 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
145 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
146 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
147 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
148 final String uri = "instance-identifier-module:cont";
149 mockBodyReader(uri, this.jsonBodyReader, true);
150 final InputStream inputStream = XmlBodyReaderTest.class
151 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
152 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
154 checkNormalizedNodeContext(returnValue);
155 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
158 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
159 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
160 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
161 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
162 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));