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 com.google.common.base.Optional;
19 import java.io.InputStream;
21 import java.util.Collection;
22 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.restconf.common.context.NormalizedNodeContext;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.common.QNameModule;
33 import org.opendaylight.yangtools.yang.common.Revision;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
39 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
40 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44 public class TestJsonBodyReaderMountPoint extends AbstractBodyReaderTest {
46 private final JsonNormalizedNodeBodyReader jsonBodyReader;
47 private static SchemaContext schemaContext;
49 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
50 URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
52 public TestJsonBodyReaderMountPoint() throws NoSuchFieldException,
54 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
58 protected MediaType getMediaType() {
59 return new MediaType(MediaType.APPLICATION_XML, null);
63 public static void initialization() throws Exception {
64 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
65 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
66 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
68 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
69 when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
70 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
71 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
72 .thenReturn(Optional.of(mountInstance));
74 ControllerContext.getInstance().setMountService(mockMountService);
75 CONTROLLER_CONTEXT.setSchemas(schemaContext);
79 public void moduleDataTest() 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";
83 mockBodyReader(uri, this.jsonBodyReader, false);
84 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
85 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
86 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
87 null, null, this.mediaType, null, inputStream);
88 checkMountPointNormalizedNodeContext(returnValue);
89 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
93 public void moduleSubContainerDataPutTest() throws Exception {
94 final DataSchemaNode dataSchemaNode = schemaContext
95 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
96 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
97 mockBodyReader(uri, this.jsonBodyReader, false);
98 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
99 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
100 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
101 null, null, this.mediaType, null, inputStream);
102 checkMountPointNormalizedNodeContext(returnValue);
103 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
104 QName.create(dataSchemaNode.getQName(), "cont1"));
108 public void moduleSubContainerDataPostTest() throws Exception {
109 final DataSchemaNode dataSchemaNode = schemaContext
110 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
111 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
112 mockBodyReader(uri, this.jsonBodyReader, true);
113 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
114 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
115 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
116 null, null, this.mediaType, null, inputStream);
117 checkMountPointNormalizedNodeContext(returnValue);
118 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
122 public void rpcModuleInputTest() throws Exception {
123 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
124 mockBodyReader(uri, this.jsonBodyReader, true);
125 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
126 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
127 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
128 null, null, this.mediaType, null, inputStream);
129 checkNormalizedNodeContext(returnValue);
130 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
131 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
132 .create(inputNode.getNodeType(), "cont"));
133 final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
134 .getChild(yangCont.getLastPathArgument());
135 assertTrue(contDataNode.isPresent());
136 assertTrue(contDataNode.get() instanceof ContainerNode);
137 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
138 .create(inputNode.getNodeType(), "lf"));
139 final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode =
140 ((ContainerNode) contDataNode.get()).getChild(yangleaf.getLastPathArgument());
141 assertTrue(leafDataNode.isPresent());
142 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
146 private void checkExpectValueNormalizeNodeContext(
147 final DataSchemaNode dataSchemaNode,
148 final NormalizedNodeContext nnContext) {
149 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
152 protected void checkExpectValueNormalizeNodeContext(
153 final DataSchemaNode dataSchemaNode,
154 final NormalizedNodeContext nnContext, final QName qualifiedName) {
155 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
156 .of(dataSchemaNode.getQName());
157 final DOMMountPoint mountPoint = nnContext
158 .getInstanceIdentifierContext().getMountPoint();
159 final DataSchemaNode mountDataSchemaNode = mountPoint
160 .getSchemaContext().getDataChildByName(
161 dataSchemaNode.getQName());
162 assertNotNull(mountDataSchemaNode);
163 if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
164 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
165 .getDataChildByName(qualifiedName);
166 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
167 .node(child.getQName()).build();
168 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
171 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
173 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),