Further warnings mitigation
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / databind / XmlChildBodyTest.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.databind;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11
12 import java.util.List;
13 import org.junit.jupiter.api.BeforeAll;
14 import org.junit.jupiter.api.Test;
15 import org.opendaylight.restconf.server.api.DataPostPath;
16 import org.opendaylight.restconf.server.api.DatabindContext;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
22 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25
26 class XmlChildBodyTest extends AbstractBodyTest {
27     private static final QName TOP_LEVEL_LIST = QName.create("foo", "2017-08-09", "top-level-list");
28
29     private static DataPostPath EMPTY_PATH;
30     private static DataPostPath CONT_PATH;
31
32     @BeforeAll
33     static void beforeAll() throws Exception {
34         final var testFiles = loadFiles("/instanceidentifier/yang");
35         testFiles.addAll(loadFiles("/modules"));
36         testFiles.addAll(loadFiles("/foo-xml-test/yang"));
37         final var modelContext = YangParserTestUtils.parseYangFiles(testFiles);
38
39         CONT_PATH = new DataPostPath(DatabindContext.ofModel(modelContext),
40             Inference.ofDataTreePath(modelContext, CONT_QNAME), YangInstanceIdentifier.of(CONT_QNAME));
41         EMPTY_PATH = new DataPostPath(DatabindContext.ofModel(modelContext),
42             Inference.ofDataTreePath(modelContext), YangInstanceIdentifier.of());
43     }
44
45     @Test
46     void postXmlTest() {
47         final var body = new XmlChildBody(stringInputStream("""
48             <top-level-list xmlns="foo">
49                 <key-leaf>key-value</key-leaf>
50                 <ordinary-leaf>leaf-value</ordinary-leaf>
51             </top-level-list>"""));
52         final var payload = body.toPayload(EMPTY_PATH);
53
54         final var entryId = NodeIdentifierWithPredicates.of(TOP_LEVEL_LIST,
55             QName.create(TOP_LEVEL_LIST, "key-leaf"), "key-value");
56         assertEquals(List.of(new NodeIdentifier(TOP_LEVEL_LIST), entryId), payload.prefix());
57         assertEquals(ImmutableNodes.newMapEntryBuilder()
58             .withNodeIdentifier(entryId)
59             .withChild(ImmutableNodes.leafNode(QName.create(TOP_LEVEL_LIST, "key-leaf"), "key-value"))
60             .withChild(ImmutableNodes.leafNode(QName.create(TOP_LEVEL_LIST, "ordinary-leaf"), "leaf-value"))
61             .build(), payload.body());
62     }
63
64     @Test
65     void moduleSubContainerDataPostTest() {
66         final var body = new XmlChildBody(stringInputStream("""
67             <cont1 xmlns="instance:identifier:module">
68               <lflst11 xmlns="augment:module:leaf:list">lflst11_1</lflst11>
69               <lflst11 xmlns="augment:module:leaf:list">lflst11_2</lflst11>
70               <lflst11 xmlns="augment:module:leaf:list">lflst11_3</lflst11>
71               <lf11 xmlns:a="instance:identifier:module" xmlns:b="augment:module:leaf:list" \
72             xmlns="augment:module:leaf:list">/a:cont/a:cont1/b:lflst11[.="lflst11_1"]</lf11>
73             </cont1>"""));
74         final var payload = body.toPayload(CONT_PATH);
75
76         final var lflst11 = QName.create("augment:module:leaf:list", "2014-01-27", "lflst11");
77         assertEquals(List.of(new NodeIdentifier(CONT1_QNAME)), payload.prefix());
78         assertEquals(ImmutableNodes.newContainerBuilder()
79             .withNodeIdentifier(new NodeIdentifier(CONT1_QNAME))
80             .withChild(ImmutableNodes.newSystemLeafSetBuilder()
81                 .withNodeIdentifier(new NodeIdentifier(lflst11))
82                 .withChildValue("lflst11_1")
83                 .withChildValue("lflst11_2")
84                 .withChildValue("lflst11_3")
85                 .build())
86             .withChild(ImmutableNodes.leafNode(QName.create(lflst11, "lf11"), YangInstanceIdentifier.of(
87                 new NodeIdentifier(CONT_QNAME),
88                 new NodeIdentifier(CONT1_QNAME),
89                 new NodeIdentifier(lflst11),
90                 new NodeWithValue<>(lflst11, "lflst11_1"))))
91             .build(), payload.body());
92     }
93
94     @Test
95     void moduleSubContainerAugmentDataPostTest() {
96         final var body = new XmlChildBody(
97             XmlChildBodyTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml"));
98         final var payload = body.toPayload(CONT_PATH);
99
100         final var contAugment = QName.create("augment:module", "2014-01-17", "cont-augment");
101         assertEquals(List.of(new NodeIdentifier(contAugment)), payload.prefix());
102         assertEquals(ImmutableNodes.newContainerBuilder()
103             .withNodeIdentifier(new NodeIdentifier(contAugment))
104             .withChild(ImmutableNodes.leafNode(QName.create(contAugment, "leaf1"), "stryng"))
105             .build(), payload.body());
106     }
107
108     @Test
109     void moduleSubContainerChoiceAugmentDataPostTest() {
110         final var body = new XmlChildBody(
111             XmlChildBodyTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml"));
112         final var payload = body.toPayload(CONT_PATH);
113
114         final var container1 = QName.create("augment:module", "2014-01-17", "case-choice-case-container1");
115         assertEquals(List.of(
116             new NodeIdentifier(QName.create(container1, "augment-choice1")),
117             new NodeIdentifier(QName.create(container1, "augment-choice2")),
118             new NodeIdentifier(container1)), payload.prefix());
119         assertEquals(ImmutableNodes.newContainerBuilder()
120             .withNodeIdentifier(new NodeIdentifier(container1))
121             .withChild(ImmutableNodes.leafNode(QName.create(container1, "case-choice-case-leaf1"), "stryng"))
122             .build(), payload.body());
123     }
124
125     /**
126      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
127      * used to distinguish between them to find correct one. Check if container was found not only according to its
128      * name, but also by correct namespace used in payload.
129      */
130     @Test
131     void findFooContainerUsingNamespaceTest() {
132         final var body = new XmlChildBody(
133             XmlChildBodyTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml"));
134         final var payload = body.toPayload(EMPTY_PATH);
135
136         final var fooBarContainer = new NodeIdentifier(QName.create("foo:module", "2016-09-29", "foo-bar-container"));
137         assertEquals(List.of(fooBarContainer), payload.prefix());
138         assertEquals(ImmutableNodes.newContainerBuilder().withNodeIdentifier(fooBarContainer).build(),
139             payload.body());
140     }
141
142     /**
143      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
144      * used to distinguish between them to find correct one. Check if container was found not only according to its
145      * name, but also by correct namespace used in payload.
146      */
147     @Test
148     void findBarContainerUsingNamespaceTest() {
149         final var body = new XmlChildBody(
150             XmlChildBodyTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml"));
151         final var payload = body.toPayload(EMPTY_PATH);
152
153         final var fooBarContainer = new NodeIdentifier(QName.create("bar:module", "2016-09-29", "foo-bar-container"));
154         assertEquals(List.of(fooBarContainer), payload.prefix());
155         assertEquals(ImmutableNodes.newContainerBuilder().withNodeIdentifier(fooBarContainer).build(),
156             payload.body());
157     }
158 }