2049b97d24fb959e08e125a2cb15cb270b0f0139
[serviceutils.git] / testutils / src / test / java / org / opendaylight / genius / tools / mdsal / rpc / FutureRpcResultsTest.java
1 /*
2  * Copyright (c) 2017 Red Hat, 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.genius.tools.mdsal.rpc;
9
10 import static com.google.common.truth.Truth.assertThat;
11 import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
12 import static com.google.common.util.concurrent.Futures.immediateFuture;
13 import static org.opendaylight.genius.tools.mdsal.rpc.FutureRpcResults.LogLevel.NONE;
14
15 import com.google.common.truth.Truth;
16 import com.google.common.util.concurrent.Futures;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.atomic.AtomicBoolean;
19 import org.junit.Rule;
20 import org.junit.Test;
21 import org.opendaylight.genius.tools.mdsal.rpc.FutureRpcResults.LogLevel;
22 import org.opendaylight.genius.tools.mdsal.testutils.TestFutureRpcResults;
23 import org.opendaylight.infrautils.testutils.LogCaptureRule;
24 import org.opendaylight.infrautils.testutils.LogRule;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Unit Test for {@link FutureRpcResults}.
31  *
32  * @author Michael Vorburger.ch
33  */
34 public class FutureRpcResultsTest {
35
36     private static final Logger LOG = LoggerFactory.getLogger(FutureRpcResultsTest.class);
37
38     public @Rule LogRule logRule = new LogRule();
39     public @Rule LogCaptureRule logCaptureRule = new LogCaptureRule();
40
41     @Test
42     public void testListenableFutureSuccess() throws Exception {
43         Future<RpcResult<String>> future = FutureRpcResults.fromListenableFuture(
44                 LOG, null, () -> immediateFuture("hello, world")).build();
45         Truth.assertThat(TestFutureRpcResults.getResult(future)).isEqualTo("hello, world");
46     }
47
48     @Test
49     public void testFailedListenableFuture() throws Exception {
50         logCaptureRule.expectError("RPC testFailedListenableFuture() failed; input = null");
51         TestFutureRpcResults.assertRpcErrorCause(FutureRpcResults.fromListenableFuture(LOG, null, () ->
52                 immediateFailedFuture(new IllegalArgumentException("boum"))).build(),
53                     IllegalArgumentException.class, "boum");
54     }
55
56     @Test
57     public void testFromListenableFutureException() throws Exception {
58         logCaptureRule.expectError("RPC testFromListenableFutureException() failed; input = null");
59         TestFutureRpcResults.assertRpcErrorCause(FutureRpcResults.fromListenableFuture(
60             LOG, null, () -> {
61                 throw new IllegalArgumentException("bam");
62             }).build(), IllegalArgumentException.class, "bam");
63     }
64
65     @Test
66     public void testFromListenableFutureExceptionWarnInsteadError() throws Exception {
67         TestFutureRpcResults.assertRpcErrorCause(FutureRpcResults.fromListenableFuture(
68             LOG, "testFromListenableFutureException", null, () -> {
69                 throw new IllegalArgumentException("bam");
70             }).onFailureLogLevel(LogLevel.WARN).build(), IllegalArgumentException.class, "bam");
71     }
72
73     @Test
74     public void testFromListenableFutureExceptionNoLog() throws Exception {
75         TestFutureRpcResults.assertRpcErrorCause(FutureRpcResults.fromListenableFuture(
76             LOG, "testFromListenableFutureException", null, () -> {
77                 throw new IllegalArgumentException("bam");
78             }).onFailureLogLevel(NONE).build(), IllegalArgumentException.class, "bam");
79     }
80
81     @Test
82     public void testFromListenableFutureExceptionAlsoLog() throws Exception {
83         final AtomicBoolean afterLogActionCalled = new AtomicBoolean(false);
84         logCaptureRule.expectError("RPC testFromListenableFutureException() failed; input = null");
85         TestFutureRpcResults.assertRpcErrorCause(FutureRpcResults.fromListenableFuture(
86             LOG, "testFromListenableFutureException", null, () -> {
87                 throw new IllegalArgumentException("bam");
88             }).onFailure(e -> afterLogActionCalled.set(true)).build(), IllegalArgumentException.class, "bam");
89         assertThat(afterLogActionCalled.get()).isTrue();
90     }
91
92     @Test
93     public void testFromListenableFutureExceptionCustomMessage() throws Exception {
94         logCaptureRule.expectError("RPC testFromListenableFutureExceptionCustomMessage() failed; input = null");
95         TestFutureRpcResults.assertRpcErrorCause(FutureRpcResults.fromListenableFuture(LOG, null, () -> {
96             throw new IllegalArgumentException("bam");
97         }).withRpcErrorMessage(e -> "tra la la").build(), IllegalArgumentException.class, "tra la la");
98     }
99
100     @Test(expected = IllegalStateException.class)
101     public void testExtraOnFailureThrowsException() throws Exception {
102         FutureRpcResults.fromListenableFuture(LOG, null, () -> Futures.immediateFuture(null)).onFailure(failure -> {
103         }).onFailure(failure -> {
104         });
105     }
106
107     @Test(expected = IllegalStateException.class)
108     public void testExtraOnSuccessThrowsException() throws Exception {
109         FutureRpcResults.fromListenableFuture(LOG, null, () -> Futures.immediateFuture(null)).onSuccess(result -> {
110         }).onSuccess(result -> {
111         });
112     }
113
114     @Test(expected = IllegalStateException.class)
115     public void testExtraWithRpcErrorMessageThrowsException() throws Exception {
116         FutureRpcResults.fromListenableFuture(LOG, null, () -> Futures.immediateFuture(null)).withRpcErrorMessage(
117             error -> null).withRpcErrorMessage(error -> null);
118     }
119
120 }