Allow emission of operation input/output
[yangtools.git] / codec / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / YT1570Test.java
1 /*
2  * Copyright (c) 2024 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.yangtools.yang.data.codec.gson;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11
12 import java.io.IOException;
13 import java.io.StringWriter;
14 import org.junit.jupiter.api.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.Uint64;
17 import org.opendaylight.yangtools.yang.common.Uint8;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
21 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
24 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26
27 class YT1570Test {
28     private static final QName INPUT = QName.create("foo", "input");
29     private static final QName OUTPUT = QName.create("foo", "output");
30     private static final QName FOO = QName.create("foo", "foo");
31     private static final QName BAR = QName.create("foo", "bar");
32     private static final QName BAZ = QName.create("foo", "baz");
33     private static final QName UINT = QName.create("foo", "uint");
34
35     private static final EffectiveModelContext MODEL_CONTEXT = YangParserTestUtils.parseYang("""
36         module foo {
37           namespace foo;
38           prefix foo;
39           yang-version 1.1;
40
41           rpc foo {
42             input {
43               leaf uint {
44                 type uint8;
45               }
46             }
47             output {
48               leaf uint {
49                 type uint64;
50               }
51             }
52           }
53
54           container bar {
55             action baz {
56               input {
57                 leaf uint {
58                   type uint8;
59                 }
60               }
61               output {
62                 leaf uint {
63                   type uint64;
64                 }
65               }
66             }
67           }
68         }""");
69
70     @Test
71     void testRpcInput() {
72         assertEquals("""
73             {
74               "foo:input": {
75                 "uint": 1
76               }
77             }""",
78             serialize(ImmutableNodes.newContainerBuilder()
79                 .withNodeIdentifier(new NodeIdentifier(INPUT))
80                 .withChild(ImmutableNodes.leafNode(UINT, Uint8.ONE))
81                 .build(), FOO));
82     }
83
84     @Test
85     void testRpcOutput() {
86         assertEquals("""
87             {
88               "foo:output": {
89                 "uint": "1"
90               }
91             }""",
92             serialize(ImmutableNodes.newContainerBuilder()
93                 .withNodeIdentifier(new NodeIdentifier(OUTPUT))
94                 .withChild(ImmutableNodes.leafNode(UINT, Uint64.ONE))
95                 .build(), FOO));
96     }
97
98     @Test
99     void testActionInput() {
100         assertEquals("""
101             {
102               "foo:input": {
103                 "uint": 2
104               }
105             }""",
106             serialize(ImmutableNodes.newContainerBuilder()
107                 .withNodeIdentifier(new NodeIdentifier(INPUT))
108                 .withChild(ImmutableNodes.leafNode(UINT, Uint8.TWO))
109                 .build(), BAR, BAZ));
110     }
111
112     @Test
113     void testActionOutput() {
114         assertEquals("""
115             {
116               "foo:output": {
117                 "uint": "2"
118               }
119             }""",
120             serialize(ImmutableNodes.newContainerBuilder()
121                 .withNodeIdentifier(new NodeIdentifier(OUTPUT))
122                 .withChild(ImmutableNodes.leafNode(UINT, Uint64.TWO))
123                 .build(), BAR, BAZ));
124     }
125
126     private static String serialize(final ContainerNode container, final QName... nodeIdentifiers) {
127         final var writer = new StringWriter();
128         final var jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
129             JSONCodecFactorySupplier.RFC7951.getShared(MODEL_CONTEXT),
130             SchemaInferenceStack.of(MODEL_CONTEXT, Absolute.of(nodeIdentifiers)).toInference(), null,
131             JsonWriterFactory.createJsonWriter(writer, 2));
132         try (var nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
133             nodeWriter.write(container);
134         } catch (IOException e) {
135             throw new AssertionError(e);
136         }
137         return writer.toString();
138     }
139 }