ecd167b43330582c7eca7929f609e5409c23434a
[mdsal.git] / binding / yang-binding / src / test / java / org / opendaylight / yangtools / yang / binding / util / RpcMethodInvokerWithInputTest.java
1 /*
2  * Copyright (c) 2016 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.assertNotNull;
11
12 import com.google.common.util.concurrent.Futures;
13 import java.lang.invoke.MethodHandle;
14 import java.lang.invoke.MethodHandles;
15 import java.util.concurrent.Future;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.RpcService;
19
20 public class RpcMethodInvokerWithInputTest {
21
22     private static final TestImplClassWithInput TEST_IMPL_CLASS = new TestImplClassWithInput();
23
24     @Test
25     public void invokeOnTest() throws Exception {
26         final MethodHandle methodHandle = MethodHandles.lookup().unreflect(
27                 TestImplClassWithInput.class.getDeclaredMethod("testMethod", RpcService.class, DataObject.class));
28         final RpcMethodInvokerWithInput rpcMethodInvokerWithInput = new RpcMethodInvokerWithInput(methodHandle);
29         assertNotNull(rpcMethodInvokerWithInput.invokeOn(TEST_IMPL_CLASS, null));
30     }
31
32     @Test(expected = InternalError.class)
33     public void invokeOnWithException() throws Exception {
34         final MethodHandle methodHandle = MethodHandles.lookup().unreflect(TestImplClassWithInput.class
35                 .getDeclaredMethod("testMethodWithException", RpcService.class, DataObject.class));
36         final RpcMethodInvokerWithInput rpcMethodInvokerWithInput = new RpcMethodInvokerWithInput(methodHandle);
37         rpcMethodInvokerWithInput.invokeOn(TEST_IMPL_CLASS, null);
38     }
39
40     private static final class TestImplClassWithInput implements RpcService {
41
42         static Future testMethod(RpcService testArg, DataObject data) {
43             return Futures.immediateFuture(null);
44         }
45
46         static Future testMethodWithException(RpcService testArg, DataObject data) throws Exception {
47             throw new InternalError();
48         }
49     }
50 }