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