Deprecate NormalizedNodeContext
[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 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.test;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThrows;
15 import static org.junit.Assert.fail;
16
17 import com.google.common.collect.Sets;
18 import java.io.ByteArrayInputStream;
19 import java.io.File;
20 import java.io.InputStream;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Collection;
23 import java.util.Optional;
24 import javax.ws.rs.core.MediaType;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
28 import org.opendaylight.restconf.common.errors.RestconfError;
29 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
30 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.XmlNormalizedNodeBodyReader;
31 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
32 import org.opendaylight.yangtools.yang.common.ErrorTag;
33 import org.opendaylight.yangtools.yang.common.ErrorType;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.common.QNameModule;
36 import org.opendaylight.yangtools.yang.common.Revision;
37 import org.opendaylight.yangtools.yang.common.XMLNamespace;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.ActionDefinition;
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.EffectiveModelContext;
46 import org.opendaylight.yangtools.yang.model.api.Module;
47 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
48
49 public class XmlBodyReaderTest extends AbstractBodyReaderTest {
50     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
51         XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
52
53     private static EffectiveModelContext schemaContext;
54
55     private final XmlNormalizedNodeBodyReader xmlBodyReader;
56
57     public XmlBodyReaderTest() throws Exception {
58         super(schemaContext);
59         xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler, mountPointService);
60     }
61
62     @Override
63     protected MediaType getMediaType() {
64         return new MediaType(MediaType.APPLICATION_XML, null);
65     }
66
67     @BeforeClass
68     public static void initialization() throws Exception {
69         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
70         testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
71         testFiles.addAll(TestRestconfUtils.loadFiles("/foo-xml-test/yang"));
72         schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
73     }
74
75     @Test
76     public void putXmlTest() throws Exception {
77         runXmlTest(false, "foo:top-level-list=key-value");
78     }
79
80     @Test
81     public void postXmlTest() throws Exception {
82         runXmlTest(true, "");
83     }
84
85     private void runXmlTest(final boolean isPost, final String path) throws Exception {
86         mockBodyReader(path, xmlBodyReader, isPost);
87         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
88             XmlBodyReaderTest.class.getResourceAsStream("/foo-xml-test/foo.xml"));
89         assertNotNull(payload);
90
91         assertThat(payload.getData(), instanceOf(MapEntryNode.class));
92         final MapEntryNode data = (MapEntryNode) payload.getData();
93         assertEquals(2, data.size());
94         for (final DataContainerChild child : data.body()) {
95             switch (child.getIdentifier().getNodeType().getLocalName()) {
96                 case "key-leaf":
97                     assertEquals("key-value", child.body());
98                     break;
99                 case "ordinary-leaf":
100                     assertEquals("leaf-value", child.body());
101                     break;
102                 default:
103                     fail();
104             }
105         }
106     }
107
108     @Test
109     public void moduleDataTest() throws Exception {
110         final DataSchemaNode dataSchemaNode = schemaContext
111                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
112         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
113         final String uri = "instance-identifier-module:cont";
114         mockBodyReader(uri, xmlBodyReader, false);
115         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
116             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmldata.xml"));
117         checkNormalizedNodePayload(payload);
118         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
119     }
120
121     @Test
122     public void moduleSubContainerDataPutTest() throws Exception {
123         final DataSchemaNode dataSchemaNode = schemaContext
124                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
125         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
126         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
127         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
128         final String uri = "instance-identifier-module:cont/cont1";
129         mockBodyReader(uri, xmlBodyReader, false);
130         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
131             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
132         checkNormalizedNodePayload(payload);
133         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, payload, dataII);
134     }
135
136     @Test
137     public void moduleSubContainerDataPostTest() throws Exception {
138         final DataSchemaNode dataSchemaNode = schemaContext
139                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
140         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
141         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
142         final String uri = "instance-identifier-module:cont";
143         mockBodyReader(uri, xmlBodyReader, true);
144         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
145             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
146         checkNormalizedNodePayload(payload);
147         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
148     }
149
150     @Test
151     public void moduleSubContainerDataPostActionTest() throws Exception {
152         final Optional<DataSchemaNode> dataSchemaNode = schemaContext
153             .findDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
154         final QName cont1QName = QName.create(dataSchemaNode.get().getQName(), "cont1");
155         final QName actionQName = QName.create(dataSchemaNode.get().getQName(), "reset");
156         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.get().getQName())
157             .node(cont1QName).node(actionQName);
158         final String uri = "instance-identifier-module:cont/cont1/reset";
159         mockBodyReader(uri, xmlBodyReader, true);
160         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
161             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_cont_action.xml"));
162         checkNormalizedNodePayload(payload);
163         assertThat(payload.getInstanceIdentifierContext().getSchemaNode(), instanceOf(ActionDefinition.class));
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.findModules(XMLNamespace.of("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, xmlBodyReader, true);
178         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
179             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml"));
180         checkNormalizedNodePayload(payload);
181         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
182     }
183
184     @Test
185     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
186         final DataSchemaNode dataSchemaNode = schemaContext
187                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
188         final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
189         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
190         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
191         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
192         final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
193                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
194         final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
195                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
196         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
197                 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
198         final String uri = "instance-identifier-module:cont";
199         mockBodyReader(uri, xmlBodyReader, true);
200         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
201             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml"));
202         checkNormalizedNodePayload(payload);
203         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
204     }
205
206     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
207             final NormalizedNodePayload nnContext, final YangInstanceIdentifier dataNodeIdent) {
208         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
209         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
210         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
211     }
212
213     /**
214      * Test when container with the same name is placed in two modules
215      * (foo-module and bar-module). Namespace must be used to distinguish
216      * between them to find correct one. Check if container was found not only
217      * according to its name but also by correct namespace used in payload.
218      */
219     @Test
220     public void findFooContainerUsingNamespaceTest() throws Exception {
221         mockBodyReader("", xmlBodyReader, true);
222         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
223             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml"));
224
225         // check return value
226         checkNormalizedNodePayload(payload);
227         // check if container was found both according to its name and namespace
228         final var payloadNodeType = payload.getData().getIdentifier().getNodeType();
229         assertEquals("foo-bar-container", payloadNodeType.getLocalName());
230         assertEquals("foo:module", payloadNodeType.getNamespace().toString());
231     }
232
233     /**
234      * Test when container with the same name is placed in two modules
235      * (foo-module and bar-module). Namespace must be used to distinguish
236      * between them to find correct one. Check if container was found not only
237      * according to its name but also by correct namespace used in payload.
238      */
239     @Test
240     public void findBarContainerUsingNamespaceTest() throws Exception {
241         mockBodyReader("", xmlBodyReader, true);
242         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null,
243             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml"));
244
245         // check return value
246         checkNormalizedNodePayload(payload);
247         // check if container was found both according to its name and namespace
248         final var payloadNodeType = payload.getData().getIdentifier().getNodeType();
249         assertEquals("foo-bar-container", payloadNodeType.getLocalName());
250         assertEquals("bar:module", payloadNodeType.getNamespace().toString());
251     }
252
253     /**
254      * Test PUT operation when message root element is not the same as the last element in request URI.
255      * PUT operation message should always start with schema node from URI otherwise exception should be
256      * thrown.
257      */
258     @Test
259     public void wrongRootElementTest() throws Exception {
260         mockBodyReader("instance-identifier-module:cont", xmlBodyReader, false);
261         final InputStream inputStream =
262                 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/bug7933.xml");
263
264         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
265             () -> xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
266
267         final RestconfError restconfError = ex.getErrors().get(0);
268         assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
269         assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
270     }
271
272     @Test
273     public void testRangeViolation() throws Exception {
274         mockBodyReader("netconf786:foo", xmlBodyReader, false);
275
276         final InputStream inputStream = new ByteArrayInputStream(
277             "<foo xmlns=\"netconf786\"><bar>100</bar></foo>".getBytes(StandardCharsets.UTF_8));
278
279         assertRangeViolation(() -> xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
280     }
281 }