Migrate yang-data-codec-gson to JUnit5
[yangtools.git] / codec / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / Bug7246Test.java
1 /*
2  * Copyright (c) 2017 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.yangtools.yang.data.codec.gson;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11
12 import com.google.gson.JsonParser;
13 import java.io.File;
14 import java.io.FileReader;
15 import java.io.StringWriter;
16 import java.nio.charset.StandardCharsets;
17 import org.junit.jupiter.api.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.XMLNamespace;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
23 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28
29 class Bug7246Test {
30     private static final String NS = "my-namespace";
31
32     @Test
33     void test() throws Exception {
34         final var schemaContext = YangParserTestUtils.parseYang("""
35             module rpc-test {
36               namespace my-namespace;
37               prefix p;
38               feature my-name;
39               identity my-name;
40               extension my-name;
41
42               typedef my-name {
43                 type string;
44               }
45
46               grouping my-name {
47                 leaf my-name {
48                   type my-name;
49                 }
50               }
51
52               rpc my-name {
53                 input {
54                   container my-name {
55                     leaf my-name {
56                       type my-name;
57                     }
58                   }
59                 }
60                 output {
61                   container my-name {
62                     leaf my-name {
63                       type my-name;
64                     }
65                   }
66                 }
67               }
68             }""");
69         final var inputStructure = Builders.containerBuilder()
70             .withNodeIdentifier(new NodeIdentifier(qN("my-name")))
71             .withChild(ImmutableNodes.leafNode(new NodeIdentifier(qN("my-name")), "my-value"))
72             .build();
73         final var jsonOutput = normalizedNodeToJsonStreamTransformation(schemaContext, inputStructure,
74             qN("my-name"), qN("input"));
75
76         assertEquals(JsonParser.parseReader(new FileReader(
77             new File(getClass().getResource("/bug7246/json/expected-output.json").toURI()), StandardCharsets.UTF_8)),
78             JsonParser.parseString(jsonOutput));
79     }
80
81     private static QName qN(final String localName) {
82         return QName.create(NS, localName);
83     }
84
85     private static String normalizedNodeToJsonStreamTransformation(final EffectiveModelContext schemaContext,
86             final NormalizedNode inputStructure, final QName... path) throws Exception {
87         final var writer = new StringWriter();
88         final var jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
89                 JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), Absolute.of(path),
90                 XMLNamespace.of(NS), JsonWriterFactory.createJsonWriter(writer, 2));
91         try (var nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
92             nodeWriter.write(inputStructure);
93         }
94         return writer.toString();
95     }
96 }