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.Matchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
17 import java.io.InputStream;
19 import javax.ws.rs.core.MediaType;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
24 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
25 import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader;
26 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
27 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
28 import org.opendaylight.yangtools.yang.common.QName;
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;
38 import com.google.common.base.Optional;
41 * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
45 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
47 * Created: Mar 11, 2015
49 public class TestJsonBodyReaderMountPoint extends AbstractBodyReaderTest {
51 private final JsonNormalizedNodeBodyReader jsonBodyReader;
52 private static SchemaContext schemaContext;
54 public TestJsonBodyReaderMountPoint() throws NoSuchFieldException,
57 jsonBodyReader = new JsonNormalizedNodeBodyReader();
61 protected MediaType getMediaType() {
62 return new MediaType(MediaType.APPLICATION_XML, null);
66 public static void initialization() throws NoSuchFieldException,
68 schemaContext = schemaContextLoader("/instanceidentifier/yang",
70 schemaContext = schemaContextLoader("/modules", schemaContext);
71 schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
72 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
73 when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
74 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
75 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
76 .thenReturn(Optional.of(mountInstance));
78 ControllerContext.getInstance().setMountService(mockMountService);
79 controllerContext.setSchemas(schemaContext);
83 public void moduleDataTest() throws Exception {
84 final DataSchemaNode dataSchemaNode = schemaContext
85 .getDataChildByName("cont");
86 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
87 mockBodyReader(uri, jsonBodyReader, false);
88 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
89 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
90 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
91 null, null, mediaType, null, inputStream);
92 checkMountPointNormalizedNodeContext(returnValue);
93 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
97 public void moduleSubContainerDataPutTest() throws Exception {
98 final DataSchemaNode dataSchemaNode = schemaContext
99 .getDataChildByName("cont");
100 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
101 mockBodyReader(uri, jsonBodyReader, false);
102 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
103 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
104 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
105 null, null, mediaType, null, inputStream);
106 checkMountPointNormalizedNodeContext(returnValue);
107 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
112 public void moduleSubContainerDataPostTest() throws Exception {
113 final DataSchemaNode dataSchemaNode = schemaContext
114 .getDataChildByName("cont");
115 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
116 mockBodyReader(uri, jsonBodyReader, true);
117 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
118 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
119 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
120 null, null, mediaType, null, inputStream);
121 checkMountPointNormalizedNodeContext(returnValue);
122 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
126 public void rpcModuleInputTest() throws Exception {
127 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
128 mockBodyReader(uri, jsonBodyReader, true);
129 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
130 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
131 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
132 null, null, mediaType, null, inputStream);
133 checkNormalizedNodeContext(returnValue);
134 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
135 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
136 .create(inputNode.getNodeType(), "cont"));
137 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
138 .getChild(yangCont.getLastPathArgument());
139 assertTrue(contDataNode.isPresent());
140 assertTrue(contDataNode.get() instanceof ContainerNode);
141 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
142 .create(inputNode.getNodeType(), "lf"));
143 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
144 .get()).getChild(yangleaf.getLastPathArgument());
145 assertTrue(leafDataNode.isPresent());
146 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
150 private void checkExpectValueNormalizeNodeContext(
151 final DataSchemaNode dataSchemaNode,
152 final NormalizedNodeContext nnContext) {
153 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
156 protected void checkExpectValueNormalizeNodeContext(
157 final DataSchemaNode dataSchemaNode,
158 final NormalizedNodeContext nnContext, final String localQname) {
159 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
160 .of(dataSchemaNode.getQName());
161 final DOMMountPoint mountPoint = nnContext
162 .getInstanceIdentifierContext().getMountPoint();
163 final DataSchemaNode mountDataSchemaNode = mountPoint
164 .getSchemaContext().getDataChildByName(
165 dataSchemaNode.getQName());
166 assertNotNull(mountDataSchemaNode);
167 if (localQname != null && dataSchemaNode instanceof DataNodeContainer) {
168 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
169 .getDataChildByName(localQname);
170 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
171 .node(child.getQName()).build();
172 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
175 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
177 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),