8ad7d0a8c17e97cb2b831b8097c2d4957cb992ef
[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.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 TestXmlBodyReaderMountPoint extends AbstractBodyReaderTest {
44     private final XmlNormalizedNodeBodyReader xmlBodyReader;
45     private static EffectiveModelContext schemaContext;
46
47     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
48         XMLNamespace.of("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(
121                 QName.create(contNode.getIdentifier().getNodeType(), "cont"));
122         final Optional<DataContainerChild> contDataNodePotential =
123                 contNode.findChildByArg(yangCont.getLastPathArgument());
124         assertTrue(contDataNodePotential.isPresent());
125         final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
126         final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(
127                 QName.create(contDataNode.getIdentifier().getNodeType(), "lf"));
128         final Optional<DataContainerChild> leafDataNode = contDataNode.findChildByArg(yangLeaf.getLastPathArgument());
129         assertTrue(leafDataNode.isPresent());
130         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().body().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, final NormalizedNodeContext nnContext, final QName qualifiedName) {
141         YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier.of(dataSchemaNode.getQName());
142         final DOMMountPoint mountPoint = nnContext.getInstanceIdentifierContext().getMountPoint();
143         final DataSchemaNode mountDataSchemaNode =
144                 modelContext(mountPoint).getDataChildByName(dataSchemaNode.getQName());
145         assertNotNull(mountDataSchemaNode);
146         if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
147             final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode).getDataChildByName(qualifiedName);
148             dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent).node(child.getQName()).build();
149             assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode().equals(child));
150         } else {
151             assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
152         }
153         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
154     }
155
156     /**
157      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
158      * used to distinguish between them to find correct one. Check if container was found not only according to its name
159      * but also by correct namespace used in payload.
160      */
161     @Test
162     public void findFooContainerUsingNamespaceTest() throws Exception {
163         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
164         final InputStream inputStream = TestXmlBodyReader.class
165                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
166         final NormalizedNodeContext returnValue = this.xmlBodyReader
167                 .readFrom(null, null, null, this.mediaType, null, inputStream);
168
169         // check return value
170         checkMountPointNormalizedNodeContext(returnValue);
171         // check if container was found both according to its name and namespace
172         assertEquals("Not correct container found, name was ignored",
173                 "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
174         assertEquals("Not correct container found, namespace was ignored",
175                 "foo:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
176     }
177
178     /**
179      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
180      * used to distinguish between them to find correct one. Check if container was found not only according to its name
181      * but also by correct namespace used in payload.
182      */
183     @Test
184     public void findBarContainerUsingNamespaceTest() throws Exception {
185         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
186         final InputStream inputStream = TestXmlBodyReader.class
187                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
188         final NormalizedNodeContext returnValue = this.xmlBodyReader
189                 .readFrom(null, null, null, this.mediaType, null, inputStream);
190
191         // check return value
192         checkMountPointNormalizedNodeContext(returnValue);
193         // check if container was found both according to its name and namespace
194         assertEquals("Not correct container found, name was ignored",
195                 "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
196         assertEquals("Not correct container found, namespace was ignored",
197                 "bar:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
198     }
199
200     /**
201      * Test PUT operation when message root element is not the same as the last element in request URI.
202      * PUT operation message should always start with schema node from URI otherwise exception should be
203      * thrown.
204      */
205     @Test
206     public void wrongRootElementTest() throws Exception {
207         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, false);
208         final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
209                 "/instanceidentifier/xml/bug7933.xml");
210         try {
211             this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
212             Assert.fail("Test should fail due to malformed PUT operation message");
213         } catch (final RestconfDocumentedException exception) {
214             final RestconfError restconfError = exception.getErrors().get(0);
215             Assert.assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
216             Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
217         }
218     }
219 }