Fix checkstyle violations in yang-binding
[mdsal.git] / binding / yang-binding / src / test / java / org / opendaylight / yangtools / yang / binding / util / BindingReflectionsTest.java
index 694c807c1f8c4ad5ad0a518f69f1413c24fbe6d8..11fa38a6e33cec679b1a6f8c09ccab85a0df2e08 100644 (file)
@@ -7,19 +7,33 @@
  */
 package org.opendaylight.yangtools.yang.binding.util;
 
-import org.junit.Test;
-import org.opendaylight.yangtools.yang.binding.DataObject;
-
-import java.util.Collections;
-
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.opendaylight.yangtools.yang.binding.util.BindingReflections.findHierarchicalParent;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.Future;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.binding.Augmentation;
+import org.opendaylight.yangtools.yang.binding.BaseIdentity;
+import org.opendaylight.yangtools.yang.binding.ChildOf;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.RpcService;
+import org.opendaylight.yangtools.yang.binding.test.mock.FooChild;
+import org.opendaylight.yangtools.yang.binding.test.mock.GroupingFoo;
+import org.opendaylight.yangtools.yang.common.QName;
 
 public class BindingReflectionsTest {
 
     @Test
-    public void testBindingWithDummyObject() {
+    public void testBindingWithDummyObject() throws Exception {
         assertEquals("Package name should be equal to string", "org.opendaylight.yang.gen.v1.test.rev990939",
                 BindingReflections.getModelRootPackageName("org.opendaylight.yang.gen.v1.test.rev990939"));
         assertEquals("ModuleInfoClassName should be equal to string", "test.$YangModuleInfoImpl",
@@ -30,5 +44,46 @@ public class BindingReflectionsTest {
         assertFalse("Should not be AugmentationChild", BindingReflections.isAugmentationChild(DataObject.class));
         assertTrue("Should be BindingClass", BindingReflections.isBindingClass(DataObject.class));
         assertFalse("Should not be Notification", BindingReflections.isNotification(DataObject.class));
+
+        assertNull(findHierarchicalParent(mock(DataObject.class)));
+        assertEquals(GroupingFoo.class, BindingReflections.findHierarchicalParent(FooChild.class));
+        final ChildOf<?> childOf = mock(FooChild.class);
+        doReturn(FooChild.class).when(childOf).getImplementedInterface();
+        assertEquals(GroupingFoo.class, BindingReflections.findHierarchicalParent(childOf));
+        assertTrue(BindingReflections.isRpcMethod(TestImplementation.class.getDeclaredMethod("rpcMethodTest")));
+        assertEquals(TestImplementation.class, BindingReflections.findAugmentationTarget(TestImplementation.class));
+
+        assertEquals(Object.class, BindingReflections.resolveRpcOutputClass(
+                TestImplementation.class.getDeclaredMethod("rpcMethodTest")).get());
+        assertFalse(BindingReflections.resolveRpcOutputClass(
+                TestImplementation.class.getDeclaredMethod("rpcMethodTest2")).isPresent());
+
+        assertTrue(BindingReflections.getQName(TestImplementation.class).toString().equals("test"));
+    }
+
+    @Test(expected = UnsupportedOperationException.class)
+    @SuppressWarnings("checkstyle:illegalThrows")
+    public void testPrivateConstructor() throws Throwable {
+        assertFalse(BindingReflections.class.getDeclaredConstructor().isAccessible());
+        final Constructor<?> constructor = BindingReflections.class.getDeclaredConstructor();
+        constructor.setAccessible(true);
+        try {
+            constructor.newInstance();
+        } catch (InvocationTargetException e) {
+            throw e.getCause();
+        }
+    }
+
+    private static final class TestImplementation extends BaseIdentity implements Augmentation<TestImplementation>,
+                                                                                    RpcService {
+        public static final QName QNAME = QName.create("test");
+
+        Future<List<Object>> rpcMethodTest() {
+            return null;
+        }
+
+        Future rpcMethodTest2() {
+            return null;
+        }
     }
 }
\ No newline at end of file