Deprecate RpcService
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / invoke / AbstractMappedRpcInvokerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter.invoke;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.mock;
13
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;
18 import java.util.Map;
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;
26
27 @Deprecated(since = "11.0.0", forRemoval = true)
28 public class AbstractMappedRpcInvokerTest {
29     @Test
30     public void invokeRpcTest() throws Exception {
31         final Method methodWithInput =
32                 TestRpcService.class.getDeclaredMethod("methodWithInput", RpcService.class, DataObject.class);
33
34         methodWithInput.setAccessible(true);
35
36         final RpcService rpcService = new TestRpcService();
37
38         final TestRpcInvokerImpl testRpcInvoker =
39                 new TestRpcInvokerImpl(ImmutableMap.of(
40                         "(test)tstWithInput", methodWithInput));
41
42         assertTrue(testRpcInvoker.map.get("(test)tstWithInput") instanceof RpcMethodInvoker);
43
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());
51     }
52
53     private static class TestRpcInvokerImpl extends AbstractMappedRpcInvoker<String> {
54         TestRpcInvokerImpl(final Map<String, Method> map) {
55             super(map);
56         }
57
58         @Override
59         protected String qnameToKey(final QName qname) {
60             return qname.toString();
61         }
62     }
63
64     static class Crate {
65         private final RpcService rpcService;
66         private final ThreadLocal<Optional<DataObject>> dataObject;
67
68         Crate(final @NonNull RpcService rpcService, final @Nullable DataObject dataObject) {
69             this.rpcService = rpcService;
70             this.dataObject =
71                 ThreadLocal.withInitial(() -> dataObject == null ? Optional.empty() : Optional.of(dataObject));
72         }
73
74         RpcService getRpcService() {
75             return rpcService;
76         }
77
78         Optional<DataObject> getDataObject() {
79             return dataObject.get();
80         }
81     }
82
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));
86         }
87     }
88 }
89