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;
14 import java.io.InputStream;
16 import javax.ws.rs.core.MediaType;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader;
21 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import com.google.common.base.Optional;
35 * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
39 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
41 * Created: Mar 11, 2015
43 public class TestJsonBodyReader extends AbstractBodyReaderTest {
45 private final JsonNormalizedNodeBodyReader jsonBodyReader;
46 private static SchemaContext schemaContext;
48 public TestJsonBodyReader() throws NoSuchFieldException, SecurityException {
50 jsonBodyReader = new JsonNormalizedNodeBodyReader();
54 protected MediaType getMediaType() {
55 return new MediaType(MediaType.APPLICATION_XML, null);
59 public static void initialization() throws NoSuchFieldException,
61 schemaContext = schemaContextLoader("/instanceidentifier/yang",
63 schemaContext = schemaContextLoader("/modules", schemaContext);
64 schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
65 controllerContext.setSchemas(schemaContext);
69 public void moduleDataTest() throws Exception {
70 final DataSchemaNode dataSchemaNode = schemaContext
71 .getDataChildByName("cont");
72 final String uri = "instance-identifier-module:cont";
73 mockBodyReader(uri, jsonBodyReader, false);
74 final InputStream inputStream = TestJsonBodyReader.class
75 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
76 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
77 null, null, mediaType, null, inputStream);
78 checkNormalizedNodeContext(returnValue);
79 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
83 public void moduleSubContainerDataPutTest() throws Exception {
84 final DataSchemaNode dataSchemaNode = schemaContext
85 .getDataChildByName("cont");
86 final String uri = "instance-identifier-module:cont/cont1";
87 mockBodyReader(uri, jsonBodyReader, false);
88 final InputStream inputStream = TestJsonBodyReader.class
89 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
90 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
91 null, null, mediaType, null, inputStream);
92 checkNormalizedNodeContext(returnValue);
93 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
98 public void moduleSubContainerDataPostTest() throws Exception {
99 final DataSchemaNode dataSchemaNode = schemaContext
100 .getDataChildByName("cont");
101 final String uri = "instance-identifier-module:cont";
102 mockBodyReader(uri, jsonBodyReader, true);
103 final InputStream inputStream = TestJsonBodyReader.class
104 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
105 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
106 null, null, mediaType, null, inputStream);
107 checkNormalizedNodeContext(returnValue);
108 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
112 public void rpcModuleInputTest() throws Exception {
113 final String uri = "invoke-rpc-module:rpc-test";
114 mockBodyReader(uri, jsonBodyReader, true);
115 final InputStream inputStream = TestJsonBodyReader.class
116 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
117 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
118 null, null, mediaType, null, inputStream);
119 checkNormalizedNodeContext(returnValue);
120 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
121 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
122 .create(inputNode.getNodeType(), "cont"));
123 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
124 .getChild(yangCont.getLastPathArgument());
125 assertTrue(contDataNode.isPresent());
126 assertTrue(contDataNode.get() instanceof ContainerNode);
127 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
128 .create(inputNode.getNodeType(), "lf"));
129 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
130 .get()).getChild(yangleaf.getLastPathArgument());
131 assertTrue(leafDataNode.isPresent());
132 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
136 private void checkExpectValueNormalizeNodeContext(
137 final DataSchemaNode dataSchemaNode,
138 final NormalizedNodeContext nnContext) {
139 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
142 private void checkExpectValueNormalizeNodeContext(
143 final DataSchemaNode dataSchemaNode,
144 final NormalizedNodeContext nnContext, final String localQname) {
145 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
146 .of(dataSchemaNode.getQName());
148 if (localQname != null && dataSchemaNode instanceof DataNodeContainer) {
149 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
150 .getDataChildByName(localQname);
151 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
152 .node(child.getQName()).build();
153 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
156 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
157 .equals(dataSchemaNode));
159 assertTrue(nnContext.getInstanceIdentifierContext()
160 .getInstanceIdentifier().equals(dataNodeIdent));
161 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),