Migrate restconf/restconf-nb tests to JUnit5
[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.DatabindContext;
16 import org.opendaylight.restconf.server.api.DatabindPath;
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.test.util.YangParserTestUtils;
24
25 class JsonChildBodyTest extends AbstractBodyTest {
26     private static DatabindPath.Data CONT_PATH;
27
28     @BeforeAll
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);
33
34         final var contPath = YangInstanceIdentifier.of(CONT_QNAME);
35         final var databind = DatabindContext.ofModel(modelContext);
36         final var nodeAndStack = databind.schemaTree().enterPath(contPath).orElseThrow();
37         CONT_PATH = new DatabindPath.Data(databind, nodeAndStack.stack().toInference(), contPath, nodeAndStack.node());
38     }
39
40     @Test
41     void moduleSubContainerDataPostTest() {
42         final var body = new JsonChildBody(stringInputStream("""
43             {
44               "instance-identifier-module:cont1": {
45                 "augment-module-leaf-list:lf11" : "/instance-identifier-module:cont\
46             /instance-identifier-module:cont1/augment-module-leaf-list:lflst11[.=\\"lflst11_1\\"]"
47               }
48             }"""));
49         final var payload = body.toPayload(CONT_PATH);
50
51         final var lflst11 = QName.create("augment:module:leaf:list", "2014-01-27", "lflst11");
52         assertEquals(List.of(new NodeIdentifier(CONT1_QNAME)), payload.prefix());
53         assertEquals(ImmutableNodes.newContainerBuilder()
54             .withNodeIdentifier(new NodeIdentifier(CONT1_QNAME))
55             .withChild(ImmutableNodes.leafNode(QName.create("augment:module:leaf:list", "2014-01-27", "lf11"),
56                 YangInstanceIdentifier.of(
57                     new NodeIdentifier(CONT_QNAME),
58                     new NodeIdentifier(CONT1_QNAME),
59                     new NodeIdentifier(lflst11),
60                     new NodeWithValue<>(lflst11, "lflst11_1"))))
61             .build(), payload.body());
62     }
63
64     @Test
65     void moduleSubContainerAugmentDataPostTest() {
66         final var body = new JsonChildBody(
67             JsonChildBodyTest.class.getResourceAsStream("/instanceidentifier/json/json_augment_container.json"));
68         final var payload = body.toPayload(CONT_PATH);
69
70         final var contAugment = QName.create("augment:module", "2014-01-17", "cont-augment");
71         assertEquals(List.of(new NodeIdentifier(contAugment)), payload.prefix());
72         assertEquals(ImmutableNodes.newContainerBuilder()
73             .withNodeIdentifier(new NodeIdentifier(contAugment))
74             .withChild(ImmutableNodes.leafNode(QName.create(contAugment, "leaf1"), "stryng"))
75             .build(), payload.body());
76     }
77
78     @Test
79     void moduleSubContainerChoiceAugmentDataPostTest() {
80         final var body = new JsonChildBody(
81             JsonChildBodyTest.class.getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json"));
82         final var payload = body.toPayload(CONT_PATH);
83
84         final var container1 = QName.create("augment:module", "2014-01-17", "case-choice-case-container1");
85         assertEquals(List.of(
86             new NodeIdentifier(QName.create(container1, "augment-choice1")),
87             new NodeIdentifier(QName.create(container1, "augment-choice2")),
88             new NodeIdentifier(container1)), payload.prefix());
89         assertEquals(ImmutableNodes.newContainerBuilder()
90             .withNodeIdentifier(new NodeIdentifier(container1))
91             .withChild(ImmutableNodes.leafNode(QName.create(container1, "case-choice-case-leaf1"), "stryng"))
92             .build(), payload.body());
93     }
94 }