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
8 package org.opendaylight.mdsal.binding.dom.adapter.invoke;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.mock;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.lang.reflect.Method;
20 import java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.RpcService;
26 import org.opendaylight.yangtools.yang.common.QName;
28 public class AbstractMappedRpcInvokerTest {
31 public void invokeRpcTest() throws Exception {
32 final Method methodWithoutInput =
33 TestRpcService.class.getDeclaredMethod("methodWithoutInput", RpcService.class);
34 final Method methodWithInput =
35 TestRpcService.class.getDeclaredMethod("methodWithInput", RpcService.class, DataObject.class);
37 methodWithInput.setAccessible(true);
38 methodWithoutInput.setAccessible(true);
40 final RpcService rpcService = new TestRpcService();
42 final TestRpcInvokerImpl testRpcInvoker =
43 new TestRpcInvokerImpl(ImmutableMap.of(
44 "(test)tstWithoutInput", methodWithoutInput,
45 "(test)tstWithInput", methodWithInput));
47 assertTrue(testRpcInvoker.map.get("(test)tstWithInput") instanceof RpcMethodInvokerWithInput);
48 assertTrue(testRpcInvoker.map.get("(test)tstWithoutInput") instanceof RpcMethodInvokerWithoutInput);
50 final Crate crateWithoutInput =
51 (Crate) testRpcInvoker.invokeRpc(rpcService, QName.create("test", "tstWithoutInput"), null).get();
52 assertEquals(TestRpcService.methodWithoutInput(rpcService).get().getRpcService(),
53 crateWithoutInput.getRpcService());
54 assertFalse(crateWithoutInput.getDataObject().isPresent());
56 final DataObject dataObject = mock(DataObject.class);
57 final Crate crateWithInput =
58 (Crate) testRpcInvoker.invokeRpc(rpcService, QName.create("test", "tstWithInput"), dataObject).get();
59 assertEquals(TestRpcService.methodWithInput(rpcService, dataObject).get().getRpcService(),
60 crateWithInput.getRpcService());
61 assertTrue(crateWithInput.getDataObject().isPresent());
62 assertEquals(dataObject, crateWithInput.getDataObject().get());
65 private class TestRpcInvokerImpl extends AbstractMappedRpcInvoker<String> {
67 TestRpcInvokerImpl(final Map<String, Method> map) {
72 protected String qnameToKey(final QName qname) {
73 return qname.toString();
78 private final RpcService rpcService;
79 private final ThreadLocal<Optional<DataObject>> dataObject;
81 Crate(final @NonNull RpcService rpcService, final @Nullable DataObject dataObject) {
82 this.rpcService = rpcService;
84 ThreadLocal.withInitial(() -> dataObject == null ? Optional.empty() : Optional.of(dataObject));
87 RpcService getRpcService() {
88 return this.rpcService;
91 Optional<DataObject> getDataObject() {
92 return this.dataObject.get();
96 static class TestRpcService implements RpcService {
97 static ListenableFuture<Crate> methodWithoutInput(final RpcService testArgument) {
98 return Futures.immediateFuture(new Crate(testArgument, null));
101 static ListenableFuture<Crate> methodWithInput(final RpcService testArgument, final DataObject testArgument2) {
102 return Futures.immediateFuture(new Crate(testArgument, testArgument2));