Bug 6163: fixed number of argument when resolving rpc input
[mdsal.git] / binding / yang-binding / src / test / java / org / opendaylight / yangtools / yang / binding / util / 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
9 package org.opendaylight.yangtools.yang.binding.util;
10
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;
15
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.util.concurrent.Futures;
18 import java.lang.reflect.Field;
19 import java.lang.reflect.Method;
20 import java.util.Map;
21 import java.util.Optional;
22 import java.util.concurrent.Future;
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.RpcService;
28 import org.opendaylight.yangtools.yang.common.QName;
29
30 public class AbstractMappedRpcInvokerTest {
31
32     @Test
33     public void invokeRpcTest() throws Exception {
34         final Method methodWithoutInput =
35                 TestRpcService.class.getDeclaredMethod("methodWithoutInput", RpcService.class);
36         final Method methodWithInput =
37                 TestRpcService.class.getDeclaredMethod("methodWithInput", RpcService.class, DataObject.class);
38
39         methodWithInput.setAccessible(true);
40         methodWithoutInput.setAccessible(true);
41
42         final RpcService rpcService = new TestRpcService();
43
44         final RpcServiceInvoker testRpcInvoker =
45                 new TestRpcInvokerImpl(ImmutableMap.of(
46                         "tstWithoutInput", methodWithoutInput,
47                         "tstWithInput", methodWithInput));
48
49         final Field testInvokerMapField = testRpcInvoker.getClass().getSuperclass().getDeclaredField("map");
50         testInvokerMapField.setAccessible(true);
51         final Map<String, RpcMethodInvoker> testInvokerMap =
52                 (Map<String, RpcMethodInvoker>) testInvokerMapField.get(testRpcInvoker);
53
54         assertTrue(testInvokerMap.get("tstWithInput") instanceof RpcMethodInvokerWithInput);
55         assertTrue(testInvokerMap.get("tstWithoutInput") instanceof RpcMethodInvokerWithoutInput);
56
57         final Crate crateWithoutInput =
58                 (Crate) testRpcInvoker.invokeRpc(rpcService, QName.create("tstWithoutInput"), null).get();
59         assertEquals(TestRpcService.methodWithoutInput(rpcService).get().getRpcService(),
60                 crateWithoutInput.getRpcService());
61         assertFalse(crateWithoutInput.getDataObject().isPresent());
62
63         final DataObject dataObject = mock(DataObject.class);
64         final Crate crateWithInput =
65                 (Crate) testRpcInvoker.invokeRpc(rpcService, QName.create("tstWithInput"), dataObject).get();
66         assertEquals(TestRpcService.methodWithInput(rpcService, dataObject).get().getRpcService(),
67                 crateWithInput.getRpcService());
68         assertTrue(crateWithInput.getDataObject().isPresent());
69         assertEquals(dataObject, crateWithInput.getDataObject().get());
70     }
71
72     private class TestRpcInvokerImpl extends AbstractMappedRpcInvoker<String> {
73
74         TestRpcInvokerImpl(Map<String, Method> map) {
75             super(map);
76         }
77
78         @Override
79         protected String qnameToKey(QName qname) {
80             return qname.toString();
81         }
82     }
83
84     static class Crate {
85         private final RpcService rpcService;
86         private final ThreadLocal<Optional<DataObject>> dataObject;
87
88         Crate(@Nonnull RpcService rpcService, @Nullable DataObject dataObject) {
89             this.rpcService = rpcService;
90             this.dataObject = new ThreadLocal<Optional<DataObject>>() {
91                 @Override
92                 protected Optional<DataObject> initialValue() {
93                     return dataObject == null ? Optional.empty() : Optional.of(dataObject);
94                 }
95             };
96         }
97
98         RpcService getRpcService() {
99             return this.rpcService;
100         }
101
102         Optional<DataObject> getDataObject() {
103             return this.dataObject.get();
104         }
105     }
106
107     static class TestRpcService implements RpcService {
108         static Future<Crate> methodWithoutInput(RpcService testArgument) {
109             return Futures.immediateFuture(new Crate(testArgument, null));
110         }
111
112         static Future<Crate> methodWithInput(RpcService testArgument, DataObject testArgument2) {
113             return Futures.immediateFuture(new Crate(testArgument, testArgument2));
114         }
115     }
116 }
117