Adjust to yangtools-2.0.0/odlparent-3.0.0 changes
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / 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.nb.rfc8040.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.util.Collection;
23 import javax.ws.rs.core.MediaType;
24 import org.junit.Assert;
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.restconf.common.context.NormalizedNodeContext;
30 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
31 import org.opendaylight.restconf.common.errors.RestconfError;
32 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
33 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
34 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
35 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.common.QNameModule;
38 import org.opendaylight.yangtools.yang.common.Revision;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
41 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
44 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
45 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
48
49 public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
50     private final XmlNormalizedNodeBodyReader xmlBodyReader;
51     private static SchemaContext schemaContext;
52
53     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME =  QNameModule.create(
54         URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
55
56     public XmlBodyReaderMountPointTest() throws Exception {
57         this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
58     }
59
60     @Override
61     protected MediaType getMediaType() {
62         return new MediaType(MediaType.APPLICATION_XML, null);
63     }
64
65     @BeforeClass
66     public static void initialization() throws Exception {
67         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
68         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
69         schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
70
71         final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
72         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
73
74         when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mountPointService);
75         when(mountPointService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountPoint));
76         when(mountPoint.getSchemaContext()).thenReturn(schemaContext);
77         SchemaContextHandler.setActualSchemaContext(schemaContext);
78     }
79
80     @Test
81     public void moduleDataTest() throws Exception {
82         final DataSchemaNode dataSchemaNode = schemaContext
83                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
84         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
85         mockBodyReader(uri, this.xmlBodyReader, false);
86         final InputStream inputStream = XmlBodyReaderMountPointTest.class
87                 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
88         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
89                 null, null, this.mediaType, null, inputStream);
90         checkMountPointNormalizedNodeContext(returnValue);
91         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
92     }
93
94     @Test
95     public void moduleSubContainerDataPutTest() throws Exception {
96         final DataSchemaNode dataSchemaNode = schemaContext
97                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
98         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
99         mockBodyReader(uri, this.xmlBodyReader, false);
100         final InputStream inputStream = XmlBodyReaderMountPointTest.class
101                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
102         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
103                 null, null, this.mediaType, null, inputStream);
104         checkMountPointNormalizedNodeContext(returnValue);
105         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
106                 QName.create(dataSchemaNode.getQName(), "cont1"));
107     }
108
109     @Test
110     public void moduleSubContainerDataPostTest() throws Exception {
111         final DataSchemaNode dataSchemaNode = schemaContext
112                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
113         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
114         mockBodyReader(uri, this.xmlBodyReader, true);
115         final InputStream inputStream = XmlBodyReaderMountPointTest.class
116                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
117         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
118                 null, null, this.mediaType, null, inputStream);
119         checkMountPointNormalizedNodeContext(returnValue);
120         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
121     }
122
123     @Test
124     public void rpcModuleInputTest() throws Exception {
125         final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
126         mockBodyReader(uri, this.xmlBodyReader, true);
127         final InputStream inputStream = XmlBodyReaderMountPointTest.class
128                 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
129         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
130                 null, null, this.mediaType, null, inputStream);
131         checkNormalizedNodeContext(returnValue);
132         final ContainerNode contNode = (ContainerNode) returnValue.getData();
133         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
134         final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode
135                 .getChild(yangCont.getLastPathArgument());
136         assertTrue(contDataNodePotential.isPresent());
137         final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
138         final YangInstanceIdentifier yangLeaf =
139                 YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
140         final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(
141                 yangLeaf.getLastPathArgument());
142         assertTrue(leafDataNode.isPresent());
143         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
144     }
145
146     private void checkExpectValueNormalizeNodeContext(
147             final DataSchemaNode dataSchemaNode,
148             final NormalizedNodeContext nnContext) {
149         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
150     }
151
152     private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
153             final NormalizedNodeContext nnContext, final QName qualifiedName) {
154         YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
155                 .of(dataSchemaNode.getQName());
156         final DOMMountPoint mountPoint = nnContext
157                 .getInstanceIdentifierContext().getMountPoint();
158         final DataSchemaNode mountDataSchemaNode = mountPoint
159                 .getSchemaContext().getDataChildByName(
160                         dataSchemaNode.getQName());
161         assertNotNull(mountDataSchemaNode);
162         if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
163             final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
164                     .getDataChildByName(qualifiedName);
165             dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
166                     .node(child.getQName()).build();
167             assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
168                     .equals(child));
169         } else {
170             assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
171         }
172         assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
173                 dataNodeIdent));
174     }
175
176     /**
177      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
178      * used to distinguish between them to find correct one. Check if container was found not only according to its name
179      * but also by correct namespace used in payload.
180      */
181     @Test
182     public void findFooContainerUsingNamespaceTest() throws Exception {
183         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
184         final InputStream inputStream = XmlBodyReaderTest.class
185                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
186         final NormalizedNodeContext returnValue = this.xmlBodyReader
187                 .readFrom(null, null, null, this.mediaType, null, inputStream);
188
189         // check return value
190         checkMountPointNormalizedNodeContext(returnValue);
191         // check if container was found both according to its name and namespace
192         assertEquals("Not correct container found, name was ignored",
193                 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
194         assertEquals("Not correct container found, namespace was ignored",
195                 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
196     }
197
198     /**
199      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
200      * used to distinguish between them to find correct one. Check if container was found not only according to its name
201      * but also by correct namespace used in payload.
202      */
203     @Test
204     public void findBarContainerUsingNamespaceTest() throws Exception {
205         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
206         final InputStream inputStream = XmlBodyReaderTest.class
207                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
208         final NormalizedNodeContext returnValue = this.xmlBodyReader
209                 .readFrom(null, null, null, this.mediaType, null, inputStream);
210
211         // check return value
212         checkMountPointNormalizedNodeContext(returnValue);
213         // check if container was found both according to its name and namespace
214         assertEquals("Not correct container found, name was ignored",
215                 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
216         assertEquals("Not correct container found, namespace was ignored",
217                 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
218     }
219
220     /**
221      * Test PUT operation when message root element is not the same as the last element in request URI.
222      * PUT operation message should always start with schema node from URI otherwise exception should be
223      * thrown.
224      */
225     @Test
226     public void wrongRootElementTest() throws Exception {
227         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, false);
228         final InputStream inputStream =
229                 XmlBodyReaderTest.class.getResourceAsStream(
230                 "/instanceidentifier/xml/bug7933.xml");
231         try {
232             this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
233             Assert.fail("Test should fail due to malformed PUT operation message");
234         } catch (final RestconfDocumentedException exception) {
235             final RestconfError restconfError = exception.getErrors().get(0);
236             Assert.assertEquals(RestconfError.ErrorType.PROTOCOL, restconfError.getErrorType());
237             Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
238         }
239     }
240 }