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.mdsal.binding.dom.adapter.invoke;
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 com.google.common.util.concurrent.ListenableFuture;
19 import java.lang.reflect.Method;
21 import java.util.Optional;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.RpcService;
27 import org.opendaylight.yangtools.yang.common.QName;
29 public class AbstractMappedRpcInvokerTest {
32 public void invokeRpcTest() throws Exception {
33 final Method methodWithoutInput =
34 TestRpcService.class.getDeclaredMethod("methodWithoutInput", RpcService.class);
35 final Method methodWithInput =
36 TestRpcService.class.getDeclaredMethod("methodWithInput", RpcService.class, DataObject.class);
38 methodWithInput.setAccessible(true);
39 methodWithoutInput.setAccessible(true);
41 final RpcService rpcService = new TestRpcService();
43 final TestRpcInvokerImpl testRpcInvoker =
44 new TestRpcInvokerImpl(ImmutableMap.of(
45 "(test)tstWithoutInput", methodWithoutInput,
46 "(test)tstWithInput", methodWithInput));
48 assertTrue(testRpcInvoker.map.get("(test)tstWithInput") instanceof RpcMethodInvokerWithInput);
49 assertTrue(testRpcInvoker.map.get("(test)tstWithoutInput") instanceof RpcMethodInvokerWithoutInput);
51 final Crate crateWithoutInput =
52 (Crate) testRpcInvoker.invokeRpc(rpcService, QName.create("test", "tstWithoutInput"), null).get();
53 assertEquals(TestRpcService.methodWithoutInput(rpcService).get().getRpcService(),
54 crateWithoutInput.getRpcService());
55 assertFalse(crateWithoutInput.getDataObject().isPresent());
57 final DataObject dataObject = mock(DataObject.class);
58 final Crate crateWithInput =
59 (Crate) testRpcInvoker.invokeRpc(rpcService, QName.create("test", "tstWithInput"), dataObject).get();
60 assertEquals(TestRpcService.methodWithInput(rpcService, dataObject).get().getRpcService(),
61 crateWithInput.getRpcService());
62 assertTrue(crateWithInput.getDataObject().isPresent());
63 assertEquals(dataObject, crateWithInput.getDataObject().get());
66 private class TestRpcInvokerImpl extends AbstractMappedRpcInvoker<String> {
68 TestRpcInvokerImpl(final Map<String, Method> map) {
73 protected String qnameToKey(final QName qname) {
74 return qname.toString();
79 private final RpcService rpcService;
80 private final ThreadLocal<Optional<DataObject>> dataObject;
82 Crate(@Nonnull final RpcService rpcService, @Nullable final DataObject dataObject) {
83 this.rpcService = rpcService;
85 ThreadLocal.withInitial(() -> dataObject == null ? Optional.empty() : Optional.of(dataObject));
88 RpcService getRpcService() {
89 return this.rpcService;
92 Optional<DataObject> getDataObject() {
93 return this.dataObject.get();
97 static class TestRpcService implements RpcService {
98 static ListenableFuture<Crate> methodWithoutInput(final RpcService testArgument) {
99 return Futures.immediateFuture(new Crate(testArgument, null));
102 static ListenableFuture<Crate> methodWithInput(final RpcService testArgument, final DataObject testArgument2) {
103 return Futures.immediateFuture(new Crate(testArgument, testArgument2));