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