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 public class AbstractMappedRpcInvokerTest {
29 public void invokeRpcTest() throws Exception {
30 final Method methodWithInput =
31 TestRpcService.class.getDeclaredMethod("methodWithInput", RpcService.class, DataObject.class);
33 methodWithInput.setAccessible(true);
35 final RpcService rpcService = new TestRpcService();
37 final TestRpcInvokerImpl testRpcInvoker =
38 new TestRpcInvokerImpl(ImmutableMap.of(
39 "(test)tstWithInput", methodWithInput));
41 assertTrue(testRpcInvoker.map.get("(test)tstWithInput") instanceof RpcMethodInvoker);
43 final DataObject dataObject = mock(DataObject.class);
44 final Crate crateWithInput =
45 (Crate) testRpcInvoker.invokeRpc(rpcService, QName.create("test", "tstWithInput"), dataObject).get();
46 assertEquals(TestRpcService.methodWithInput(rpcService, dataObject).get().getRpcService(),
47 crateWithInput.getRpcService());
48 assertTrue(crateWithInput.getDataObject().isPresent());
49 assertEquals(dataObject, crateWithInput.getDataObject().get());
52 private static class TestRpcInvokerImpl extends AbstractMappedRpcInvoker<String> {
53 TestRpcInvokerImpl(final Map<String, Method> map) {
58 protected String qnameToKey(final QName qname) {
59 return qname.toString();
64 private final RpcService rpcService;
65 private final ThreadLocal<Optional<DataObject>> dataObject;
67 Crate(final @NonNull RpcService rpcService, final @Nullable DataObject dataObject) {
68 this.rpcService = rpcService;
70 ThreadLocal.withInitial(() -> dataObject == null ? Optional.empty() : Optional.of(dataObject));
73 RpcService getRpcService() {
77 Optional<DataObject> getDataObject() {
78 return dataObject.get();
82 static class TestRpcService implements RpcService {
83 static ListenableFuture<Crate> methodWithInput(final RpcService testArgument, final DataObject testArgument2) {
84 return Futures.immediateFuture(new Crate(testArgument, testArgument2));