Bump upstreams
[netconf.git] / restconf / restconf-nb / 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 javax.ws.rs.core.MediaType;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
22 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
23 import org.opendaylight.restconf.common.errors.RestconfError;
24 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
25 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
26 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
27 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
28 import org.opendaylight.yangtools.yang.common.ErrorTag;
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.YangInstanceIdentifier.NodeIdentifier;
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         xmlBodyReader = new XmlNormalizedNodeBodyReader(databindProvider, 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, xmlBodyReader, false);
75         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
76             XmlBodyReaderMountPointTest.class.getResourceAsStream("/instanceidentifier/xml/xmldata.xml"));
77         checkMountPointNormalizedNodePayload(payload);
78         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload);
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, xmlBodyReader, false);
87         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
88             XmlBodyReaderMountPointTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
89         checkMountPointNormalizedNodePayload(payload);
90         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload,
91                 QName.create(dataSchemaNode.getQName(), "cont1"));
92     }
93
94     @Test
95     public void moduleSubContainerDataPostTest() throws Exception {
96         final DataSchemaNode dataSchemaNode = schemaContext
97                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
98         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
99         mockBodyReader(uri, xmlBodyReader, true);
100         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
101             XmlBodyReaderMountPointTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
102         checkMountPointNormalizedNodePayload(payload);
103         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload);
104     }
105
106     @Test
107     public void moduleSubContainerDataPostActionTest() throws Exception {
108         final DataSchemaNode dataSchemaNode = schemaContext
109             .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
110         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1/reset";
111         mockBodyReader(uri, xmlBodyReader, true);
112         final NormalizedNodePayload pyaload = xmlBodyReader.readFrom(null,null, null, mediaType, null,
113             XmlBodyReaderMountPointTest.class.getResourceAsStream("/instanceidentifier/xml/xml_cont_action.xml"));
114         checkMountPointNormalizedNodePayload(pyaload);
115         checkExpectValueNormalizeNodeContext(dataSchemaNode, pyaload);
116     }
117
118     @Test
119     public void rpcModuleInputTest() throws Exception {
120         final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
121         mockBodyReader(uri, xmlBodyReader, true);
122         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
123             XmlBodyReaderMountPointTest.class.getResourceAsStream("/invoke-rpc/xml/rpc-input.xml"));
124         checkNormalizedNodePayload(payload);
125         final ContainerNode contNode = (ContainerNode) payload.getData();
126         final ContainerNode contDataNode = (ContainerNode) contNode.getChildByArg(
127             new NodeIdentifier(QName.create(contNode.name().getNodeType(), "cont")));
128         final DataContainerChild leafDataNode = contDataNode.getChildByArg(
129             new NodeIdentifier(QName.create(contDataNode.name().getNodeType(), "lf")));
130         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.body().toString()));
131     }
132
133     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
134             final NormalizedNodePayload nnContext) {
135         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
136     }
137
138     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
139             final NormalizedNodePayload nnContext, final QName qualifiedName) {
140         YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier.of(dataSchemaNode.getQName());
141         final DOMMountPoint mountPoint = nnContext.getInstanceIdentifierContext().getMountPoint();
142         final DataSchemaNode mountDataSchemaNode = modelContext(mountPoint)
143                 .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             assertEquals(nnContext.getInstanceIdentifierContext().getSchemaNode(), child);
149         } else {
150             assertEquals(mountDataSchemaNode, 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", xmlBodyReader, true);
163         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
164             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml"));
165
166         // check return value
167         checkMountPointNormalizedNodePayload(payload);
168         // check if container was found both according to its name and namespace
169         final var dataNodeType = payload.getData().getIdentifier().getNodeType();
170         assertEquals("foo-bar-container", dataNodeType.getLocalName());
171         assertEquals("foo:module", dataNodeType.getNamespace().toString());
172     }
173
174     /**
175      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
176      * used to distinguish between them to find correct one. Check if container was found not only according to its name
177      * but also by correct namespace used in payload.
178      */
179     @Test
180     public void findBarContainerUsingNamespaceTest() throws Exception {
181         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, true);
182         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
183             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml"));
184
185         // check return value
186         checkMountPointNormalizedNodePayload(payload);
187         // check if container was found both according to its name and namespace
188         final var dataNodeType = payload.getData().getIdentifier().getNodeType();
189         assertEquals("foo-bar-container", dataNodeType.getLocalName());
190         assertEquals("bar:module", dataNodeType.getNamespace().toString());
191     }
192
193     /**
194      * Test PUT operation when message root element is not the same as the last element in request URI.
195      * PUT operation message should always start with schema node from URI otherwise exception should be
196      * thrown.
197      */
198     @Test
199     public void wrongRootElementTest() throws Exception {
200         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, false);
201         final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
202             "/instanceidentifier/xml/bug7933.xml");
203
204         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
205             () -> xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
206         final RestconfError restconfError = ex.getErrors().get(0);
207         assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
208         assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
209     }
210 }