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;
46 * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
50 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
52 * Created: Mar 11, 2015
54 public class TestJsonBodyReaderMountPoint extends AbstractBodyReaderTest {
56 private final JsonNormalizedNodeBodyReader jsonBodyReader;
57 private static SchemaContext schemaContext;
59 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
61 private static QNameModule initializeInstanceIdentifierModule() {
63 return QNameModule.create(URI.create("instance:identifier:module"),
64 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
65 } catch (final ParseException e) {
70 public TestJsonBodyReaderMountPoint() throws NoSuchFieldException,
73 this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
77 protected MediaType getMediaType() {
78 return new MediaType(MediaType.APPLICATION_XML, null);
82 public static void initialization() throws Exception {
83 final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
84 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
85 schemaContext = YangParserTestUtils.parseYangSources(testFiles);
87 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
88 when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
89 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
90 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
91 .thenReturn(Optional.of(mountInstance));
93 ControllerContext.getInstance().setMountService(mockMountService);
94 controllerContext.setSchemas(schemaContext);
98 public void moduleDataTest() throws Exception {
99 final DataSchemaNode dataSchemaNode = schemaContext
100 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
101 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
102 mockBodyReader(uri, this.jsonBodyReader, false);
103 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
104 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
105 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
106 null, null, this.mediaType, null, inputStream);
107 checkMountPointNormalizedNodeContext(returnValue);
108 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
112 public void moduleSubContainerDataPutTest() throws Exception {
113 final DataSchemaNode dataSchemaNode = schemaContext
114 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
115 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
116 mockBodyReader(uri, this.jsonBodyReader, false);
117 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
118 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
119 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
120 null, null, this.mediaType, null, inputStream);
121 checkMountPointNormalizedNodeContext(returnValue);
122 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
123 QName.create(dataSchemaNode.getQName(), "cont1"));
127 public void moduleSubContainerDataPostTest() throws Exception {
128 final DataSchemaNode dataSchemaNode = schemaContext
129 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
130 final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
131 mockBodyReader(uri, this.jsonBodyReader, true);
132 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
133 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
134 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
135 null, null, this.mediaType, null, inputStream);
136 checkMountPointNormalizedNodeContext(returnValue);
137 checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
141 public void rpcModuleInputTest() throws Exception {
142 final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
143 mockBodyReader(uri, this.jsonBodyReader, true);
144 final InputStream inputStream = TestJsonBodyReaderMountPoint.class
145 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
146 final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
147 null, null, this.mediaType, null, inputStream);
148 checkNormalizedNodeContext(returnValue);
149 final ContainerNode inputNode = (ContainerNode) returnValue.getData();
150 final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
151 .create(inputNode.getNodeType(), "cont"));
152 final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
153 .getChild(yangCont.getLastPathArgument());
154 assertTrue(contDataNode.isPresent());
155 assertTrue(contDataNode.get() instanceof ContainerNode);
156 final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
157 .create(inputNode.getNodeType(), "lf"));
158 final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
159 .get()).getChild(yangleaf.getLastPathArgument());
160 assertTrue(leafDataNode.isPresent());
161 assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
165 private void checkExpectValueNormalizeNodeContext(
166 final DataSchemaNode dataSchemaNode,
167 final NormalizedNodeContext nnContext) {
168 checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
171 protected void checkExpectValueNormalizeNodeContext(
172 final DataSchemaNode dataSchemaNode,
173 final NormalizedNodeContext nnContext, final QName qName) {
174 YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
175 .of(dataSchemaNode.getQName());
176 final DOMMountPoint mountPoint = nnContext
177 .getInstanceIdentifierContext().getMountPoint();
178 final DataSchemaNode mountDataSchemaNode = mountPoint
179 .getSchemaContext().getDataChildByName(
180 dataSchemaNode.getQName());
181 assertNotNull(mountDataSchemaNode);
182 if ((qName != null) && (dataSchemaNode instanceof DataNodeContainer)) {
183 final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
184 .getDataChildByName(qName);
185 dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
186 .node(child.getQName()).build();
187 assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
190 assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
192 assertNotNull(NormalizedNodes.findNode(nnContext.getData(),