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