2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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.yangtools.yang.data.codec.gson;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
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;
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");
35 private static final EffectiveModelContext MODEL_CONTEXT = YangParserTestUtils.parseYang("""
78 serialize(ImmutableNodes.newContainerBuilder()
79 .withNodeIdentifier(new NodeIdentifier(INPUT))
80 .withChild(ImmutableNodes.leafNode(UINT, Uint8.ONE))
85 void testRpcOutput() {
92 serialize(ImmutableNodes.newContainerBuilder()
93 .withNodeIdentifier(new NodeIdentifier(OUTPUT))
94 .withChild(ImmutableNodes.leafNode(UINT, Uint64.ONE))
99 void testActionInput() {
106 serialize(ImmutableNodes.newContainerBuilder()
107 .withNodeIdentifier(new NodeIdentifier(INPUT))
108 .withChild(ImmutableNodes.leafNode(UINT, Uint8.TWO))
109 .build(), BAR, BAZ));
113 void testActionOutput() {
120 serialize(ImmutableNodes.newContainerBuilder()
121 .withNodeIdentifier(new NodeIdentifier(OUTPUT))
122 .withChild(ImmutableNodes.leafNode(UINT, Uint64.TWO))
123 .build(), BAR, BAZ));
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);
137 return writer.toString();