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
8 package org.opendaylight.controller.sal.rest.impl.test.providers;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.mock;
15 import java.io.InputStream;
16 import java.util.Collection;
17 import java.util.Optional;
18 import javax.ws.rs.core.MediaType;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
22 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
23 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
24 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.common.Revision;
28 import org.opendaylight.yangtools.yang.common.XMLNamespace;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
33 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
36 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
38 public class TestJsonBodyReaderMountPoint extends AbstractBodyReaderTest {
40 private final JsonNormalizedNodeBodyReader jsonBodyReader;
41 private static EffectiveModelContext schemaContext;
43 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
44 XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
46 public TestJsonBodyReaderMountPoint() throws NoSuchFieldException, SecurityException {
47 super(schemaContext, mock(DOMMountPoint.class));
48 jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext);
52 protected MediaType getMediaType() {
53 return new MediaType(MediaType.APPLICATION_XML, null);
57 public static void initialization() throws Exception {
58 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
59 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
60 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
64 public void moduleDataTest() throws Exception {
65 final DataSchemaNode dataSchemaNode = schemaContext
66 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
67 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
68 mockBodyReader(uri, jsonBodyReader, false);
69 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
70 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
71 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
72 null, null, mediaType, null, inputStream);
73 checkMountPointNormalizedNodeContext(returnValue);
74 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
78 public void moduleSubContainerDataPutTest() throws Exception {
79 final DataSchemaNode dataSchemaNode = schemaContext
80 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
81 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
82 mockBodyReader(uri, jsonBodyReader, false);
83 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
84 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
85 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
86 null, null, mediaType, null, inputStream);
87 checkMountPointNormalizedNodeContext(returnValue);
88 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
89 QName.create(dataSchemaNode.getQName(), "cont1"));
93 public void moduleSubContainerDataPostTest() 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";
97 mockBodyReader(uri, jsonBodyReader, true);
98 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
99 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
100 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
101 null, null, mediaType, null, inputStream);
102 checkMountPointNormalizedNodeContext(returnValue);
103 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
107 public void rpcModuleInputTest() throws Exception {
108 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
109 mockBodyReader(uri, jsonBodyReader, true);
110 final InputStream inputStream = TestJsonBodyReaderMountPoint.class.getResourceAsStream(
111 "/invoke-rpc/json/rpc-input.json");
112 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null, null, null, mediaType, null,
114 checkNormalizedNodeContextRpc(returnValue);
115 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
116 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(
117 QName.create(inputNode.getIdentifier().getNodeType(), "cont"));
118 final Optional<DataContainerChild> contDataNode = inputNode.findChildByArg(yangCont.getLastPathArgument());
119 assertTrue(contDataNode.isPresent());
120 assertTrue(contDataNode.get() instanceof ContainerNode);
121 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(
122 QName.create(inputNode.getIdentifier().getNodeType(), "lf"));
123 final Optional<DataContainerChild> leafDataNode =
124 ((ContainerNode) contDataNode.get()).findChildByArg(yangleaf.getLastPathArgument());
125 assertTrue(leafDataNode.isPresent());
126 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().body().toString()));
129 private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
130 final NormalizedNodeContext nnContext) {
131 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
134 protected void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
135 final NormalizedNodeContext nnContext, final QName qualifiedName) {
136 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier.of(dataSchemaNode.getQName());
137 final DOMMountPoint mountPoint = nnContext.getInstanceIdentifierContext().getMountPoint();
138 final DataSchemaNode mountDataSchemaNode = modelContext(mountPoint).getDataChildByName(
139 dataSchemaNode.getQName());
140 assertNotNull(mountDataSchemaNode);
141 if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
142 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode).getDataChildByName(qualifiedName);
143 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent).node(child.getQName()).build();
144 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode().equals(child));
146 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
148 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),