Migrate restconf/restconf-nb tests to JUnit5
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / databind / AbstractOperationInputBodyTest.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12
13 import org.junit.jupiter.api.BeforeAll;
14 import org.junit.jupiter.api.Test;
15 import org.opendaylight.restconf.nb.rfc8040.AbstractInstanceIdentifierTest;
16 import org.opendaylight.restconf.server.api.DatabindContext;
17 import org.opendaylight.restconf.server.api.DatabindPath.Action;
18 import org.opendaylight.restconf.server.api.DatabindPath.Rpc;
19 import org.opendaylight.restconf.server.api.OperationInputBody;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.Uint32;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29
30 abstract class AbstractOperationInputBodyTest extends AbstractInstanceIdentifierTest {
31     private static final NodeIdentifier INPUT_NID = new NodeIdentifier(QName.create(CONT_QNAME, "input"));
32
33     private static Action RESET_PATH;
34
35     @BeforeAll
36     static final void setupInference() {
37         final var stack = SchemaInferenceStack.ofDataTreePath(IID_SCHEMA, CONT_QNAME, CONT1_QNAME);
38         final var action = assertInstanceOf(ActionEffectiveStatement.class, stack.enterSchemaTree(RESET_QNAME));
39
40         RESET_PATH = new Action(IID_DATABIND, stack.toInference(), YangInstanceIdentifier.of(CONT_QNAME, CONT1_QNAME),
41             action);
42     }
43
44     @Test
45     final void moduleSubContainerDataPostActionTest() throws Exception {
46         final var body = moduleSubContainerDataPostActionBody();
47
48         assertEquals(ImmutableNodes.newContainerBuilder()
49             .withNodeIdentifier(INPUT_NID)
50             .withChild(ImmutableNodes.leafNode(DELAY_QNAME, Uint32.valueOf(600)))
51             .build(), body.toContainerNode(RESET_PATH));
52     }
53
54     abstract OperationInputBody moduleSubContainerDataPostActionBody();
55
56     @Test
57     final void testEmpty() throws Exception {
58         final var body = testEmptyBody();
59         assertEquals(ImmutableNodes.newContainerBuilder().withNodeIdentifier(INPUT_NID).build(),
60             body.toContainerNode(RESET_PATH));
61     }
62
63     abstract OperationInputBody testEmptyBody();
64
65     @Test
66     final void testRpcModuleInput() throws Exception {
67         final var rpcTest = QName.create("invoke:rpc:module", "2013-12-03", "rpc-test");
68         final var modelContext = YangParserTestUtils.parseYangResourceDirectory("/invoke-rpc");
69         final var stack = SchemaInferenceStack.of(modelContext);
70         final var rpc = assertInstanceOf(RpcEffectiveStatement.class, stack.enterSchemaTree(rpcTest));
71
72         final var body = testRpcModuleInputBody();
73
74         assertEquals(ImmutableNodes.newContainerBuilder()
75             .withNodeIdentifier(new NodeIdentifier(QName.create(rpcTest, "input")))
76             .withChild(ImmutableNodes.newContainerBuilder()
77                 .withNodeIdentifier(new NodeIdentifier(QName.create(rpcTest, "cont")))
78                 .withChild(ImmutableNodes.leafNode(QName.create(rpcTest, "lf"), "lf-test"))
79                 .build())
80             .build(),
81             body.toContainerNode(new Rpc(DatabindContext.ofModel(modelContext), stack.toInference(), rpc)));
82     }
83
84     abstract OperationInputBody testRpcModuleInputBody();
85 }