2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.adapter;
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;
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.RpcOutput;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
32 public class LazyDOMRpcResultFutureTest {
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);
40 public void setUp() throws Exception {
41 lazyDOMRpcResultFuture = LazyDOMRpcResultFuture.create(codec, future);
43 doReturn(true).when(future).cancel(anyBoolean());
44 doNothing().when(future).addListener(any(), any());
47 doReturn(true).when(domRpcResult).isSuccessful();
48 doReturn(true).when(future).isCancelled();
49 doReturn(true).when(future).isDone();
51 doReturn(mock(RpcOutput.class)).when(domRpcResult).getResult();
52 doReturn(domRpcResult).when(future).get();
53 doReturn(domRpcResult).when(future).get(1, TimeUnit.SECONDS);
57 public void basicTest() throws Exception {
58 assertNotNull(lazyDOMRpcResultFuture);
59 assertEquals(future, lazyDOMRpcResultFuture.getBindingFuture());
61 lazyDOMRpcResultFuture.cancel(true);
62 verify(future).cancel(anyBoolean());
64 lazyDOMRpcResultFuture.addListener(any(), any());
65 verify(future).addListener(any(), any());
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());
74 result.set(lazyDOMRpcResultFuture, null);
75 doReturn(new Object()).when(domRpcResult).getResult();
76 assertNotNull(lazyDOMRpcResultFuture.get());
78 result.set(lazyDOMRpcResultFuture, null);
79 doReturn(false).when(domRpcResult).isSuccessful();
80 doReturn(ImmutableList.of()).when(domRpcResult).getErrors();
81 assertNotNull(lazyDOMRpcResultFuture.get());
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();
89 lazyDOMRpcResultFuture.get();
90 } catch (RuntimeException e) {
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);
100 lazyDOMRpcResultFuture.get(1, TimeUnit.SECONDS);
101 } catch (RuntimeException e) {