bb1e5e43a20bcd42639ca8b159ea114e36a5e84a
[mdsal.git] / binding / yang-binding / src / test / java / org / opendaylight / yangtools / yang / binding / util / BindingReflectionsTest.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.binding.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.opendaylight.yangtools.yang.binding.util.BindingReflections.findHierarchicalParent;
17
18 import java.lang.reflect.Constructor;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.concurrent.Future;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.binding.Augmentation;
24 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
25 import org.opendaylight.yangtools.yang.binding.ChildOf;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.RpcService;
28 import org.opendaylight.yangtools.yang.binding.test.mock.FooChild;
29 import org.opendaylight.yangtools.yang.binding.test.mock.GroupingFoo;
30 import org.opendaylight.yangtools.yang.common.QName;
31
32 public class BindingReflectionsTest {
33
34     @Test
35     public void testBindingWithDummyObject() throws Exception {
36         assertEquals("Package name should be equal to string", "org.opendaylight.yang.gen.v1.test.rev990939",
37                 BindingReflections.getModelRootPackageName("org.opendaylight.yang.gen.v1.test.rev990939"));
38         assertEquals("ModuleInfoClassName should be equal to string", "test.$YangModuleInfoImpl",
39                 BindingReflections.getModuleInfoClassName("test"));
40         assertEquals("Module info should be empty Set", Collections.EMPTY_SET,
41                 BindingReflections.loadModuleInfos());
42         assertFalse("Should not be RpcType", BindingReflections.isRpcType(DataObject.class));
43         assertFalse("Should not be AugmentationChild", BindingReflections.isAugmentationChild(DataObject.class));
44         assertTrue("Should be BindingClass", BindingReflections.isBindingClass(DataObject.class));
45         assertFalse("Should not be Notification", BindingReflections.isNotification(DataObject.class));
46
47         assertNull(findHierarchicalParent(mock(DataObject.class)));
48         assertEquals(GroupingFoo.class, BindingReflections.findHierarchicalParent(FooChild.class));
49         final ChildOf childOf = mock(FooChild.class);
50         doReturn(FooChild.class).when(childOf).getImplementedInterface();
51         assertEquals(GroupingFoo.class, BindingReflections.findHierarchicalParent(childOf));
52         assertTrue(BindingReflections.isRpcMethod(TestImplementation.class.getDeclaredMethod("rpcMethodTest")));
53         assertEquals(TestImplementation.class, BindingReflections.findAugmentationTarget(TestImplementation.class));
54
55         assertEquals(Object.class, BindingReflections.resolveRpcOutputClass(
56                 TestImplementation.class.getDeclaredMethod("rpcMethodTest")).get());
57         assertFalse(BindingReflections.resolveRpcOutputClass(
58                 TestImplementation.class.getDeclaredMethod("rpcMethodTest2")).isPresent());
59
60         assertTrue(BindingReflections.getQName(TestImplementation.class).toString().equals("test"));
61     }
62
63     @Test(expected = UnsupportedOperationException.class)
64     public void testPrivateConstructor() throws Throwable {
65         assertFalse(BindingReflections.class.getDeclaredConstructor().isAccessible());
66         final Constructor constructor = BindingReflections.class.getDeclaredConstructor();
67         constructor.setAccessible(true);
68         try {
69             constructor.newInstance();
70         } catch (Exception e) {
71             throw e.getCause();
72         }
73     }
74
75     private static final class TestImplementation extends BaseIdentity implements Augmentation<TestImplementation>,
76                                                                                     RpcService {
77         public static final QName QNAME = QName.create("test");
78
79         Future<List<Object>> rpcMethodTest() {
80             return null;
81         }
82
83         Future rpcMethodTest2() {
84             return null;
85         }
86     }
87 }