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.NodeWithValue;
21 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
22 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25 class JsonChildBodyTest extends AbstractBodyTest {
26 private static DataPostPath CONT_PATH;
29 static void beforeAll() throws Exception {
30 final var testFiles = loadFiles("/instanceidentifier/yang");
31 testFiles.addAll(loadFiles("/modules"));
32 final var modelContext = YangParserTestUtils.parseYangFiles(testFiles);
34 CONT_PATH = new DataPostPath(DatabindContext.ofModel(modelContext),
35 Inference.ofDataTreePath(modelContext, CONT_QNAME), YangInstanceIdentifier.of(CONT_QNAME));
39 void moduleSubContainerDataPostTest() {
40 final var body = new JsonChildBody(stringInputStream("""
42 "instance-identifier-module:cont1": {
43 "augment-module-leaf-list:lf11" : "/instance-identifier-module:cont\
44 /instance-identifier-module:cont1/augment-module-leaf-list:lflst11[.=\\"lflst11_1\\"]"
47 final var payload = body.toPayload(CONT_PATH);
49 final var lflst11 = QName.create("augment:module:leaf:list", "2014-01-27", "lflst11");
50 assertEquals(List.of(new NodeIdentifier(CONT1_QNAME)), payload.prefix());
51 assertEquals(ImmutableNodes.newContainerBuilder()
52 .withNodeIdentifier(new NodeIdentifier(CONT1_QNAME))
53 .withChild(ImmutableNodes.leafNode(QName.create("augment:module:leaf:list", "2014-01-27", "lf11"),
54 YangInstanceIdentifier.of(
55 new NodeIdentifier(CONT_QNAME),
56 new NodeIdentifier(CONT1_QNAME),
57 new NodeIdentifier(lflst11),
58 new NodeWithValue<>(lflst11, "lflst11_1"))))
59 .build(), payload.body());
63 void moduleSubContainerAugmentDataPostTest() {
64 final var body = new JsonChildBody(
65 JsonChildBodyTest.class.getResourceAsStream("/instanceidentifier/json/json_augment_container.json"));
66 final var payload = body.toPayload(CONT_PATH);
68 final var contAugment = QName.create("augment:module", "2014-01-17", "cont-augment");
69 assertEquals(List.of(new NodeIdentifier(contAugment)), payload.prefix());
70 assertEquals(ImmutableNodes.newContainerBuilder()
71 .withNodeIdentifier(new NodeIdentifier(contAugment))
72 .withChild(ImmutableNodes.leafNode(QName.create(contAugment, "leaf1"), "stryng"))
73 .build(), payload.body());
77 void moduleSubContainerChoiceAugmentDataPostTest() {
78 final var body = new JsonChildBody(
79 JsonChildBodyTest.class.getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json"));
80 final var payload = body.toPayload(CONT_PATH);
82 final var container1 = QName.create("augment:module", "2014-01-17", "case-choice-case-container1");
84 new NodeIdentifier(QName.create(container1, "augment-choice1")),
85 new NodeIdentifier(QName.create(container1, "augment-choice2")),
86 new NodeIdentifier(container1)), payload.prefix());
87 assertEquals(ImmutableNodes.newContainerBuilder()
88 .withNodeIdentifier(new NodeIdentifier(container1))
89 .withChild(ImmutableNodes.leafNode(QName.create(container1, "case-choice-case-leaf1"), "stryng"))
90 .build(), payload.body());