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