2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.binding;
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;
17 import java.lang.reflect.Constructor;
18 import java.lang.reflect.InvocationTargetException;
20 import java.util.HashMap;
21 import java.util.List;
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;
28 public class BindingMappingTest {
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(""));
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());
56 bindingMappingConstructor.setAccessible(true);
58 bindingMappingConstructor.newInstance();
59 fail("Expected exception for calling private constructor");
60 } catch (InvocationTargetException e) {
66 public void mapEnumAssignedNamesTest() {
68 assertEqualMapping(of("__"), of("__"));
69 assertEqualMapping(of("True"), of("true"));
70 assertEqualMapping(of("ĽaľahoPapľuhu"), of("ľaľaho papľuhu"));
72 // Mostly okay, but numbers need to be prefixed
73 assertEqualMapping(of("AZ", "_09"), of("a-z", "0-9"));
75 // Invalid identifier (conflicts with a Java 9 keyword)
76 assertEqualMapping(of("_"), of("_"));
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"));
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"));
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));
98 assertEquals(expected, BindingMapping.mapEnumAssignedNames(yang));