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.util.Collection;
22 import javax.ws.rs.core.MediaType;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
26 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
27 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
28 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
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.Revision;
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 = QNameModule.create(
49 URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
51 public JsonBodyReaderTest() throws Exception {
52 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
56 protected MediaType getMediaType() {
57 return new MediaType(MediaType.APPLICATION_XML, null);
61 public static void initialization()
62 throws NoSuchFieldException, SecurityException, FileNotFoundException, SourceException, ReactorException {
63 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
64 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
65 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
66 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
67 SchemaContextHandler.setActualSchemaContext(schemaContext);
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 moduleSubContainerAugmentDataPostTest() throws Exception {
120 final DataSchemaNode dataSchemaNode = schemaContext
121 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
122 final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
123 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
124 final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
125 Sets.newHashSet(contAugmentQName));
126 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
127 .node(contAugmentQName);
128 final String uri = "instance-identifier-module:cont";
129 mockBodyReader(uri, this.jsonBodyReader, true);
130 final InputStream inputStream =
131 XmlBodyReaderTest.class
132 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
133 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
135 checkNormalizedNodeContext(returnValue);
136 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
140 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
141 final DataSchemaNode dataSchemaNode = schemaContext
142 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
143 final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
144 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
145 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
146 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
147 final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
148 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
149 final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
150 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
151 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
152 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
153 final String uri = "instance-identifier-module:cont";
154 mockBodyReader(uri, this.jsonBodyReader, true);
155 final InputStream inputStream = XmlBodyReaderTest.class
156 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
157 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
159 checkNormalizedNodeContext(returnValue);
160 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
163 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
164 final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
165 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
166 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
167 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));