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