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