4cd307d01e5ba797c34814143c19f8a04a794a15
[yangtools.git] / codec / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / YT1543Test.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.xml;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11
12 import java.io.StringWriter;
13 import javax.xml.XMLConstants;
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.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
20 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24
25 class YT1543Test {
26     private static final YangInstanceIdentifier IID = YangInstanceIdentifier.builder()
27         .node(QName.create("barns", "bar"))
28         .nodeWithKey(QName.create("barns", "bar"), QName.create("barns", "key"),
29             YangInstanceIdentifier.of(QName.create("bazns", "baz")))
30         .build();
31
32     private static EffectiveModelContext MODEL_CONTEXT;
33
34     @BeforeAll
35     static void beforeAll() {
36         MODEL_CONTEXT = YangParserTestUtils.parseYang("""
37             module foo {
38               namespace foons;
39               prefix fo;
40
41               container foo {
42                 leaf leaf {
43                   type instance-identifier;
44                 }
45               }
46             }""", """
47             module bar {
48               namespace barns;
49               prefix br;
50
51               list bar {
52                 key key;
53                 leaf key {
54                   type instance-identifier;
55                 }
56               }
57             }""", """
58             module baz {
59               namespace bazns;
60               prefix bz;
61
62               container baz;
63             }""");
64     }
65
66     @Test
67     void nestedInstanceIdentifierInDocument() throws Exception {
68         final var stringWriter = new StringWriter();
69         try (var xmlWriter = XMLStreamNormalizedNodeStreamWriter.create(
70                 TestFactories.DEFAULT_OUTPUT_FACTORY.createXMLStreamWriter(stringWriter), MODEL_CONTEXT)) {
71             try (var nnWriter = NormalizedNodeWriter.forStreamWriter(xmlWriter)) {
72                 // Contrived: we have a document for foo's 'foo' container, with 'leaf' pointing to an instance of bar's
73                 //            'bar' list item, whose key points to baz's 'baz' container.
74                 nnWriter.write(Builders.containerBuilder()
75                     .withNodeIdentifier(new NodeIdentifier(QName.create("foons", "foo")))
76                     .withChild(ImmutableNodes.leafNode(QName.create("foons", "leaf"), IID))
77                     .build());
78             }
79         }
80
81         assertEquals("""
82             <foo xmlns="foons"><leaf xmlns:br="barns" xmlns:bz="bazns">/br:bar[br:key='/bz:baz']</leaf></foo>""",
83             stringWriter.toString());
84     }
85
86     @Test
87     void nestedInstanceIdentifierThroughCodec() throws Exception {
88         final var stringWriter = new StringWriter();
89         final var xmlWriter = TestFactories.DEFAULT_OUTPUT_FACTORY.createXMLStreamWriter(stringWriter);
90
91         xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, "foo", "foons");
92         xmlWriter.writeDefaultNamespace("foons");
93         xmlWriter.writeStartElement("leaf");
94         XmlCodecFactory.create(MODEL_CONTEXT).instanceIdentifierCodec().writeValue(xmlWriter, IID);
95         xmlWriter.writeEndElement();
96         xmlWriter.writeEndElement();
97         xmlWriter.close();
98
99         assertEquals("""
100             <foo xmlns="foons"><leaf xmlns:br="barns" xmlns:bz="bazns">/br:bar[br:key='/bz:baz']</leaf></foo>""",
101             stringWriter.toString());
102     }
103 }