Skip to content

Commit 46ecee3

Browse files
committed
Add some javadocs
1 parent 06dea9d commit 46ecee3

File tree

2 files changed

+109
-16
lines changed

2 files changed

+109
-16
lines changed

tests/src/test/java/net/devh/boot/grpc/test/util/FutureAssertions.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,65 @@
2323
import static org.junit.jupiter.api.Assertions.fail;
2424

2525
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.Future;
2627
import java.util.concurrent.TimeUnit;
2728
import java.util.concurrent.TimeoutException;
2829
import java.util.function.Function;
2930
import java.util.function.UnaryOperator;
3031

31-
import com.google.common.util.concurrent.ListenableFuture;
32-
32+
/**
33+
* Assertions related to {@link Future}s.
34+
*/
3335
public final class FutureAssertions {
3436

35-
public static <T> void assertFutureEquals(final T expected, final ListenableFuture<T> future,
37+
/**
38+
* Asserts that the {@link Future} returns the expected result.
39+
*
40+
* @param <T> The type of the future content.
41+
* @param expected The expected content.
42+
* @param future The future to check for the expected content.
43+
* @param timeout The maximum time to wait for the result.
44+
* @param timeoutUnit The time unit of the {@code timeout} argument.
45+
*/
46+
public static <T> void assertFutureEquals(final T expected, final Future<T> future,
3647
final int timeout, final TimeUnit timeoutUnit) {
3748
assertFutureEquals(expected, future, UnaryOperator.identity(), timeout, timeoutUnit);
3849
}
3950

40-
public static <T, R> void assertFutureEquals(final T expected, final ListenableFuture<R> future,
51+
/**
52+
* Asserts that the {@link Future} returns the expected result.
53+
*
54+
* @param <T> The type of the unwrapped/expected content.
55+
* @param <R> The type of the future content.
56+
* @param expected The expected content.
57+
* @param future The future to check for the expected content.
58+
* @param unwrapper The function used to extract the content.
59+
* @param timeout The maximum time to wait for the result.
60+
* @param timeoutUnit The time unit of the {@code timeout} argument.
61+
*/
62+
public static <T, R> void assertFutureEquals(final T expected, final Future<R> future,
4163
final Function<R, T> unwrapper, final int timeout, final TimeUnit timeoutUnit) {
4264
try {
4365
assertEquals(expected, unwrapper.apply(future.get(timeout, timeoutUnit)));
4466
} catch (InterruptedException | ExecutionException | TimeoutException e) {
45-
fail(e);
67+
fail("Unexpected error while trying to get the result", e);
4668
}
4769
}
4870

71+
/**
72+
* Asserts that the given {@link Future} fails with an {@link ExecutionException} caused by the given exception
73+
* type.
74+
*
75+
* @param <T> The type of the causing exception.
76+
* @param expectedType The expected type of the causing exception.
77+
* @param future The future expected to throw.
78+
* @param timeout The maximum time to wait for the result.
79+
* @param timeoutUnit The time unit of the {@code timeout} argument.
80+
* @return The causing exception.
81+
*/
4982
@SuppressWarnings("unchecked")
5083
public static <T extends Exception> T assertFutureThrows(final Class<T> expectedType,
51-
final ListenableFuture<?> future, final int timeout, final TimeUnit timeoutUnit) {
84+
final Future<?> future, final int timeout, final TimeUnit timeoutUnit) {
5285
final Throwable cause =
5386
assertThrows(ExecutionException.class, () -> future.get(timeout, timeoutUnit)).getCause();
5487
final Class<? extends Throwable> causeClass = cause.getClass();

tests/src/test/java/net/devh/boot/grpc/test/util/GrpcAssertions.java

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,110 @@
2222
import static org.junit.Assert.assertEquals;
2323
import static org.junit.jupiter.api.Assertions.assertThrows;
2424

25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.Future;
2527
import java.util.concurrent.TimeUnit;
2628
import java.util.function.Function;
2729
import java.util.function.UnaryOperator;
2830

31+
import org.junit.jupiter.api.Assertions;
2932
import org.junit.jupiter.api.function.Executable;
3033

31-
import com.google.common.util.concurrent.ListenableFuture;
32-
3334
import io.grpc.Status;
3435
import io.grpc.StatusRuntimeException;
3536
import io.grpc.internal.testing.StreamRecorder;
3637

38+
/**
39+
* Assertions related to gRPC client calls.
40+
*/
3741
public final class GrpcAssertions {
3842

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+
*/
3952
public static <T> void assertFutureFirstEquals(final T expected, final StreamRecorder<T> responseObserver,
4053
final int timeout, final TimeUnit timeoutUnit) {
4154
assertFutureFirstEquals(expected, responseObserver, UnaryOperator.identity(), timeout, timeoutUnit);
4255
}
4356

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+
*/
4468
public static <T, R> void assertFutureFirstEquals(final T expected, final StreamRecorder<R> responseObserver,
4569
final Function<R, T> unwrapper, final int timeout, final TimeUnit timeoutUnit) {
4670
assertFutureEquals(expected, responseObserver.firstValue(), unwrapper, timeout, timeoutUnit);
4771
}
4872

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) {
5082
final StatusRuntimeException exception = assertThrows(StatusRuntimeException.class, executable);
51-
return assertStatus(code, exception);
83+
return assertStatus(expectedCode, exception);
5284
}
5385

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,
5598
final int timeout, final TimeUnit timeoutUnit) {
56-
return assertFutureThrowsStatus(code, recorder.firstValue(), timeout, timeoutUnit);
99+
return assertFutureThrowsStatus(expectedCode, recorder.firstValue(), timeout, timeoutUnit);
57100
}
58101

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,
60113
final int timeout, final TimeUnit timeoutUnit) {
61114
final StatusRuntimeException exception =
62115
assertFutureThrows(StatusRuntimeException.class, future, timeout, timeoutUnit);
63-
return assertStatus(code, exception);
116+
return assertStatus(expectedCode, exception);
64117
}
65118

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) {
67127
final Status status = exception.getStatus();
68-
assertEquals(code, status.getCode());
128+
assertEquals(expectedCode, status.getCode());
69129
return status;
70130
}
71131

0 commit comments

Comments
 (0)