36146d55a4dac89ca0f7641689371456dd587107
[mdsal.git] / binding2 / mdsal-binding2-runtime / src / test / java / org / opendaylight / mdsal / binding / javav2 / runtime / reflection / BindingReflectionsTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.mdsal.binding.javav2.runtime.reflection;
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.mock;
15
16 import java.lang.reflect.Constructor;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.concurrent.Future;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.binding.javav2.runtime.reflection.test.mock.FooChild;
22 import org.opendaylight.mdsal.binding.javav2.runtime.reflection.test.mock.GroupingFoo;
23 import org.opendaylight.mdsal.binding.javav2.spec.base.BaseIdentity;
24 import org.opendaylight.mdsal.binding.javav2.spec.base.Input;
25 import org.opendaylight.mdsal.binding.javav2.spec.base.Rpc;
26 import org.opendaylight.mdsal.binding.javav2.spec.base.RpcCallback;
27 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
28 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
29 import org.opendaylight.mdsal.binding.javav2.spec.structural.TreeChildNode;
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.mdsal.gen.javav2.test.rev990939",
37                 BindingReflections.getModelRootPackageName("org.opendaylight.mdsal.gen.javav2.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, BindingReflections.loadModuleInfos());
41         assertFalse("Should not be RpcType", BindingReflections.isOperationType(TreeNode.class));
42         assertFalse("Should not be AugmentationChild", BindingReflections.isAugmentationChild(TreeNode.class));
43         assertTrue("Should be BindingClass", BindingReflections.isBindingClass(TreeNode.class));
44         assertFalse("Should not be Notification", BindingReflections.isNotification(TreeNode.class));
45
46         assertNull(mock(TreeChildNode.class).treeParent());
47
48         assertEquals(GroupingFoo.class, BindingReflections.findHierarchicalParent(FooChild.class));
49
50         assertTrue(BindingReflections.isOperationMethod(TestImplementation.class.getDeclaredMethod("rpcMethodTest")));
51         assertEquals(TestImplementation.class, BindingReflections.findAugmentationTarget(TestImplementation.class));
52
53         assertEquals(Object.class, BindingReflections
54                 .resolveOperationOutputClass(TestImplementation.class.getDeclaredMethod("rpcMethodTest")).get());
55         assertFalse(BindingReflections
56                 .resolveOperationOutputClass(TestImplementation.class.getDeclaredMethod("rpcMethodTest2")).isPresent());
57
58         assertTrue(BindingReflections.getQName(TestImplementation.class).toString().equals("test"));
59     }
60
61     @SuppressWarnings("rawtypes")
62     @Test(expected = UnsupportedOperationException.class)
63     public void testPrivateConstructor() throws Throwable {
64         assertFalse(BindingReflections.class.getDeclaredConstructor().isAccessible());
65         final Constructor constructor = BindingReflections.class.getDeclaredConstructor();
66         constructor.setAccessible(true);
67         try {
68             constructor.newInstance();
69         } catch (final Exception e) {
70             throw e.getCause();
71         }
72     }
73
74     @SuppressWarnings({ "rawtypes", "unused" })
75     private static final class TestImplementation extends BaseIdentity
76             implements Augmentation<TestImplementation>, Rpc {
77
78         public static final QName QNAME = QName.create("test");
79
80         Future<List<Object>> rpcMethodTest() {
81             return null;
82         }
83
84         Future rpcMethodTest2() {
85             return null;
86         }
87
88         @Override
89         public void invoke(final Input input, final RpcCallback callback) {
90         }
91     }
92 }