ec047ff54d88c8c5bea34a3993e8ff2896c32103
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / LazyDOMRpcResultFutureTest.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;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.anyBoolean;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.doThrow;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.reset;
20 import static org.mockito.Mockito.verify;
21
22 import com.google.common.collect.ImmutableList;
23 import com.google.common.util.concurrent.ListenableFuture;
24 import java.lang.reflect.Field;
25 import java.util.concurrent.TimeUnit;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
29 import org.opendaylight.yangtools.yang.binding.DataContainer;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31
32 public class LazyDOMRpcResultFutureTest {
33
34     private LazyDOMRpcResultFuture lazyDOMRpcResultFuture;
35     private final BindingNormalizedNodeSerializer codec = mock(BindingNormalizedNodeSerializer.class);
36     private final ListenableFuture<RpcResult<?>> future = mock(ListenableFuture.class);
37     private final RpcResult<?> domRpcResult = mock(RpcResult.class);
38
39     @Before
40     public void setUp() throws Exception {
41         lazyDOMRpcResultFuture = LazyDOMRpcResultFuture.create(codec, future);
42         reset(future);
43         doReturn(true).when(future).cancel(anyBoolean());
44         doNothing().when(future).addListener(any(), any());
45
46
47         doReturn(true).when(domRpcResult).isSuccessful();
48         doReturn(true).when(future).isCancelled();
49         doReturn(true).when(future).isDone();
50
51         doReturn(mock(DataContainer.class)).when(domRpcResult).getResult();
52         doReturn(domRpcResult).when(future).get();
53         doReturn(domRpcResult).when(future).get(1, TimeUnit.SECONDS);
54     }
55
56     @Test
57     public void basicTest() throws Exception {
58         assertNotNull(lazyDOMRpcResultFuture);
59         assertEquals(future, lazyDOMRpcResultFuture.getBindingFuture());
60
61         lazyDOMRpcResultFuture.cancel(true);
62         verify(future).cancel(anyBoolean());
63
64         lazyDOMRpcResultFuture.addListener(any(), any());
65         verify(future).addListener(any(), any());
66
67         assertTrue(lazyDOMRpcResultFuture.isCancelled() && lazyDOMRpcResultFuture.isDone());
68         assertEquals(lazyDOMRpcResultFuture.get(), lazyDOMRpcResultFuture.get(1, TimeUnit.SECONDS));
69         final Field result = LazyDOMRpcResultFuture.class.getDeclaredField("result");
70         result.setAccessible(true);
71         result.set(lazyDOMRpcResultFuture, null);
72         assertEquals(lazyDOMRpcResultFuture.get(1, TimeUnit.SECONDS), lazyDOMRpcResultFuture.get());
73
74         result.set(lazyDOMRpcResultFuture, null);
75         doReturn(new Object()).when(domRpcResult).getResult();
76         assertNotNull(lazyDOMRpcResultFuture.get());
77
78         result.set(lazyDOMRpcResultFuture, null);
79         doReturn(false).when(domRpcResult).isSuccessful();
80         doReturn(ImmutableList.of()).when(domRpcResult).getErrors();
81         assertNotNull(lazyDOMRpcResultFuture.get());
82     }
83
84     @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch", "checkstyle:avoidHidingCauseException"})
85     @Test(expected = InterruptedException.class)
86     public void checkedGetWithException() throws Throwable {
87         doThrow(InterruptedException.class).when(future).get();
88         try {
89             lazyDOMRpcResultFuture.get();
90         } catch (RuntimeException e) {
91             throw e.getCause();
92         }
93     }
94
95     @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch", "checkstyle:avoidHidingCauseException"})
96     @Test(expected = InterruptedException.class)
97     public void checkedGetWithException2() throws Throwable {
98         doThrow(InterruptedException.class).when(future).get(1, TimeUnit.SECONDS);
99         try {
100             lazyDOMRpcResultFuture.get(1, TimeUnit.SECONDS);
101         } catch (RuntimeException e) {
102             throw e.getCause();
103         }
104     }
105 }