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 com.google.common.base.Optional;
14 import java.io.InputStream;
15 import javax.ws.rs.core.MediaType;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader;
19 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
26 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 * org.opendaylight.controller.sal.rest.impl.test.providers
36 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
38 * Created: Mar 11, 2015
40 public class TestJsonBodyReader extends AbstractBodyReaderTest {
42 private final JsonNormalizedNodeBodyReader jsonBodyReader;
43 private static SchemaContext schemaContext;
45 public TestJsonBodyReader () throws NoSuchFieldException, SecurityException {
47 jsonBodyReader = new JsonNormalizedNodeBodyReader();
51 MediaType getMediaType() {
52 return new MediaType(MediaType.APPLICATION_XML, null);
56 public static void initialization() throws NoSuchFieldException, SecurityException {
57 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
58 schemaContext = schemaContextLoader("/modules", schemaContext);
59 schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
60 controllerContext.setSchemas(schemaContext);
64 public void moduleDataTest() throws Exception {
65 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
66 final String uri = "instance-identifier-module:cont";
67 mockBodyReader(uri, jsonBodyReader, false);
68 final InputStream inputStream = TestJsonBodyReader.class
69 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
70 final NormalizedNodeContext returnValue = jsonBodyReader
71 .readFrom(null, null, null, mediaType, null, inputStream);
72 checkNormalizedNodeContext(returnValue);
73 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
77 public void moduleSubContainerDataPutTest() throws Exception {
78 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
79 final String uri = "instance-identifier-module:cont/cont1";
80 mockBodyReader(uri, jsonBodyReader, false);
81 final InputStream inputStream = TestJsonBodyReader.class
82 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
83 final NormalizedNodeContext returnValue = jsonBodyReader
84 .readFrom(null, null, null, mediaType, null, inputStream);
85 checkNormalizedNodeContext(returnValue);
86 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, "cont1");
90 public void moduleSubContainerDataPostTest() throws Exception {
91 final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
92 final String uri = "instance-identifier-module:cont";
93 mockBodyReader(uri, jsonBodyReader, true);
94 final InputStream inputStream = TestJsonBodyReader.class
95 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
96 final NormalizedNodeContext returnValue = jsonBodyReader
97 .readFrom(null, null, null, mediaType, null, inputStream);
98 checkNormalizedNodeContext(returnValue);
99 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
103 public void rpcModuleInputTest() throws Exception {
104 final String uri = "invoke-rpc-module:rpc-test";
105 mockBodyReader(uri, jsonBodyReader, true);
106 final InputStream inputStream = TestJsonBodyReader.class
107 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
108 final NormalizedNodeContext returnValue = jsonBodyReader
109 .readFrom(null, null, null, mediaType, null, inputStream);
110 checkNormalizedNodeContext(returnValue);
111 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
112 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(inputNode.getNodeType(), "cont"));
113 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode.getChild(yangCont.getLastPathArgument());
114 assertTrue(contDataNode.isPresent());
115 assertTrue(contDataNode.get() instanceof ContainerNode);
116 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName.create(inputNode.getNodeType(), "lf"));
117 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode)contDataNode.get()).getChild(yangleaf.getLastPathArgument());
118 assertTrue(leafDataNode.isPresent());
119 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
122 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
123 final NormalizedNodeContext nnContext) {
124 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
127 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
128 final NormalizedNodeContext nnContext, final String localQname) {
129 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier.of(dataSchemaNode.getQName());
131 if (localQname != null && dataSchemaNode instanceof DataNodeContainer) {
132 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode).getDataChildByName(localQname);
133 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent).node(child.getQName()).build();
134 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode().equals(child));
136 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode().equals(dataSchemaNode));
138 assertTrue(nnContext.getInstanceIdentifierContext().getInstanceIdentifier().equals(dataNodeIdent));
139 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));