Add alternative enum assigned name mapping
[mdsal.git] / binding / yang-binding / src / test / java / org / opendaylight / yangtools / yang / binding / BindingMappingTest.java
1 /*
2  * Copyright (c) 2016 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.binding;
9
10 import static com.google.common.collect.ImmutableList.of;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16
17 import java.lang.reflect.Constructor;
18 import java.lang.reflect.InvocationTargetException;
19 import java.net.URI;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.QNameModule;
26 import org.opendaylight.yangtools.yang.common.Revision;
27
28 public class BindingMappingTest {
29
30     @Test
31     public void basicTest() {
32         assertTrue(BindingMapping.getRootPackageName(QName.create(QNameModule.create(URI.create("test:URI"),
33                 Revision.of("2017-10-26")), "test")).contains("test.uri"));
34         assertNull(BindingMapping.normalizePackageName(null));
35         assertTrue(BindingMapping.normalizePackageName("1testpublic").contains("_1testpublic"));
36         assertTrue(BindingMapping.getMethodName(QName.create("testNS", "testLocalName")).equals("testLocalName"));
37         assertTrue(BindingMapping.getMethodName("TestYangIdentifier").equals("testYangIdentifier"));
38         assertTrue(BindingMapping.getClassName(QName.create("testNS", "testClass")).equals("TestClass"));
39         assertTrue(BindingMapping.getClassName("testClass").equals("TestClass"));
40         assertTrue(BindingMapping.getGetterSuffix(QName.create("test", "test")).equals("Test"));
41         assertTrue(BindingMapping.getGetterSuffix(QName.create("test", "class")).equals("XmlClass"));
42         assertTrue(BindingMapping.getPropertyName("Test").equals("test"));
43         assertTrue(BindingMapping.getPropertyName("test").equals("test"));
44         assertTrue(BindingMapping.getPropertyName("Class").equals("xmlClass"));
45         assertEquals("_5", BindingMapping.getPropertyName("5"));
46         assertEquals("", BindingMapping.getPropertyName(""));
47         assertEquals("", BindingMapping.getClassName(""));
48     }
49
50     @Test(expected = UnsupportedOperationException.class)
51     @SuppressWarnings({ "checkstyle:illegalThrows", "checkstyle:avoidHidingCauseException" })
52     public void privateConstructTest() throws Throwable {
53         final Constructor<BindingMapping> bindingMappingConstructor = BindingMapping.class.getDeclaredConstructor();
54         assertFalse(bindingMappingConstructor.isAccessible());
55
56         bindingMappingConstructor.setAccessible(true);
57         try {
58             bindingMappingConstructor.newInstance();
59             fail("Expected exception for calling private constructor");
60         } catch (InvocationTargetException e) {
61             throw e.getCause();
62         }
63     }
64
65     @Test
66     public void mapEnumAssignedNamesTest() {
67         // Okay identifier
68         assertEqualMapping(of("__"), of("__"));
69         assertEqualMapping(of("True"), of("true"));
70         assertEqualMapping(of("ĽaľahoPapľuhu"), of("ľaľaho papľuhu"));
71
72         // Mostly okay, but numbers need to be prefixed
73         assertEqualMapping(of("AZ", "_09"), of("a-z", "0-9"));
74
75         // Invalid identifier (conflicts with a Java 9 keyword)
76         assertEqualMapping(of("_"), of("_"));
77
78         // Invalid characters, fall back to bijection
79         assertEqualMapping(of("$$2A$"), of("*"));
80         assertEqualMapping(of("$$2E$"), of("."));
81         assertEqualMapping(of("$$2F$"), of("/"));
82         assertEqualMapping(of("$$3F$"), of("?"));
83         assertEqualMapping(of("$a$2A$a"), of("a*a"));
84
85         // Conflict, fallback to bijection
86         assertEqualMapping(of("aZ", "$a$2D$z"), of("aZ", "a-z"));
87         assertEqualMapping(of("$a2$2E$5", "a25"), of("a2.5", "a25"));
88         assertEqualMapping(of("$ľaľaho$20$papľuhu", "$ľaľaho$20$$20$papľuhu"), of("ľaľaho papľuhu", "ľaľaho  papľuhu"));
89     }
90
91     private static void assertEqualMapping(final List<String> mapped, final List<String> yang) {
92         assertEquals(mapped.size(), yang.size());
93         final Map<String, String> expected = new HashMap<>();
94         for (int i = 0; i < mapped.size(); ++i) {
95             expected.put(yang.get(i), mapped.get(i));
96         }
97
98         assertEquals(expected, BindingMapping.mapEnumAssignedNames(yang));
99     }
100 }