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