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