Migrate users of deprecated QNameModule methods
[yangtools.git] / codec / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / YT1472Test.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.yangtools.yang.data.codec.gson;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11
12 import com.google.gson.stream.JsonReader;
13 import java.io.StringReader;
14 import org.junit.jupiter.api.BeforeAll;
15 import org.junit.jupiter.api.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.YangDataName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizationResultHolder;
22 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
25 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27
28 class YT1472Test {
29     private static final QNameModule RESTCONF_MODULE =
30         QNameModule.of("urn:ietf:params:xml:ns:yang:ietf-restconf", "2017-01-26");
31     private static final YangDataName ERRORS_NAME = new YangDataName(RESTCONF_MODULE, "yang-errors");
32     private static final NodeIdentifier ERROR_NID = NodeIdentifier.create(QName.create(RESTCONF_MODULE, "error"));
33
34     private static EffectiveModelContext CONTEXT;
35     private static JSONCodecFactory CODEC_FACTORY;
36     private static Inference ERRORS_INFERENCE;
37
38     @BeforeAll
39     static void beforeClass() {
40         CONTEXT = YangParserTestUtils.parseYangResourceDirectory("/yt1472");
41         CODEC_FACTORY = JSONCodecFactorySupplier.RFC7951.getShared(CONTEXT);
42
43         final var stack = SchemaInferenceStack.of(CONTEXT);
44         stack.enterYangData(ERRORS_NAME);
45         ERRORS_INFERENCE = stack.toInference();
46     }
47
48     @Test
49     void testErrorsParsing() {
50         final var result = new NormalizationResultHolder();
51         final var streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
52         final var jsonParser = JsonParserStream.create(streamWriter, CODEC_FACTORY, ERRORS_INFERENCE);
53         // https://www.rfc-editor.org/rfc/rfc8040#page-77
54         jsonParser.parse(new JsonReader(new StringReader("""
55             {
56               "ietf-restconf:errors" : {
57                 "error" : [
58                   {
59                     "error-type" : "protocol",
60                     "error-tag" : "lock-denied",
61                     "error-message" : "Lock failed; lock already held"
62                   }
63                 ]
64               }
65             }""")));
66         assertEquals(ImmutableNodes.newContainerBuilder()
67             .withNodeIdentifier(NodeIdentifier.create(QName.create(RESTCONF_MODULE, "errors")))
68             .withChild(ImmutableNodes.newUnkeyedListBuilder()
69                 .withNodeIdentifier(ERROR_NID)
70                 .withChild(ImmutableNodes.newUnkeyedListEntryBuilder()
71                     .withNodeIdentifier(ERROR_NID)
72                     .withChild(ImmutableNodes.leafNode(QName.create(RESTCONF_MODULE, "error-type"), "protocol"))
73                     .withChild(ImmutableNodes.leafNode(QName.create(RESTCONF_MODULE, "error-tag"), "lock-denied"))
74                     .withChild(ImmutableNodes.leafNode(QName.create(RESTCONF_MODULE, "error-message"),
75                         "Lock failed; lock already held"))
76                     .build())
77                 .build())
78             .build(), result.getResult().data());
79     }
80 }