2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.binding.util;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Mockito.mock;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.util.concurrent.Futures;
18 import java.lang.reflect.Field;
19 import java.lang.reflect.Method;
21 import java.util.Optional;
22 import java.util.concurrent.Future;
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.RpcService;
28 import org.opendaylight.yangtools.yang.common.QName;
30 public class AbstractMappedRpcInvokerTest {
33 public void invokeRpcTest() throws Exception {
34 final Method methodWithoutInput =
35 TestRpcService.class.getDeclaredMethod("methodWithoutInput", RpcService.class);
36 final Method methodWithInput =
37 TestRpcService.class.getDeclaredMethod("methodWithInput", RpcService.class, DataObject.class);
39 methodWithInput.setAccessible(true);
40 methodWithoutInput.setAccessible(true);
42 final RpcService rpcService = new TestRpcService();
44 final RpcServiceInvoker testRpcInvoker =
45 new TestRpcInvokerImpl(ImmutableMap.of(
46 "tstWithoutInput", methodWithoutInput,
47 "tstWithInput", methodWithInput));
49 final Field testInvokerMapField = testRpcInvoker.getClass().getSuperclass().getDeclaredField("map");
50 testInvokerMapField.setAccessible(true);
51 final Map<String, RpcMethodInvoker> testInvokerMap =
52 (Map<String, RpcMethodInvoker>) testInvokerMapField.get(testRpcInvoker);
54 assertTrue(testInvokerMap.get("tstWithInput") instanceof RpcMethodInvokerWithInput);
55 assertTrue(testInvokerMap.get("tstWithoutInput") instanceof RpcMethodInvokerWithoutInput);
57 final Crate crateWithoutInput =
58 (Crate) testRpcInvoker.invokeRpc(rpcService, QName.create("tstWithoutInput"), null).get();
59 assertEquals(TestRpcService.methodWithoutInput(rpcService).get().getRpcService(),
60 crateWithoutInput.getRpcService());
61 assertFalse(crateWithoutInput.getDataObject().isPresent());
63 final DataObject dataObject = mock(DataObject.class);
64 final Crate crateWithInput =
65 (Crate) testRpcInvoker.invokeRpc(rpcService, QName.create("tstWithInput"), dataObject).get();
66 assertEquals(TestRpcService.methodWithInput(rpcService, dataObject).get().getRpcService(),
67 crateWithInput.getRpcService());
68 assertTrue(crateWithInput.getDataObject().isPresent());
69 assertEquals(dataObject, crateWithInput.getDataObject().get());
72 private class TestRpcInvokerImpl extends AbstractMappedRpcInvoker<String> {
74 TestRpcInvokerImpl(Map<String, Method> map) {
79 protected String qnameToKey(QName qname) {
80 return qname.toString();
85 private final RpcService rpcService;
86 private final ThreadLocal<Optional<DataObject>> dataObject;
88 Crate(@Nonnull RpcService rpcService, @Nullable DataObject dataObject) {
89 this.rpcService = rpcService;
90 this.dataObject = new ThreadLocal<Optional<DataObject>>() {
92 protected Optional<DataObject> initialValue() {
93 return dataObject == null ? Optional.empty() : Optional.of(dataObject);
98 RpcService getRpcService() {
99 return this.rpcService;
102 Optional<DataObject> getDataObject() {
103 return this.dataObject.get();
107 static class TestRpcService implements RpcService {
108 static Future<Crate> methodWithoutInput(RpcService testArgument) {
109 return Futures.immediateFuture(new Crate(testArgument, null));
112 static Future<Crate> methodWithInput(RpcService testArgument, DataObject testArgument2) {
113 return Futures.immediateFuture(new Crate(testArgument, testArgument2));