Merge "Migrate restconf to MD-SAL APIs"
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestJsonBodyReaderMountPoint.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.rest.impl.test.providers;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.mock;
13
14 import java.io.File;
15 import java.io.InputStream;
16 import java.net.URI;
17 import java.util.Collection;
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.restconf.common.context.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.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
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.SchemaContext;
36 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
37
38 public class TestJsonBodyReaderMountPoint extends AbstractBodyReaderTest {
39
40     private final JsonNormalizedNodeBodyReader jsonBodyReader;
41     private static SchemaContext schemaContext;
42
43     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
44         URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
45
46     public TestJsonBodyReaderMountPoint() throws NoSuchFieldException, SecurityException {
47         super(schemaContext, mock(DOMMountPoint.class));
48         this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext);
49     }
50
51     @Override
52     protected MediaType getMediaType() {
53         return new MediaType(MediaType.APPLICATION_XML, null);
54     }
55
56     @BeforeClass
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);
61     }
62
63     @Test
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, this.jsonBodyReader, false);
69         final InputStream inputStream = TestJsonBodyReaderMountPoint.class
70                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
71         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
72                 null, null, this.mediaType, null, inputStream);
73         checkMountPointNormalizedNodeContext(returnValue);
74         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
75     }
76
77     @Test
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, this.jsonBodyReader, false);
83         final InputStream inputStream = TestJsonBodyReaderMountPoint.class
84                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
85         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
86                 null, null, this.mediaType, null, inputStream);
87         checkMountPointNormalizedNodeContext(returnValue);
88         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
89                 QName.create(dataSchemaNode.getQName(), "cont1"));
90     }
91
92     @Test
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, this.jsonBodyReader, true);
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     }
105
106     @Test
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, this.jsonBodyReader, true);
110         final InputStream inputStream = TestJsonBodyReaderMountPoint.class
111                 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
112         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
113                 null, null, this.mediaType, null, inputStream);
114         checkNormalizedNodeContext(returnValue);
115         final ContainerNode inputNode = (ContainerNode) returnValue.getData();
116         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
117                 .create(inputNode.getNodeType(), "cont"));
118         final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
119                 .getChild(yangCont.getLastPathArgument());
120         assertTrue(contDataNode.isPresent());
121         assertTrue(contDataNode.get() instanceof ContainerNode);
122         final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
123                 .create(inputNode.getNodeType(), "lf"));
124         final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode =
125                 ((ContainerNode) contDataNode.get()).getChild(yangleaf.getLastPathArgument());
126         assertTrue(leafDataNode.isPresent());
127         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
128                 .toString()));
129     }
130
131     private void checkExpectValueNormalizeNodeContext(
132             final DataSchemaNode dataSchemaNode,
133             final NormalizedNodeContext nnContext) {
134         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
135     }
136
137     protected void checkExpectValueNormalizeNodeContext(
138             final DataSchemaNode dataSchemaNode,
139             final NormalizedNodeContext nnContext, final QName qualifiedName) {
140         YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
141                 .of(dataSchemaNode.getQName());
142         final DOMMountPoint mountPoint = nnContext
143                 .getInstanceIdentifierContext().getMountPoint();
144         final DataSchemaNode mountDataSchemaNode = mountPoint
145                 .getSchemaContext().getDataChildByName(
146                         dataSchemaNode.getQName());
147         assertNotNull(mountDataSchemaNode);
148         if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
149             final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
150                     .getDataChildByName(qualifiedName);
151             dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
152                     .node(child.getQName()).build();
153             assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
154                     .equals(child));
155         } else {
156             assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
157         }
158         assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
159                 dataNodeIdent));
160     }
161 }