Fix bits/instance-identifier value serdes
[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.jupiter.api.Assertions.assertInstanceOf;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
15
16 import com.google.common.collect.ImmutableSet;
17 import javax.xml.namespace.NamespaceContext;
18 import javax.xml.stream.XMLStreamWriter;
19 import org.junit.jupiter.api.AfterAll;
20 import org.junit.jupiter.api.BeforeAll;
21 import org.junit.jupiter.api.Test;
22 import org.mockito.ArgumentCaptor;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32
33 class YT1473Test {
34     private static final String FOO_NS = "foons"; // namespace for prefix 'foo'
35     private static final QName FOO_FOO = QName.create(FOO_NS, "foo"); // list with key 'str'
36     private static final QName FOO_BAR = QName.create(FOO_NS, "bar"); // list with key 'qname'
37     private static final QName FOO_BAZ = QName.create(FOO_NS, "baz"); // list with key 'id'
38     private static final QName FOO_BEE = QName.create(FOO_NS, "bee"); // list with key 'bts'
39     private static final QName FOO_ONE = QName.create(FOO_NS, "one"); // identity
40     private static final QName FOO_STR = QName.create(FOO_NS, "str"); // key of type 'string'
41     private static final QName FOO_QNAME = QName.create(FOO_NS, "qname"); // key of type 'one' based
42     private static final QName FOO_ID = QName.create(FOO_NS, "id"); // key of type 'instance-identifier'
43     private static final QName FOO_BTS = QName.create(FOO_NS, "bts"); // key of type 'bts' (bits)
44
45     private static final String BAR_NS = "barns"; // namespace for prefix 'bar'
46     private static final QName BAR_TWO = QName.create(BAR_NS, "two"); // identity inheriting 'foo:one'
47     private static final QName BAR_STR = QName.create(BAR_NS, "str"); // leaf-list of type 'string'
48     private static final QName BAR_FOO = QName.create(BAR_NS, "foo"); // leaf-list of type 'foo:one' based
49     private static final QName BAR_BAR = QName.create(BAR_NS, "bar"); // leaf-list of type 'instance-identifier'
50     private static final QName BAR_BEE = QName.create(BAR_NS, "bee"); // leaf-list of type 'foo:bts' (bits)
51
52     private static XmlCodec<YangInstanceIdentifier> CODEC;
53
54     @BeforeAll
55     static void beforeAll() {
56         final var modelContext = YangParserTestUtils.parseYangResourceDirectory("/yt1473");
57         final var baz = assertInstanceOf(ListSchemaNode.class, modelContext.getDataChildByName(FOO_BAZ));
58         final var id = assertInstanceOf(LeafSchemaNode.class, baz.getDataChildByName(FOO_ID));
59         final var type = assertInstanceOf(InstanceIdentifierTypeDefinition.class, id.getType());
60         CODEC = XmlCodecFactory.create(modelContext).instanceIdentifierCodec(type);
61     }
62
63     @AfterAll
64     static void afterAll() {
65         CODEC = null;
66     }
67
68     @Test
69     void testSerializeSimple() throws Exception {
70         // No escaping needed, use single quotes
71         assertSerdes("/bar:str[.='str\"']", buildYangInstanceIdentifier(BAR_STR, "str\""));
72         assertSerdes("/bar:str[.='str\\']", buildYangInstanceIdentifier(BAR_STR, "str\\"));
73         assertSerdes("/bar:str[.='str\r']", buildYangInstanceIdentifier(BAR_STR, "str\r"));
74         assertSerdes("/bar:str[.='str\n']", buildYangInstanceIdentifier(BAR_STR, "str\n"));
75         assertSerdes("/bar:str[.='str\t']", buildYangInstanceIdentifier(BAR_STR, "str\t"));
76
77         assertSerdes("/foo:foo[foo:str='str\"\\']", buildYangInstanceIdentifier(FOO_FOO, FOO_STR, "str\"\\"));
78         assertSerdes("/foo:foo[foo:str='str\r\n\t']", buildYangInstanceIdentifier(FOO_FOO, FOO_STR, "str\r\n\t"));
79     }
80
81     @Test
82     void testSerializeEscaped() throws Exception {
83         // Escaping is needed, use double quotes and escape
84         assertSerdes("/bar:str[.=\"str'\\\"\"]", buildYangInstanceIdentifier(BAR_STR, "str'\""));
85         assertSerdes("/bar:str[.=\"str'\\n\"]", buildYangInstanceIdentifier(BAR_STR, "str'\n"));
86         assertSerdes("/bar:str[.=\"str'\\t\"]", buildYangInstanceIdentifier(BAR_STR, "str'\t"));
87         assertSerdes("/bar:str[.=\"str'\r\"]", buildYangInstanceIdentifier(BAR_STR, "str'\r"));
88
89         assertSerdes("/foo:foo[foo:str=\"str'\\\"\\n\"]", buildYangInstanceIdentifier(FOO_FOO, FOO_STR, "str'\"\n"));
90         assertSerdes("/foo:foo[foo:str=\"str'\\t\r\"]", buildYangInstanceIdentifier(FOO_FOO, FOO_STR, "str'\t\r"));
91     }
92
93     @Test
94     void testSerializeIdentity() throws Exception {
95         assertSerdes("/foo:bar[foo:qname='foo:one']", buildYangInstanceIdentifier(FOO_BAR, FOO_QNAME, FOO_ONE));
96         assertSerdes("/foo:bar[foo:qname='bar:two']", buildYangInstanceIdentifier(FOO_BAR, FOO_QNAME, BAR_TWO));
97     }
98
99     @Test
100     void testSerializeInstanceIdentifierRef() throws Exception {
101         assertSerdes("/foo:baz[foo:id=\"/foo:bar[foo:qname='bar:two']\"]",
102             buildYangInstanceIdentifier(FOO_BAZ, FOO_ID, buildYangInstanceIdentifier(FOO_BAR, FOO_QNAME, BAR_TWO)));
103     }
104
105     @Test
106     void testSerializeIdentityValue() throws Exception {
107         assertSerdes("/bar:foo[.='foo:one']", buildYangInstanceIdentifier(BAR_FOO, FOO_ONE));
108         assertSerdes("/bar:foo[.='bar:two']", buildYangInstanceIdentifier(BAR_FOO, BAR_TWO));
109     }
110
111     @Test
112     void testSerializeInstanceIdentifierValue() throws Exception {
113         assertSerdes("/bar:bar[.=\"/foo:bar[foo:qname='foo:one']\"]",
114             buildYangInstanceIdentifier(BAR_BAR, buildYangInstanceIdentifier(FOO_BAR, FOO_QNAME, FOO_ONE)));
115         assertSerdes("/bar:bar[.=\"/foo:bar[foo:qname='bar:two']\"]",
116             buildYangInstanceIdentifier(BAR_BAR, buildYangInstanceIdentifier(FOO_BAR, FOO_QNAME, BAR_TWO)));
117     }
118
119     @Test
120     void testSerializeBits() throws Exception {
121         assertSerdes("/foo:bee[foo:bts='']", buildYangInstanceIdentifier(FOO_BEE, FOO_BTS, ImmutableSet.of()));
122         assertSerdes("/foo:bee[foo:bts='one']", buildYangInstanceIdentifier(FOO_BEE, FOO_BTS, ImmutableSet.of("one")));
123         assertSerdes("/foo:bee[foo:bts='two three']",
124             buildYangInstanceIdentifier(FOO_BEE, FOO_BTS, ImmutableSet.of("two", "three")));
125     }
126
127     @Test
128     void testSerializeBitsValue() throws Exception {
129         assertSerdes("/bar:bee[.='']", buildYangInstanceIdentifier(BAR_BEE, ImmutableSet.of()));
130         assertSerdes("/bar:bee[.='one']", buildYangInstanceIdentifier(BAR_BEE, ImmutableSet.of("one")));
131         assertSerdes("/bar:bee[.='two three']", buildYangInstanceIdentifier(BAR_BEE, ImmutableSet.of("two", "three")));
132     }
133
134     private static void assertSerdes(final String expected, final YangInstanceIdentifier id) throws Exception {
135         final var writer = mock(XMLStreamWriter.class);
136         final var captor = ArgumentCaptor.forClass(String.class);
137         CODEC.writeValue(writer, id);
138         verify(writer).writeCharacters(captor.capture());
139         assertEquals(expected, captor.getValue());
140
141         final var context = mock(NamespaceContext.class);
142         doReturn("foons").when(context).getNamespaceURI("foo");
143         doReturn("barns").when(context).getNamespaceURI("bar");
144         assertEquals(id, CODEC.parseValue(context, expected));
145     }
146
147     private static YangInstanceIdentifier buildYangInstanceIdentifier(final QName node, final QName key,
148             final Object value) {
149         return YangInstanceIdentifier.create(
150                 new NodeIdentifier(node), NodeIdentifierWithPredicates.of(node, key, value));
151     }
152
153     private static YangInstanceIdentifier buildYangInstanceIdentifier(final QName node, final Object value) {
154         return YangInstanceIdentifier.create(new NodeIdentifier(node), new NodeWithValue<>(node, value));
155     }
156 }