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.assertTrue;
12 import static org.mockito.Mockito.mock;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.lang.reflect.Method;
19 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.RpcService;
25 import org.opendaylight.yangtools.yang.common.QName;
27 @Deprecated(since = "11.0.0", forRemoval = true)
28 public class AbstractMappedRpcInvokerTest {
30 public void invokeRpcTest() throws Exception {
31 final Method methodWithInput =
32 TestRpcService.class.getDeclaredMethod("methodWithInput", RpcService.class, DataObject.class);
34 methodWithInput.setAccessible(true);
36 final RpcService rpcService = new TestRpcService();
38 final TestRpcInvokerImpl testRpcInvoker =
39 new TestRpcInvokerImpl(ImmutableMap.of(
40 "(test)tstWithInput", methodWithInput));
42 assertTrue(testRpcInvoker.map.get("(test)tstWithInput") instanceof RpcMethodInvoker);
44 final DataObject dataObject = mock(DataObject.class);
45 final Crate crateWithInput =
46 (Crate) testRpcInvoker.invokeRpc(rpcService, QName.create("test", "tstWithInput"), dataObject).get();
47 assertEquals(TestRpcService.methodWithInput(rpcService, dataObject).get().getRpcService(),
48 crateWithInput.getRpcService());
49 assertTrue(crateWithInput.getDataObject().isPresent());
50 assertEquals(dataObject, crateWithInput.getDataObject().get());
53 private static class TestRpcInvokerImpl extends AbstractMappedRpcInvoker<String> {
54 TestRpcInvokerImpl(final Map<String, Method> map) {
59 protected String qnameToKey(final QName qname) {
60 return qname.toString();
65 private final RpcService rpcService;
66 private final ThreadLocal<Optional<DataObject>> dataObject;
68 Crate(final @NonNull RpcService rpcService, final @Nullable DataObject dataObject) {
69 this.rpcService = rpcService;
71 ThreadLocal.withInitial(() -> dataObject == null ? Optional.empty() : Optional.of(dataObject));
74 RpcService getRpcService() {
78 Optional<DataObject> getDataObject() {
79 return dataObject.get();
83 static class TestRpcService implements RpcService {
84 static ListenableFuture<Crate> methodWithInput(final RpcService testArgument, final DataObject testArgument2) {
85 return Futures.immediateFuture(new Crate(testArgument, testArgument2));