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;
18 import java.io.InputStream;
19 import java.util.Collection;
21 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.DOMMountPoint;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
27 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
28 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
29 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
30 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
37 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
38 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import com.google.common.base.Optional;
44 * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
48 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
50 * Created: Mar 11, 2015
52 public class TestJsonBodyReaderMountPoint extends AbstractBodyReaderTest {
54 private final JsonNormalizedNodeBodyReader jsonBodyReader;
55 private static SchemaContext schemaContext;
57 public TestJsonBodyReaderMountPoint() throws NoSuchFieldException,
60 jsonBodyReader = new JsonNormalizedNodeBodyReader();
64 protected MediaType getMediaType() {
65 return new MediaType(MediaType.APPLICATION_XML, null);
69 public static void initialization() throws Exception {
70 Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
71 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
72 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
73 schemaContext = TestRestconfUtils.parseYangSources(testFiles);
75 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
76 when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
77 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
78 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
79 .thenReturn(Optional.of(mountInstance));
81 ControllerContext.getInstance().setMountService(mockMountService);
82 controllerContext.setSchemas(schemaContext);
86 public void moduleDataTest() throws Exception {
87 final DataSchemaNode dataSchemaNode = schemaContext
88 .getDataChildByName("cont");
89 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
90 mockBodyReader(uri, jsonBodyReader, false);
91 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
92 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
93 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
94 null, null, mediaType, null, inputStream);
95 checkMountPointNormalizedNodeContext(returnValue);
96 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
100 public void moduleSubContainerDataPutTest() throws Exception {
101 final DataSchemaNode dataSchemaNode = schemaContext
102 .getDataChildByName("cont");
103 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
104 mockBodyReader(uri, jsonBodyReader, false);
105 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
106 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
107 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
108 null, null, mediaType, null, inputStream);
109 checkMountPointNormalizedNodeContext(returnValue);
110 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
115 public void moduleSubContainerDataPostTest() throws Exception {
116 final DataSchemaNode dataSchemaNode = schemaContext
117 .getDataChildByName("cont");
118 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
119 mockBodyReader(uri, jsonBodyReader, true);
120 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
121 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
122 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
123 null, null, mediaType, null, inputStream);
124 checkMountPointNormalizedNodeContext(returnValue);
125 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
129 public void rpcModuleInputTest() throws Exception {
130 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
131 mockBodyReader(uri, jsonBodyReader, true);
132 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
133 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
134 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
135 null, null, mediaType, null, inputStream);
136 checkNormalizedNodeContext(returnValue);
137 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
138 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
139 .create(inputNode.getNodeType(), "cont"));
140 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
141 .getChild(yangCont.getLastPathArgument());
142 assertTrue(contDataNode.isPresent());
143 assertTrue(contDataNode.get() instanceof ContainerNode);
144 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
145 .create(inputNode.getNodeType(), "lf"));
146 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
147 .get()).getChild(yangleaf.getLastPathArgument());
148 assertTrue(leafDataNode.isPresent());
149 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
153 private void checkExpectValueNormalizeNodeContext(
154 final DataSchemaNode dataSchemaNode,
155 final NormalizedNodeContext nnContext) {
156 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
159 protected void checkExpectValueNormalizeNodeContext(
160 final DataSchemaNode dataSchemaNode,
161 final NormalizedNodeContext nnContext, final String localQname) {
162 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
163 .of(dataSchemaNode.getQName());
164 final DOMMountPoint mountPoint = nnContext
165 .getInstanceIdentifierContext().getMountPoint();
166 final DataSchemaNode mountDataSchemaNode = mountPoint
167 .getSchemaContext().getDataChildByName(
168 dataSchemaNode.getQName());
169 assertNotNull(mountDataSchemaNode);
170 if (localQname != null && dataSchemaNode instanceof DataNodeContainer) {
171 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
172 .getDataChildByName(localQname);
173 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
174 .node(child.getQName()).build();
175 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
178 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
180 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),