Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / RandomPrefixTest.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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThat;
12
13 import com.google.common.collect.Iterables;
14 import java.net.URI;
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
18 import org.hamcrest.CoreMatchers;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.Revision;
23
24 public class RandomPrefixTest {
25     static final int MAX_COUNTER = 4000;
26
27     @Test
28     public void testEncodeDecode() throws Exception {
29
30         final List<String> allGenerated = new ArrayList<>(MAX_COUNTER);
31         for (int i = 0; i < MAX_COUNTER; i++) {
32             final String encoded = RandomPrefix.encode(i);
33             assertEquals(RandomPrefix.decode(encoded), i);
34             allGenerated.add(encoded);
35         }
36
37         assertEquals(allGenerated.size(), MAX_COUNTER);
38         assertEquals("dPT", allGenerated.get(MAX_COUNTER - 1));
39         assertEquals("a", allGenerated.get(0));
40         assertEquals(allGenerated.size(), new HashSet<>(allGenerated).size());
41     }
42
43     @Test
44     public void testQNameWithPrefix() throws Exception {
45         final RandomPrefix a = new RandomPrefix(null);
46
47         final List<String> allGenerated = new ArrayList<>();
48         for (int i = 0; i < MAX_COUNTER; i++) {
49             final String prefix = RandomPrefix.encode(i);
50             final URI uri = new URI("localhost:" + prefix);
51             final QName qname = QName.create(QNameModule.create(uri, Revision.of("2000-01-01")), "local-name");
52             allGenerated.add(a.encodePrefix(qname.getNamespace()));
53         }
54
55         assertEquals(MAX_COUNTER, allGenerated.size());
56         // We are generating MAX_COUNTER_VALUE + 27 prefixes total, so we should encounter a reset in prefix a start
57         // from 0 at some point. At the end, there should be only 27 values in RandomPrefix cache
58         assertEquals(MAX_COUNTER, Iterables.size(a.getPrefixes()));
59         assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xml")));
60         assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xmla")));
61         assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xmlz")));
62
63         assertEquals(1, Iterables.frequency(allGenerated, "a"));
64     }
65
66     @Test
67     public void test2QNames1Namespace() throws Exception {
68         final RandomPrefix a = new RandomPrefix(null);
69
70         final URI uri = URI.create("localhost");
71         final QName qname = QName.create(QNameModule.create(uri, Revision.of("2000-01-01")), "local-name");
72         final QName qname2 = QName.create(QNameModule.create(uri, Revision.of("2000-01-01")), "local-name");
73
74         assertEquals(a.encodePrefix(qname.getNamespace()), a.encodePrefix(qname2.getNamespace()));
75     }
76
77     @Test
78     public void testQNameNoPrefix() throws Exception {
79         final RandomPrefix a = new RandomPrefix(null);
80
81         final URI uri = URI.create("localhost");
82         QName qname = QName.create(uri, Revision.of("2000-01-01"), "local-name");
83         assertEquals("a", a.encodePrefix(qname.getNamespace()));
84         qname = QName.create(QNameModule.create(uri, Revision.of("2000-01-01")), "local-name");
85         assertEquals("a", a.encodePrefix(qname.getNamespace()));
86         qname = QName.create(QNameModule.create(URI.create("second"), Revision.of("2000-01-01")), "local-name");
87         assertEquals("b", a.encodePrefix(qname.getNamespace()));
88
89     }
90 }