2 * Copyright (c) 2017 Red Hat, 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.genius.tools.mdsal.rpc;
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;
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;
30 * Unit Test for {@link FutureRpcResults}.
32 * @author Michael Vorburger.ch
34 public class FutureRpcResultsTest {
36 private static final Logger LOG = LoggerFactory.getLogger(FutureRpcResultsTest.class);
38 public @Rule LogRule logRule = new LogRule();
39 public @Rule LogCaptureRule logCaptureRule = new LogCaptureRule();
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");
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");
57 public void testFromListenableFutureException() throws Exception {
58 logCaptureRule.expectError("RPC testFromListenableFutureException() failed; input = null");
59 TestFutureRpcResults.assertRpcErrorCause(FutureRpcResults.fromListenableFuture(
61 throw new IllegalArgumentException("bam");
62 }).build(), IllegalArgumentException.class, "bam");
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");
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");
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();
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");
100 @Test(expected = IllegalStateException.class)
101 public void testExtraOnFailureThrowsException() throws Exception {
102 FutureRpcResults.fromListenableFuture(LOG, null, () -> Futures.immediateFuture(null)).onFailure(failure -> {
103 }).onFailure(failure -> {
107 @Test(expected = IllegalStateException.class)
108 public void testExtraOnSuccessThrowsException() throws Exception {
109 FutureRpcResults.fromListenableFuture(LOG, null, () -> Futures.immediateFuture(null)).onSuccess(result -> {
110 }).onSuccess(result -> {
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);