Peel of RPC invocation InstanceIdentifierContext
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestXmlBodyReader.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.junit.Assert.fail;
14
15 import java.io.File;
16 import java.io.InputStream;
17 import java.util.Collection;
18 import java.util.Optional;
19 import java.util.Set;
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.netconf.sal.rest.impl.NormalizedNodeContext;
26 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
27 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
28 import org.opendaylight.restconf.common.errors.RestconfError;
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.YangInstanceIdentifier.AugmentationIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
42 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
43 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
45 import org.opendaylight.yangtools.yang.model.api.Module;
46 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47
48 public class TestXmlBodyReader extends AbstractBodyReaderTest {
49
50     private final XmlNormalizedNodeBodyReader xmlBodyReader;
51     private static EffectiveModelContext schemaContext;
52     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
53         XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
54
55     public TestXmlBodyReader() {
56         super(schemaContext, null);
57         xmlBodyReader = new XmlNormalizedNodeBodyReader(controllerContext);
58     }
59
60     @Override
61     protected MediaType getMediaType() {
62         return new MediaType(MediaType.APPLICATION_XML, null);
63     }
64
65     @BeforeClass
66     public static void initialization() throws Exception {
67         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
68         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
69         testFiles.addAll(TestRestconfUtils.loadFiles("/foo-xml-test/yang"));
70         schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
71     }
72
73     @Test
74     public void putXmlTest() throws Exception {
75         runXmlTest(false, "foo:top-level-list/key-value");
76     }
77
78     @Test
79     public void postXmlTest() throws Exception {
80         runXmlTest(true, "");
81     }
82
83     private void runXmlTest(final boolean isPost, final String path) throws Exception {
84         mockBodyReader(path, xmlBodyReader, isPost);
85         final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream("/foo-xml-test/foo.xml");
86         final NormalizedNodeContext nnc = xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
87         assertNotNull(nnc);
88
89         assertTrue(nnc.getData() instanceof MapEntryNode);
90         final MapEntryNode data = (MapEntryNode) nnc.getData();
91         assertEquals(2, data.size());
92         for (final DataContainerChild child : data.body()) {
93             switch (child.getIdentifier().getNodeType().getLocalName()) {
94                 case "key-leaf":
95                     assertEquals("key-value", child.body());
96                     break;
97
98                 case "ordinary-leaf":
99                     assertEquals("leaf-value", child.body());
100                     break;
101                 default:
102                     fail();
103             }
104         }
105     }
106
107     @Test
108     public void moduleDataTest() throws Exception {
109         final DataSchemaNode dataSchemaNode =
110                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
111         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
112         final String uri = "instance-identifier-module:cont";
113         mockBodyReader(uri, xmlBodyReader, false);
114         final InputStream inputStream = TestXmlBodyReader.class
115                 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
116         final NormalizedNodeContext returnValue = xmlBodyReader
117                 .readFrom(null, null, null, mediaType, null, inputStream);
118         checkNormalizedNodeContext(returnValue);
119         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
120     }
121
122     @Test
123     public void moduleSubContainerDataPutTest() throws Exception {
124         final DataSchemaNode dataSchemaNode =
125                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
126         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
127         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
128         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
129         final String uri = "instance-identifier-module:cont/cont1";
130         mockBodyReader(uri, xmlBodyReader, false);
131         final InputStream inputStream = TestXmlBodyReader.class
132                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
133         final NormalizedNodeContext returnValue = xmlBodyReader
134                 .readFrom(null, null, null, mediaType, null, inputStream);
135         checkNormalizedNodeContext(returnValue);
136         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
137     }
138
139     @Test
140     public void moduleSubContainerDataPostTest() throws Exception {
141         final DataSchemaNode dataSchemaNode =
142                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
143         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
144         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
145         final String uri = "instance-identifier-module:cont";
146         mockBodyReader(uri, xmlBodyReader, true);
147         final InputStream inputStream = TestXmlBodyReader.class
148                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
149         final NormalizedNodeContext returnValue = xmlBodyReader
150                 .readFrom(null, null, null, mediaType, null, inputStream);
151         checkNormalizedNodeContext(returnValue);
152         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
153     }
154
155     @Test
156     public void moduleSubContainerAugmentDataPostTest() throws Exception {
157         final DataSchemaNode dataSchemaNode =
158                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
159         final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
160         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
161         final AugmentationIdentifier augII = new AugmentationIdentifier(Set.of(contAugmentQName));
162         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
163                 .node(augII).node(contAugmentQName);
164         final String uri = "instance-identifier-module:cont";
165         mockBodyReader(uri, xmlBodyReader, true);
166         final InputStream inputStream = TestXmlBodyReader.class
167                 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
168         final NormalizedNodeContext returnValue = xmlBodyReader
169                 .readFrom(null, null, null, mediaType, null, inputStream);
170         checkNormalizedNodeContext(returnValue);
171         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
172     }
173
174     @Test
175     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
176         final DataSchemaNode dataSchemaNode =
177                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
178         final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
179         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
180         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
181         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
182         final AugmentationIdentifier augChoice1II = new AugmentationIdentifier(Set.of(augmentChoice1QName));
183         final AugmentationIdentifier augChoice2II = new AugmentationIdentifier(Set.of(augmentChoice2QName));
184         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
185                 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
186                 .node(containerQName);
187         final String uri = "instance-identifier-module:cont";
188         mockBodyReader(uri, xmlBodyReader, true);
189         final InputStream inputStream = TestXmlBodyReader.class
190                 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
191         final NormalizedNodeContext returnValue = xmlBodyReader
192                 .readFrom(null, null, null, mediaType, null, inputStream);
193         checkNormalizedNodeContext(returnValue);
194         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
195     }
196
197     @Test
198     public void rpcModuleInputTest() throws Exception {
199         final String uri = "invoke-rpc-module:rpc-test";
200         mockBodyReader(uri, xmlBodyReader, true);
201         final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
202         final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null, null, null, mediaType, null,
203             inputStream);
204         checkNormalizedNodeContextRpc(returnValue);
205         final ContainerNode contNode = (ContainerNode) returnValue.getData();
206         final Optional<DataContainerChild> contDataNodePotential = contNode.findChildByArg(new NodeIdentifier(
207             QName.create(contNode.getIdentifier().getNodeType(), "cont")));
208         assertTrue(contDataNodePotential.isPresent());
209         final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
210         final Optional<DataContainerChild> leafDataNode = contDataNode.findChildByArg(new NodeIdentifier(
211             QName.create(contDataNode.getIdentifier().getNodeType(), "lf")));
212         assertTrue(leafDataNode.isPresent());
213         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().body().toString()));
214     }
215
216     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
217             final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
218         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
219         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
220         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
221     }
222
223     /**
224      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
225      * used to distinguish between them to find correct one. Check if container was found not only according to its name
226      * but also by correct namespace used in payload.
227      */
228     @Test
229     public void findFooContainerUsingNamespaceTest() throws Exception {
230         mockBodyReader("", xmlBodyReader, true);
231         final InputStream inputStream = TestXmlBodyReader.class
232                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
233         final NormalizedNodeContext returnValue = xmlBodyReader
234                 .readFrom(null, null, null, mediaType, null, inputStream);
235
236         // check return value
237         checkNormalizedNodeContext(returnValue);
238         // check if container was found both according to its name and namespace
239         assertEquals("Not correct container found, name was ignored",
240                 "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
241         assertEquals("Not correct container found, namespace was ignored",
242                 "foo:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
243     }
244
245     /**
246      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
247      * used to distinguish between them to find correct one. Check if container was found not only according to its name
248      * but also by correct namespace used in payload.
249      */
250     @Test
251     public void findBarContainerUsingNamespaceTest() throws Exception {
252         mockBodyReader("", xmlBodyReader, true);
253         final InputStream inputStream = TestXmlBodyReader.class
254                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
255         final NormalizedNodeContext returnValue = xmlBodyReader
256                 .readFrom(null, null, null, mediaType, null, inputStream);
257
258         // check return value
259         checkNormalizedNodeContext(returnValue);
260         // check if container was found both according to its name and namespace
261         assertEquals("Not correct container found, name was ignored",
262                 "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
263         assertEquals("Not correct container found, namespace was ignored",
264                 "bar:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
265     }
266
267     /**
268      * Test PUT operation when message root element is not the same as the last element in request URI.
269      * PUT operation message should always start with schema node from URI otherwise exception should be
270      * thrown.
271      */
272     @Test
273     public void wrongRootElementTest() throws Exception {
274         mockBodyReader("instance-identifier-module:cont", xmlBodyReader, false);
275         final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
276                 "/instanceidentifier/xml/bug7933.xml");
277         try {
278             xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
279             Assert.fail("Test should fail due to malformed PUT operation message");
280         } catch (final RestconfDocumentedException exception) {
281             final RestconfError restconfError = exception.getErrors().get(0);
282             assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
283             assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
284         }
285     }
286 }