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.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;
26 class XmlChildBodyTest extends AbstractBodyTest {
27 private static final QName TOP_LEVEL_LIST = QName.create("foo", "2017-08-09", "top-level-list");
29 private static DataPostPath EMPTY_PATH;
30 private static DataPostPath CONT_PATH;
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);
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());
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);
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());
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>
74 final var payload = body.toPayload(CONT_PATH);
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")
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());
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);
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());
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);
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());
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.
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);
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(),
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.
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);
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(),