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