4a6201af831b88d890bbf6e5c239aa5734631bc3
[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 public class AbstractMappedRpcInvokerTest {
28     @Test
29     public void invokeRpcTest() throws Exception {
30         final Method methodWithInput =
31                 TestRpcService.class.getDeclaredMethod("methodWithInput", RpcService.class, DataObject.class);
32
33         methodWithInput.setAccessible(true);
34
35         final RpcService rpcService = new TestRpcService();
36
37         final TestRpcInvokerImpl testRpcInvoker =
38                 new TestRpcInvokerImpl(ImmutableMap.of(
39                         "(test)tstWithInput", methodWithInput));
40
41         assertTrue(testRpcInvoker.map.get("(test)tstWithInput") instanceof RpcMethodInvoker);
42
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());
50     }
51
52     private static class TestRpcInvokerImpl extends AbstractMappedRpcInvoker<String> {
53         TestRpcInvokerImpl(final Map<String, Method> map) {
54             super(map);
55         }
56
57         @Override
58         protected String qnameToKey(final QName qname) {
59             return qname.toString();
60         }
61     }
62
63     static class Crate {
64         private final RpcService rpcService;
65         private final ThreadLocal<Optional<DataObject>> dataObject;
66
67         Crate(final @NonNull RpcService rpcService, final @Nullable DataObject dataObject) {
68             this.rpcService = rpcService;
69             this.dataObject =
70                 ThreadLocal.withInitial(() -> dataObject == null ? Optional.empty() : Optional.of(dataObject));
71         }
72
73         RpcService getRpcService() {
74             return rpcService;
75         }
76
77         Optional<DataObject> getDataObject() {
78             return dataObject.get();
79         }
80     }
81
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));
85         }
86     }
87 }
88