Move AbstractBody et al.
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / databind / JsonChildBodyTest.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.restconf.server.api.JsonChildBody;
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.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 JsonChildBodyTest extends AbstractBodyTest {
27     private static DataPostPath CONT_PATH;
28
29     @BeforeAll
30     static void beforeAll() throws Exception {
31         final var testFiles = loadFiles("/instanceidentifier/yang");
32         testFiles.addAll(loadFiles("/modules"));
33         final var modelContext = YangParserTestUtils.parseYangFiles(testFiles);
34
35         CONT_PATH = new DataPostPath(DatabindContext.ofModel(modelContext),
36             Inference.ofDataTreePath(modelContext, CONT_QNAME), YangInstanceIdentifier.of(CONT_QNAME));
37     }
38
39     @Test
40     void moduleSubContainerDataPostTest() {
41         final var body = new JsonChildBody(stringInputStream("""
42             {
43               "instance-identifier-module:cont1": {
44                 "augment-module-leaf-list:lf11" : "/instance-identifier-module:cont\
45             /instance-identifier-module:cont1/augment-module-leaf-list:lflst11[.=\\"lflst11_1\\"]"
46               }
47             }"""));
48         final var payload = body.toPayload(CONT_PATH);
49
50         final var lflst11 = QName.create("augment:module:leaf:list", "2014-01-27", "lflst11");
51         assertEquals(List.of(new NodeIdentifier(CONT1_QNAME)), payload.prefix());
52         assertEquals(ImmutableNodes.newContainerBuilder()
53             .withNodeIdentifier(new NodeIdentifier(CONT1_QNAME))
54             .withChild(ImmutableNodes.leafNode(QName.create("augment:module:leaf:list", "2014-01-27", "lf11"),
55                 YangInstanceIdentifier.of(
56                     new NodeIdentifier(CONT_QNAME),
57                     new NodeIdentifier(CONT1_QNAME),
58                     new NodeIdentifier(lflst11),
59                     new NodeWithValue<>(lflst11, "lflst11_1"))))
60             .build(), payload.body());
61     }
62
63     @Test
64     void moduleSubContainerAugmentDataPostTest() {
65         final var body = new JsonChildBody(
66             JsonChildBodyTest.class.getResourceAsStream("/instanceidentifier/json/json_augment_container.json"));
67         final var payload = body.toPayload(CONT_PATH);
68
69         final var contAugment = QName.create("augment:module", "2014-01-17", "cont-augment");
70         assertEquals(List.of(new NodeIdentifier(contAugment)), payload.prefix());
71         assertEquals(ImmutableNodes.newContainerBuilder()
72             .withNodeIdentifier(new NodeIdentifier(contAugment))
73             .withChild(ImmutableNodes.leafNode(QName.create(contAugment, "leaf1"), "stryng"))
74             .build(), payload.body());
75     }
76
77     @Test
78     void moduleSubContainerChoiceAugmentDataPostTest() {
79         final var body = new JsonChildBody(
80             JsonChildBodyTest.class.getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json"));
81         final var payload = body.toPayload(CONT_PATH);
82
83         final var container1 = QName.create("augment:module", "2014-01-17", "case-choice-case-container1");
84         assertEquals(List.of(
85             new NodeIdentifier(QName.create(container1, "augment-choice1")),
86             new NodeIdentifier(QName.create(container1, "augment-choice2")),
87             new NodeIdentifier(container1)), payload.prefix());
88         assertEquals(ImmutableNodes.newContainerBuilder()
89             .withNodeIdentifier(new NodeIdentifier(container1))
90             .withChild(ImmutableNodes.leafNode(QName.create(container1, "case-choice-case-leaf1"), "stryng"))
91             .build(), payload.body());
92     }
93 }