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.restconf.common.context.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,
63 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
67 protected MediaType getMediaType() {
68 return new MediaType(MediaType.APPLICATION_XML, null);
72 public static void initialization() throws Exception {
73 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
74 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
75 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
77 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
78 when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
79 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
80 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
81 .thenReturn(Optional.of(mountInstance));
83 ControllerContext.getInstance().setMountService(mockMountService);
84 CONTROLLER_CONTEXT.setSchemas(schemaContext);
88 public void moduleDataTest() throws Exception {
89 final DataSchemaNode dataSchemaNode = schemaContext
90 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
91 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
92 mockBodyReader(uri, this.jsonBodyReader, false);
93 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
94 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
95 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
96 null, null, this.mediaType, null, inputStream);
97 checkMountPointNormalizedNodeContext(returnValue);
98 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
102 public void moduleSubContainerDataPutTest() throws Exception {
103 final DataSchemaNode dataSchemaNode = schemaContext
104 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
105 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
106 mockBodyReader(uri, this.jsonBodyReader, false);
107 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
108 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
109 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
110 null, null, this.mediaType, null, inputStream);
111 checkMountPointNormalizedNodeContext(returnValue);
112 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
113 QName.create(dataSchemaNode.getQName(), "cont1"));
117 public void moduleSubContainerDataPostTest() throws Exception {
118 final DataSchemaNode dataSchemaNode = schemaContext
119 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
120 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
121 mockBodyReader(uri, this.jsonBodyReader, true);
122 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
123 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
124 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
125 null, null, this.mediaType, null, inputStream);
126 checkMountPointNormalizedNodeContext(returnValue);
127 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
131 public void rpcModuleInputTest() throws Exception {
132 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
133 mockBodyReader(uri, this.jsonBodyReader, true);
134 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
135 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
136 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
137 null, null, this.mediaType, null, inputStream);
138 checkNormalizedNodeContext(returnValue);
139 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
140 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
141 .create(inputNode.getNodeType(), "cont"));
142 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
143 .getChild(yangCont.getLastPathArgument());
144 assertTrue(contDataNode.isPresent());
145 assertTrue(contDataNode.get() instanceof ContainerNode);
146 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
147 .create(inputNode.getNodeType(), "lf"));
148 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
149 .get()).getChild(yangleaf.getLastPathArgument());
150 assertTrue(leafDataNode.isPresent());
151 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
155 private void checkExpectValueNormalizeNodeContext(
156 final DataSchemaNode dataSchemaNode,
157 final NormalizedNodeContext nnContext) {
158 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
161 protected void checkExpectValueNormalizeNodeContext(
162 final DataSchemaNode dataSchemaNode,
163 final NormalizedNodeContext nnContext, final QName qualifiedName) {
164 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
165 .of(dataSchemaNode.getQName());
166 final DOMMountPoint mountPoint = nnContext
167 .getInstanceIdentifierContext().getMountPoint();
168 final DataSchemaNode mountDataSchemaNode = mountPoint
169 .getSchemaContext().getDataChildByName(
170 dataSchemaNode.getQName());
171 assertNotNull(mountDataSchemaNode);
172 if ((qualifiedName != null) && (dataSchemaNode instanceof DataNodeContainer)) {
173 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
174 .getDataChildByName(qualifiedName);
175 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
176 .node(child.getQName()).build();
177 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
180 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
182 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),