Tests for YangInstanceIdentifier key value serialization
[yangtools.git] / codec / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / YT1473Test.java
1 /*
2  * Copyright (c) 2022 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.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.verify;
13
14 import javax.xml.stream.XMLStreamWriter;
15 import org.junit.jupiter.api.AfterAll;
16 import org.junit.jupiter.api.BeforeAll;
17 import org.junit.jupiter.api.Disabled;
18 import org.junit.jupiter.api.Test;
19 import org.junit.jupiter.api.extension.ExtendWith;
20 import org.mockito.ArgumentCaptor;
21 import org.mockito.Captor;
22 import org.mockito.Mock;
23 import org.mockito.junit.jupiter.MockitoExtension;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
29 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33
34 @ExtendWith(MockitoExtension.class)
35 public class YT1473Test {
36     public static final String FOO_NS = "foons"; // namespace for prefix 'foo'
37     public static final QName FOO_FOO = QName.create(FOO_NS, "foo"); // list with key 'str'
38     public static final QName FOO_BAR = QName.create(FOO_NS, "bar"); // list with key 'qname'
39     public static final QName FOO_BAZ = QName.create(FOO_NS, "baz"); // list with key 'id'
40     public static final QName FOO_ONE = QName.create(FOO_NS, "one"); // identity
41     public static final QName FOO_STR = QName.create(FOO_NS, "str"); // key of type 'string'
42     public static final QName FOO_QNAME = QName.create(FOO_NS, "qname"); // key of type 'one' based
43     public static final QName FOO_ID = QName.create(FOO_NS, "id"); // key of type 'instance-identifier'
44
45     public static final String BAR_NS = "barns"; // namespace for prefix 'bar'
46     public static final QName BAR_FOO = QName.create(BAR_NS, "foo"); // leaf of type 'foo:one' based
47     public static final QName BAR_BAR = QName.create(BAR_NS, "bar"); // leaf of type 'instance-identifier'
48     public static final QName BAR_TWO = QName.create(BAR_NS, "two"); // identity inheriting 'foo:one'
49
50     public static XmlCodec<YangInstanceIdentifier> CODEC;
51
52     @Mock
53     public XMLStreamWriter writer;
54     @Captor
55     public ArgumentCaptor<String> captor;
56
57     @BeforeAll
58     public static void beforeAll() {
59         final var modelContext = YangParserTestUtils.parseYangResourceDirectory("/yt1473");
60         final var baz = modelContext.getDataChildByName(FOO_BAZ);
61         assertTrue(baz instanceof ListSchemaNode);
62         final var id = ((ListSchemaNode) baz).getDataChildByName(FOO_ID);
63         assertTrue(id instanceof LeafSchemaNode);
64         final var type = ((LeafSchemaNode) id).getType();
65         assertTrue(type instanceof InstanceIdentifierTypeDefinition);
66         CODEC = (XmlStringInstanceIdentifierCodec) XmlCodecFactory.create(modelContext)
67                 .instanceIdentifierCodec((InstanceIdentifierTypeDefinition) type);
68     }
69
70     @AfterAll
71     public static void afterAll() {
72         CODEC = null;
73     }
74
75     @Test
76     public void testSerializeSimple() throws Exception {
77         // No escaping needed, use single quotes
78         assertEquals("/foo:foo[foo:str='str\"']", write(buildYangInstanceIdentifier(FOO_FOO, FOO_STR, "str\"")));
79     }
80
81     @Test
82     @Disabled("YT-1473: string escaping needs to work")
83     public void testSerializeEscaped() throws Exception {
84         // Escaping is needed, use double quotes and escape
85         assertEquals("/foo:foo[foo:str=\"str'\\\"\"]", write(buildYangInstanceIdentifier(FOO_FOO, FOO_STR, "str'\"")));
86     }
87
88     @Test
89     @Disabled("YT-1473: QName values need to be recognized and properly encoded via identity codec")
90     public void testSerializeIdentityRefSame() throws Exception {
91         // TODO: an improvement is to use just 'one' as the namespace is the same as the leaf (see RFC7951 section 6.8)
92         assertEquals("/foo:bar[qname='one']", write(buildYangInstanceIdentifier(FOO_BAR, FOO_QNAME, FOO_ONE)));
93     }
94
95     @Test
96     @Disabled("YT-1473: QName values need to be recognized and properly encoded via identity codec")
97     public void testSerializeIdentityRefOther() throws Exception {
98         // No escaping is needed, use double quotes and escape
99         assertEquals("/foo:bar[qname='bar:two']", write(buildYangInstanceIdentifier(FOO_BAR, FOO_QNAME, BAR_TWO)));
100     }
101
102     @Test
103     @Disabled("YT-1473: Instance-identifier values need to be recognized and properly encoded and escaped")
104     public void testSerializeInstanceIdentifierRef() throws Exception {
105         assertEquals("/foo:baz[id=\"/foo:bar[qname='bar:two']\"]", write(
106                 buildYangInstanceIdentifier(FOO_BAZ, FOO_ID, buildYangInstanceIdentifier(FOO_BAR, FOO_QNAME, BAR_TWO)))
107         );
108     }
109
110     @Test
111     @Disabled("YT-1473: QName values need to be recognized and properly encoded via identity codec")
112     public void testSerializeIdentityValue() throws Exception {
113         assertEquals("/bar:foo[.='foo:one']", write(buildYangInstanceIdentifier(BAR_FOO, FOO_ONE)));
114     }
115
116     @Test
117     @Disabled("YT-1473: Instance-identifier values need to be recognized and properly encoded and escaped")
118     public void testSerializeInstanceIdentifierValue() throws Exception {
119         assertEquals("/bar:bar[.=\"/foo:bar/bar[qname='bar:two'\"]']",
120                 write(buildYangInstanceIdentifier(BAR_BAR, buildYangInstanceIdentifier(FOO_BAR, FOO_QNAME, BAR_TWO))));
121     }
122
123     private static YangInstanceIdentifier buildYangInstanceIdentifier(final QName node, final QName key,
124             final Object value) {
125         return YangInstanceIdentifier.create(
126                 new NodeIdentifier(node), NodeIdentifierWithPredicates.of(node, key, value));
127     }
128
129     private static YangInstanceIdentifier buildYangInstanceIdentifier(final QName node, final Object value) {
130         return YangInstanceIdentifier.create(new NodeWithValue<>(node, value));
131     }
132
133     private String write(final YangInstanceIdentifier yangInstanceIdentifier) throws Exception {
134         CODEC.writeValue(writer, yangInstanceIdentifier);
135         verify(writer).writeCharacters(captor.capture());
136         return captor.getValue();
137     }
138 }