bcd495c24a80a0d511ce5e394a3a42783c5b2eb5
[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.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.mock;
14
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.lang.reflect.Method;
19 import java.util.Map;
20 import java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.RpcService;
26 import org.opendaylight.yangtools.yang.common.QName;
27
28 public class AbstractMappedRpcInvokerTest {
29
30     @Test
31     public void invokeRpcTest() throws Exception {
32         final Method methodWithoutInput =
33                 TestRpcService.class.getDeclaredMethod("methodWithoutInput", RpcService.class);
34         final Method methodWithInput =
35                 TestRpcService.class.getDeclaredMethod("methodWithInput", RpcService.class, DataObject.class);
36
37         methodWithInput.setAccessible(true);
38         methodWithoutInput.setAccessible(true);
39
40         final RpcService rpcService = new TestRpcService();
41
42         final TestRpcInvokerImpl testRpcInvoker =
43                 new TestRpcInvokerImpl(ImmutableMap.of(
44                         "(test)tstWithoutInput", methodWithoutInput,
45                         "(test)tstWithInput", methodWithInput));
46
47         assertTrue(testRpcInvoker.map.get("(test)tstWithInput") instanceof RpcMethodInvokerWithInput);
48         assertTrue(testRpcInvoker.map.get("(test)tstWithoutInput") instanceof RpcMethodInvokerWithoutInput);
49
50         final Crate crateWithoutInput =
51                 (Crate) testRpcInvoker.invokeRpc(rpcService, QName.create("test", "tstWithoutInput"), null).get();
52         assertEquals(TestRpcService.methodWithoutInput(rpcService).get().getRpcService(),
53                 crateWithoutInput.getRpcService());
54         assertFalse(crateWithoutInput.getDataObject().isPresent());
55
56         final DataObject dataObject = mock(DataObject.class);
57         final Crate crateWithInput =
58                 (Crate) testRpcInvoker.invokeRpc(rpcService, QName.create("test", "tstWithInput"), dataObject).get();
59         assertEquals(TestRpcService.methodWithInput(rpcService, dataObject).get().getRpcService(),
60                 crateWithInput.getRpcService());
61         assertTrue(crateWithInput.getDataObject().isPresent());
62         assertEquals(dataObject, crateWithInput.getDataObject().get());
63     }
64
65     private class TestRpcInvokerImpl extends AbstractMappedRpcInvoker<String> {
66
67         TestRpcInvokerImpl(final Map<String, Method> map) {
68             super(map);
69         }
70
71         @Override
72         protected String qnameToKey(final QName qname) {
73             return qname.toString();
74         }
75     }
76
77     static class Crate {
78         private final RpcService rpcService;
79         private final ThreadLocal<Optional<DataObject>> dataObject;
80
81         Crate(final @NonNull RpcService rpcService, final @Nullable DataObject dataObject) {
82             this.rpcService = rpcService;
83             this.dataObject =
84                 ThreadLocal.withInitial(() -> dataObject == null ? Optional.empty() : Optional.of(dataObject));
85         }
86
87         RpcService getRpcService() {
88             return this.rpcService;
89         }
90
91         Optional<DataObject> getDataObject() {
92             return this.dataObject.get();
93         }
94     }
95
96     static class TestRpcService implements RpcService {
97         static ListenableFuture<Crate> methodWithoutInput(final RpcService testArgument) {
98             return Futures.immediateFuture(new Crate(testArgument, null));
99         }
100
101         static ListenableFuture<Crate> methodWithInput(final RpcService testArgument, final DataObject testArgument2) {
102             return Futures.immediateFuture(new Crate(testArgument, testArgument2));
103         }
104     }
105 }
106