Bug 8533: Not possible to invoke RPC on mount points with new Restconf
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / jersey / providers / XmlBodyReaderMountPointTest.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
9 package org.opendaylight.restconf.jersey.providers;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.base.Optional;
19 import java.io.File;
20 import java.io.InputStream;
21 import java.net.URI;
22 import java.text.ParseException;
23 import java.util.Collection;
24 import javax.ws.rs.core.MediaType;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
28 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
29 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
30 import org.opendaylight.controller.sal.rest.impl.test.providers.TestXmlBodyReader;
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
44 public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
45     private final XmlNormalizedNodeBodyReader xmlBodyReader;
46     private static SchemaContext schemaContext;
47
48     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
49
50     private static QNameModule initializeInstanceIdentifierModule() {
51         try {
52             return QNameModule.create(URI.create("instance:identifier:module"),
53                 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
54         } catch (final ParseException e) {
55             throw new Error(e);
56         }
57     }
58
59     public XmlBodyReaderMountPointTest() throws Exception {
60         super();
61         this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
62     }
63
64     @Override
65     protected MediaType getMediaType() {
66         return new MediaType(MediaType.APPLICATION_XML, null);
67     }
68
69     @BeforeClass
70     public static void initialization() throws Exception {
71         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
72         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
73         schemaContext = TestRestconfUtils.parseYangSources(testFiles);
74
75         final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
76         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
77
78         when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mountPointService);
79         when(mountPointService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountPoint));
80         when(mountPoint.getSchemaContext()).thenReturn(schemaContext);
81
82         CONTROLLER_CONTEXT.setSchemas(schemaContext);
83     }
84
85     @Test
86     public void moduleDataTest() throws Exception {
87         final DataSchemaNode dataSchemaNode = schemaContext
88                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
89         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
90         mockBodyReader(uri, this.xmlBodyReader, false);
91         final InputStream inputStream = XmlBodyReaderMountPointTest.class
92                 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
93         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
94                 null, null, this.mediaType, null, inputStream);
95         checkMountPointNormalizedNodeContext(returnValue);
96         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
97     }
98
99     @Test
100     public void moduleSubContainerDataPutTest() throws Exception {
101         final DataSchemaNode dataSchemaNode = schemaContext
102                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
103         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
104         mockBodyReader(uri, this.xmlBodyReader, false);
105         final InputStream inputStream = XmlBodyReaderMountPointTest.class
106                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
107         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
108                 null, null, this.mediaType, null, inputStream);
109         checkMountPointNormalizedNodeContext(returnValue);
110         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
111                 QName.create(dataSchemaNode.getQName(), "cont1"));
112     }
113
114     @Test
115     public void moduleSubContainerDataPostTest() throws Exception {
116         final DataSchemaNode dataSchemaNode = schemaContext
117                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
118         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
119         mockBodyReader(uri, this.xmlBodyReader, true);
120         final InputStream inputStream = XmlBodyReaderMountPointTest.class
121                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
122         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
123                 null, null, this.mediaType, null, inputStream);
124         checkMountPointNormalizedNodeContext(returnValue);
125         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
126     }
127
128     @Test
129     public void rpcModuleInputTest() throws Exception {
130         final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
131         mockBodyReader(uri, this.xmlBodyReader, true);
132         final InputStream inputStream = XmlBodyReaderMountPointTest.class
133                 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
134         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
135                 null, null, this.mediaType, null, inputStream);
136         checkNormalizedNodeContext(returnValue);
137         final ContainerNode contNode = (ContainerNode) returnValue.getData();
138         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
139         final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(
140                 yangCont.getLastPathArgument());
141         assertTrue(contDataNodePotential.isPresent());
142         final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
143         final YangInstanceIdentifier yangLeaf =
144                 YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
145         final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(
146                 yangLeaf.getLastPathArgument());
147         assertTrue(leafDataNode.isPresent());
148         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
149     }
150
151     private void checkExpectValueNormalizeNodeContext(
152             final DataSchemaNode dataSchemaNode,
153             final NormalizedNodeContext nnContext) {
154         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
155     }
156
157     private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
158             final NormalizedNodeContext nnContext, final QName qualifiedName) {
159         YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
160                 .of(dataSchemaNode.getQName());
161         final DOMMountPoint mountPoint = nnContext
162                 .getInstanceIdentifierContext().getMountPoint();
163         final DataSchemaNode mountDataSchemaNode = mountPoint
164                 .getSchemaContext().getDataChildByName(
165                         dataSchemaNode.getQName());
166         assertNotNull(mountDataSchemaNode);
167         if ((qualifiedName != null) && (dataSchemaNode instanceof DataNodeContainer)) {
168             final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
169                     .getDataChildByName(qualifiedName);
170             dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
171                     .node(child.getQName()).build();
172             assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
173                     .equals(child));
174         } else {
175             assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
176         }
177         assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
178                 dataNodeIdent));
179     }
180
181     /**
182      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
183      * used to distinguish between them to find correct one. Check if container was found not only according to its name
184      * but also by correct namespace used in payload.
185      */
186     @Test
187     public void findFooContainerUsingNamespaceTest() throws Exception {
188         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
189         final InputStream inputStream = TestXmlBodyReader.class
190                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
191         final NormalizedNodeContext returnValue = this.xmlBodyReader
192                 .readFrom(null, null, null, this.mediaType, null, inputStream);
193
194         // check return value
195         checkMountPointNormalizedNodeContext(returnValue);
196         // check if container was found both according to its name and namespace
197         assertEquals("Not correct container found, name was ignored",
198                 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
199         assertEquals("Not correct container found, namespace was ignored",
200                 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
201     }
202
203     /**
204      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
205      * used to distinguish between them to find correct one. Check if container was found not only according to its name
206      * but also by correct namespace used in payload.
207      */
208     @Test
209     public void findBarContainerUsingNamespaceTest() throws Exception {
210         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
211         final InputStream inputStream = TestXmlBodyReader.class
212                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
213         final NormalizedNodeContext returnValue = this.xmlBodyReader
214                 .readFrom(null, null, null, this.mediaType, null, inputStream);
215
216         // check return value
217         checkMountPointNormalizedNodeContext(returnValue);
218         // check if container was found both according to its name and namespace
219         assertEquals("Not correct container found, name was ignored",
220                 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
221         assertEquals("Not correct container found, namespace was ignored",
222                 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
223     }
224 }