|
22 | 22 | import static org.junit.Assert.assertEquals;
|
23 | 23 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
24 | 24 |
|
| 25 | +import java.util.concurrent.ExecutionException; |
| 26 | +import java.util.concurrent.Future; |
25 | 27 | import java.util.concurrent.TimeUnit;
|
26 | 28 | import java.util.function.Function;
|
27 | 29 | import java.util.function.UnaryOperator;
|
28 | 30 |
|
| 31 | +import org.junit.jupiter.api.Assertions; |
29 | 32 | import org.junit.jupiter.api.function.Executable;
|
30 | 33 |
|
31 |
| -import com.google.common.util.concurrent.ListenableFuture; |
32 |
| - |
33 | 34 | import io.grpc.Status;
|
34 | 35 | import io.grpc.StatusRuntimeException;
|
35 | 36 | import io.grpc.internal.testing.StreamRecorder;
|
36 | 37 |
|
| 38 | +/** |
| 39 | + * Assertions related to gRPC client calls. |
| 40 | + */ |
37 | 41 | public final class GrpcAssertions {
|
38 | 42 |
|
| 43 | + /** |
| 44 | + * Asserts that the first value in the {@link StreamRecorder} equals the expected value. |
| 45 | + * |
| 46 | + * @param <T> The type of the observer's content. |
| 47 | + * @param expected The expected content. |
| 48 | + * @param responseObserver The observer to check for the expected content. |
| 49 | + * @param timeout The maximum time to wait for the result. |
| 50 | + * @param timeoutUnit The time unit of the {@code timeout} argument. |
| 51 | + */ |
39 | 52 | public static <T> void assertFutureFirstEquals(final T expected, final StreamRecorder<T> responseObserver,
|
40 | 53 | final int timeout, final TimeUnit timeoutUnit) {
|
41 | 54 | assertFutureFirstEquals(expected, responseObserver, UnaryOperator.identity(), timeout, timeoutUnit);
|
42 | 55 | }
|
43 | 56 |
|
| 57 | + /** |
| 58 | + * Asserts that the first value in the {@link StreamRecorder} equals the expected value. |
| 59 | + * |
| 60 | + * @param <T> The type of the unwrapped/expected content. |
| 61 | + * @param <R> The type of the observer's content. |
| 62 | + * @param expected The expected content. |
| 63 | + * @param responseObserver The observer to check for the expected content. |
| 64 | + * @param unwrapper The function used to extract the content. |
| 65 | + * @param timeout The maximum time to wait for the result. |
| 66 | + * @param timeoutUnit The time unit of the {@code timeout} argument. |
| 67 | + */ |
44 | 68 | public static <T, R> void assertFutureFirstEquals(final T expected, final StreamRecorder<R> responseObserver,
|
45 | 69 | final Function<R, T> unwrapper, final int timeout, final TimeUnit timeoutUnit) {
|
46 | 70 | assertFutureEquals(expected, responseObserver.firstValue(), unwrapper, timeout, timeoutUnit);
|
47 | 71 | }
|
48 | 72 |
|
49 |
| - public static Status assertThrowsStatus(final Status.Code code, final Executable executable) { |
| 73 | + /** |
| 74 | + * Assert that the given {@link Executable} throws a {@link StatusRuntimeException} with the expected status code. |
| 75 | + * |
| 76 | + * @param expectedCode The expected status code. |
| 77 | + * @param executable The executable to run. |
| 78 | + * @return The status contained in the exception. |
| 79 | + * @see Assertions#assertThrows(Class, Executable) |
| 80 | + */ |
| 81 | + public static Status assertThrowsStatus(final Status.Code expectedCode, final Executable executable) { |
50 | 82 | final StatusRuntimeException exception = assertThrows(StatusRuntimeException.class, executable);
|
51 |
| - return assertStatus(code, exception); |
| 83 | + return assertStatus(expectedCode, exception); |
52 | 84 | }
|
53 | 85 |
|
54 |
| - public static Status assertFutureThrowsStatus(final Status.Code code, final StreamRecorder<?> recorder, |
| 86 | + /** |
| 87 | + * Asserts that the given {@link StreamRecorder} throws an {@link ExecutionException} caused by a |
| 88 | + * {@link StatusRuntimeException} with the expected status code. |
| 89 | + * |
| 90 | + * @param expectedCode The expected status code. |
| 91 | + * @param recorder The recorder expected to throw. |
| 92 | + * @param timeout The maximum time to wait for the result. |
| 93 | + * @param timeoutUnit The time unit of the {@code timeout} argument. |
| 94 | + * @return The status contained in the exception. |
| 95 | + * @see #assertFutureThrowsStatus(io.grpc.Status.Code, Future, int, TimeUnit) |
| 96 | + */ |
| 97 | + public static Status assertFutureThrowsStatus(final Status.Code expectedCode, final StreamRecorder<?> recorder, |
55 | 98 | final int timeout, final TimeUnit timeoutUnit) {
|
56 |
| - return assertFutureThrowsStatus(code, recorder.firstValue(), timeout, timeoutUnit); |
| 99 | + return assertFutureThrowsStatus(expectedCode, recorder.firstValue(), timeout, timeoutUnit); |
57 | 100 | }
|
58 | 101 |
|
59 |
| - public static Status assertFutureThrowsStatus(final Status.Code code, final ListenableFuture<?> future, |
| 102 | + /** |
| 103 | + * Asserts that the given {@link Future} throws an {@link ExecutionException} caused by a |
| 104 | + * {@link StatusRuntimeException} with the expected status code. |
| 105 | + * |
| 106 | + * @param expectedCode The expected status code. |
| 107 | + * @param future The future expected to throw. |
| 108 | + * @param timeout The maximum time to wait for the result. |
| 109 | + * @param timeoutUnit The time unit of the {@code timeout} argument. |
| 110 | + * @return The status contained in the exception. |
| 111 | + */ |
| 112 | + public static Status assertFutureThrowsStatus(final Status.Code expectedCode, final Future<?> future, |
60 | 113 | final int timeout, final TimeUnit timeoutUnit) {
|
61 | 114 | final StatusRuntimeException exception =
|
62 | 115 | assertFutureThrows(StatusRuntimeException.class, future, timeout, timeoutUnit);
|
63 |
| - return assertStatus(code, exception); |
| 116 | + return assertStatus(expectedCode, exception); |
64 | 117 | }
|
65 | 118 |
|
66 |
| - public static Status assertStatus(final Status.Code code, final StatusRuntimeException exception) { |
| 119 | + /** |
| 120 | + * Asserts that the given {@link StatusRuntimeException} uses the expected status code. |
| 121 | + * |
| 122 | + * @param expectedCode The expected status code. |
| 123 | + * @param exception The exception to check for the status code. |
| 124 | + * @return The status contained in the exception. |
| 125 | + */ |
| 126 | + public static Status assertStatus(final Status.Code expectedCode, final StatusRuntimeException exception) { |
67 | 127 | final Status status = exception.getStatus();
|
68 |
| - assertEquals(code, status.getCode()); |
| 128 | + assertEquals(expectedCode, status.getCode()); |
69 | 129 | return status;
|
70 | 130 | }
|
71 | 131 |
|
|
0 commit comments