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.text.ParseException;
22 import java.util.Collection;
23 import javax.ws.rs.core.MediaType;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
27 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
28 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
29 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
30 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
40 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
41 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45 public class TestJsonBodyReaderMountPoint extends AbstractBodyReaderTest {
47 private final JsonNormalizedNodeBodyReader jsonBodyReader;
48 private static SchemaContext schemaContext;
50 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
52 private static QNameModule initializeInstanceIdentifierModule() {
54 return QNameModule.create(URI.create("instance:identifier:module"),
55 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
56 } catch (final ParseException e) {
61 public TestJsonBodyReaderMountPoint() throws NoSuchFieldException,
64 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
68 protected MediaType getMediaType() {
69 return new MediaType(MediaType.APPLICATION_XML, null);
73 public static void initialization() throws Exception {
74 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
75 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
76 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
78 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
79 when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
80 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
81 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
82 .thenReturn(Optional.of(mountInstance));
84 ControllerContext.getInstance().setMountService(mockMountService);
85 CONTROLLER_CONTEXT.setSchemas(schemaContext);
89 public void moduleDataTest() throws Exception {
90 final DataSchemaNode dataSchemaNode = schemaContext
91 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
92 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
93 mockBodyReader(uri, this.jsonBodyReader, false);
94 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
95 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
96 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
97 null, null, this.mediaType, null, inputStream);
98 checkMountPointNormalizedNodeContext(returnValue);
99 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
103 public void moduleSubContainerDataPutTest() throws Exception {
104 final DataSchemaNode dataSchemaNode = schemaContext
105 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
106 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
107 mockBodyReader(uri, this.jsonBodyReader, false);
108 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
109 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
110 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
111 null, null, this.mediaType, null, inputStream);
112 checkMountPointNormalizedNodeContext(returnValue);
113 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
114 QName.create(dataSchemaNode.getQName(), "cont1"));
118 public void moduleSubContainerDataPostTest() throws Exception {
119 final DataSchemaNode dataSchemaNode = schemaContext
120 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
121 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
122 mockBodyReader(uri, this.jsonBodyReader, true);
123 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
124 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
125 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
126 null, null, this.mediaType, null, inputStream);
127 checkMountPointNormalizedNodeContext(returnValue);
128 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
132 public void rpcModuleInputTest() throws Exception {
133 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
134 mockBodyReader(uri, this.jsonBodyReader, true);
135 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
136 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
137 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
138 null, null, this.mediaType, null, inputStream);
139 checkNormalizedNodeContext(returnValue);
140 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
141 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
142 .create(inputNode.getNodeType(), "cont"));
143 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
144 .getChild(yangCont.getLastPathArgument());
145 assertTrue(contDataNode.isPresent());
146 assertTrue(contDataNode.get() instanceof ContainerNode);
147 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
148 .create(inputNode.getNodeType(), "lf"));
149 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
150 .get()).getChild(yangleaf.getLastPathArgument());
151 assertTrue(leafDataNode.isPresent());
152 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
156 private void checkExpectValueNormalizeNodeContext(
157 final DataSchemaNode dataSchemaNode,
158 final NormalizedNodeContext nnContext) {
159 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
162 protected void checkExpectValueNormalizeNodeContext(
163 final DataSchemaNode dataSchemaNode,
164 final NormalizedNodeContext nnContext, final QName qualifiedName) {
165 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
166 .of(dataSchemaNode.getQName());
167 final DOMMountPoint mountPoint = nnContext
168 .getInstanceIdentifierContext().getMountPoint();
169 final DataSchemaNode mountDataSchemaNode = mountPoint
170 .getSchemaContext().getDataChildByName(
171 dataSchemaNode.getQName());
172 assertNotNull(mountDataSchemaNode);
173 if ((qualifiedName != null) && (dataSchemaNode instanceof DataNodeContainer)) {
174 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
175 .getDataChildByName(qualifiedName);
176 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
177 .node(child.getQName()).build();
178 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
181 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
183 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),