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.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.mock;
16 import java.io.InputStream;
18 import java.util.Collection;
19 import javax.ws.rs.core.MediaType;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
23 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
24 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
25 import org.opendaylight.restconf.common.context.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.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
34 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
39 public class TestJsonBodyReaderMountPoint 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 TestJsonBodyReaderMountPoint() throws NoSuchFieldException, SecurityException {
48 super(schemaContext, mock(DOMMountPoint.class));
49 this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext);
53 protected MediaType getMediaType() {
54 return new MediaType(MediaType.APPLICATION_XML, null);
58 public static void initialization() throws Exception {
59 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
60 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
61 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
65 public void moduleDataTest() throws Exception {
66 final DataSchemaNode dataSchemaNode = schemaContext
67 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
68 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
69 mockBodyReader(uri, this.jsonBodyReader, false);
70 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
71 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
72 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
73 null, null, this.mediaType, null, inputStream);
74 checkMountPointNormalizedNodeContext(returnValue);
75 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
79 public void moduleSubContainerDataPutTest() throws Exception {
80 final DataSchemaNode dataSchemaNode = schemaContext
81 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
82 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
83 mockBodyReader(uri, this.jsonBodyReader, false);
84 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
85 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
86 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
87 null, null, this.mediaType, null, inputStream);
88 checkMountPointNormalizedNodeContext(returnValue);
89 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
90 QName.create(dataSchemaNode.getQName(), "cont1"));
94 public void moduleSubContainerDataPostTest() throws Exception {
95 final DataSchemaNode dataSchemaNode = schemaContext
96 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
97 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
98 mockBodyReader(uri, this.jsonBodyReader, true);
99 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
100 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
101 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
102 null, null, this.mediaType, null, inputStream);
103 checkMountPointNormalizedNodeContext(returnValue);
104 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
108 public void rpcModuleInputTest() throws Exception {
109 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
110 mockBodyReader(uri, this.jsonBodyReader, true);
111 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
112 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
113 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
114 null, null, this.mediaType, null, inputStream);
115 checkNormalizedNodeContext(returnValue);
116 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
117 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
118 .create(inputNode.getNodeType(), "cont"));
119 final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
120 .getChild(yangCont.getLastPathArgument());
121 assertTrue(contDataNode.isPresent());
122 assertTrue(contDataNode.get() instanceof ContainerNode);
123 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
124 .create(inputNode.getNodeType(), "lf"));
125 final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode =
126 ((ContainerNode) contDataNode.get()).getChild(yangleaf.getLastPathArgument());
127 assertTrue(leafDataNode.isPresent());
128 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
132 private void checkExpectValueNormalizeNodeContext(
133 final DataSchemaNode dataSchemaNode,
134 final NormalizedNodeContext nnContext) {
135 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
138 protected void checkExpectValueNormalizeNodeContext(
139 final DataSchemaNode dataSchemaNode,
140 final NormalizedNodeContext nnContext, final QName qualifiedName) {
141 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
142 .of(dataSchemaNode.getQName());
143 final DOMMountPoint mountPoint = nnContext
144 .getInstanceIdentifierContext().getMountPoint();
145 final DataSchemaNode mountDataSchemaNode = mountPoint
146 .getSchemaContext().getDataChildByName(
147 dataSchemaNode.getQName());
148 assertNotNull(mountDataSchemaNode);
149 if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
150 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
151 .getDataChildByName(qualifiedName);
152 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
153 .node(child.getQName()).build();
154 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
157 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
159 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),