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.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
16 import com.google.common.collect.Sets;
18 import java.io.FileNotFoundException;
19 import java.io.InputStream;
21 import java.text.ParseException;
22 import java.util.Collection;
23 import javax.ws.rs.core.MediaType;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
27 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
28 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
29 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyReader;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.QNameModule;
32 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
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.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.parser.spi.meta.ReactorException;
40 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
43 public class JsonBodyReaderTest 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) {
59 public JsonBodyReaderTest() throws Exception {
60 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
64 protected MediaType getMediaType() {
65 return new MediaType(MediaType.APPLICATION_XML, null);
69 public static void initialization()
70 throws NoSuchFieldException, SecurityException, FileNotFoundException, SourceException, ReactorException {
71 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
72 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
73 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
74 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
78 public void moduleDataTest() throws Exception {
79 final DataSchemaNode dataSchemaNode = schemaContext
80 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
81 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
82 final String uri = "instance-identifier-module:cont";
83 mockBodyReader(uri, this.jsonBodyReader, false);
84 final InputStream inputStream = JsonBodyReaderTest.class
85 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
86 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
88 checkNormalizedNodeContext(returnValue);
89 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
93 public void moduleSubContainerDataPutTest() throws Exception {
94 final DataSchemaNode dataSchemaNode = schemaContext
95 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
96 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
97 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
98 final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
99 final String uri = "instance-identifier-module:cont/cont1";
100 mockBodyReader(uri, this.jsonBodyReader, false);
101 final InputStream inputStream = JsonBodyReaderTest.class
102 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
103 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
105 checkNormalizedNodeContext(returnValue);
106 checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
110 public void moduleSubContainerDataPostTest() throws Exception {
111 final DataSchemaNode dataSchemaNode = schemaContext
112 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
113 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
114 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
115 final String uri = "instance-identifier-module:cont";
116 mockBodyReader(uri, this.jsonBodyReader, true);
117 final InputStream inputStream = JsonBodyReaderTest.class
118 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
119 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
121 checkNormalizedNodeContext(returnValue);
122 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
126 public void moduleSubContainerAugmentDataPostTest() throws Exception {
127 final DataSchemaNode dataSchemaNode = schemaContext
128 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
129 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
130 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
131 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
132 Sets.newHashSet(contAugmentQName));
133 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
134 .node(contAugmentQName);
135 final String uri = "instance-identifier-module:cont";
136 mockBodyReader(uri, this.jsonBodyReader, true);
137 final InputStream inputStream =
138 XmlBodyReaderTest.class
139 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
140 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
142 checkNormalizedNodeContext(returnValue);
143 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
147 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
148 final DataSchemaNode dataSchemaNode = schemaContext
149 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
150 final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
151 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
152 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
153 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
154 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
155 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
156 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
157 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
158 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
159 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
160 final String uri = "instance-identifier-module:cont";
161 mockBodyReader(uri, this.jsonBodyReader, true);
162 final InputStream inputStream = XmlBodyReaderTest.class
163 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
164 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
166 checkNormalizedNodeContext(returnValue);
167 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
170 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
171 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
172 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
173 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
174 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));