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