Bug 1441: Bug fixes, clean-up and test migration
[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 com.google.common.collect.Lists;
15 import com.google.common.collect.Sets;
16 import java.net.URI;
17 import java.util.Date;
18 import java.util.List;
19 import org.hamcrest.CoreMatchers;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
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 = Lists.newArrayList();
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(), Sets.newHashSet(allGenerated).size());
41     }
42
43     @Test
44     public void testQNameWithPrefix() throws Exception {
45         final RandomPrefix a = new RandomPrefix();
46
47         final List<String> allGenerated = Lists.newArrayList();
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, new Date()), "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 from 0 at some point
57         // 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();
69
70         final URI uri = URI.create("localhost");
71         final QName qName = QName.create(QNameModule.create(uri, new Date()), "local-name");
72         final QName qName2 = QName.create(QNameModule.create(uri, new Date()), "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();
80
81         final URI uri = URI.create("localhost");
82         QName qName = QName.create(uri, new Date(), "local-name");
83         assertEquals("a", a.encodePrefix(qName.getNamespace()));
84         qName = QName.create(QNameModule.create(uri, new Date()), "local-name");
85         assertEquals("a", a.encodePrefix(qName.getNamespace()));
86         qName = QName.create(QNameModule.create(URI.create("second"), new Date()), "local-name");
87         assertEquals("b", a.encodePrefix(qName.getNamespace()));
88
89     }
90 }