Use YANG-derived prefixes
[yangtools.git] / codec / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / NamespacePrefixesTest.java
1 /*
2  * Copyright (c) 2014 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.xml;
9
10 import static org.hamcrest.CoreMatchers.hasItem;
11 import static org.hamcrest.CoreMatchers.not;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14
15 import com.google.common.collect.Iterables;
16 import java.util.ArrayList;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Map;
20 import org.junit.jupiter.api.Test;
21 import org.opendaylight.yangtools.yang.common.XMLNamespace;
22
23 class NamespacePrefixesTest {
24     static final int MAX_COUNTER = 4000;
25
26     @Test
27     void testEncodeDecode() {
28         final var allGenerated = new ArrayList<>(MAX_COUNTER);
29         for (int i = 0; i < MAX_COUNTER; i++) {
30             final var encoded = NamespacePrefixes.encode(i);
31             assertEquals(decode(encoded), i);
32             allGenerated.add(encoded);
33         }
34
35         assertEquals(allGenerated.size(), MAX_COUNTER);
36         assertEquals("dPT", allGenerated.get(MAX_COUNTER - 1));
37         assertEquals("a", allGenerated.get(0));
38         assertEquals(allGenerated.size(), new HashSet<>(allGenerated).size());
39     }
40
41     @Test
42     void testQNameWithPrefix() {
43         final var a = new NamespacePrefixes(PreferredPrefixes.empty(), null);
44
45         final var allGenerated = new ArrayList<String>();
46         for (int i = 0; i < MAX_COUNTER; i++) {
47             allGenerated.add(a.encodePrefix(XMLNamespace.of("localhost:" + NamespacePrefixes.encode(i))));
48         }
49
50         assertEquals(MAX_COUNTER, allGenerated.size());
51         // We are generating MAX_COUNTER_VALUE + 27 prefixes total, so we should encounter a reset in prefix a start
52         // from 0 at some point. At the end, there should be only 27 values in RandomPrefix cache
53         assertEquals(MAX_COUNTER, a.emittedPrefixes().size());
54         assertThat(allGenerated, not(hasItem("xml")));
55         assertThat(allGenerated, not(hasItem("xmla")));
56         assertThat(allGenerated, not(hasItem("xmlz")));
57
58         assertEquals(1, Iterables.frequency(allGenerated, "a"));
59     }
60
61     @Test
62     void test2QNames1Namespace() {
63         final var a = new NamespacePrefixes(PreferredPrefixes.empty(), null);
64
65         final var uri = XMLNamespace.of("localhost");
66
67         assertEquals(a.encodePrefix(uri), a.encodePrefix(uri));
68         assertEquals(List.of(Map.entry(uri, "a")), a.emittedPrefixes());
69     }
70
71     @Test
72     void testQNameNoPrefix() {
73         final var a = new NamespacePrefixes(PreferredPrefixes.empty(), null);
74
75         final var uri = XMLNamespace.of("localhost");
76         final var second = XMLNamespace.of("second");
77         assertEquals("a", a.encodePrefix(uri));
78         assertEquals("a", a.encodePrefix(uri));
79         assertEquals("b", a.encodePrefix(second));
80         assertEquals(List.of(Map.entry(uri, "a"), Map.entry(second, "b")), a.emittedPrefixes());
81     }
82
83     private static int decode(final String str) {
84         int ret = 0;
85         for (char c : str.toCharArray()) {
86             int idx = NamespacePrefixes.LOOKUP.indexOf(c);
87             if (idx == -1) {
88                 throw new IllegalArgumentException("Invalid string " + str);
89             }
90             ret = (ret << NamespacePrefixes.SHIFT) + idx;
91         }
92         return ret;
93     }
94 }