1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.forgerock.util.test.assertj;
18
19 import java.util.concurrent.ExecutionException;
20
21 import org.assertj.core.api.AbstractAssert;
22 import org.assertj.core.api.AbstractThrowableAssert;
23 import org.assertj.core.api.Assertions;
24 import org.forgerock.util.promise.Promise;
25
26
27
28
29
30
31
32
33 public abstract class AbstractAssertJPromiseAssert<T, A extends AbstractAssertJPromiseAssert<T, A, S>, S extends AbstractAssert<S, T>>
34 extends AbstractAssert<A, Promise<T, ?>> {
35
36
37
38
39
40
41 protected AbstractAssertJPromiseAssert(Promise<T, ?> promise, Class<A> type) {
42 super(promise, type);
43 }
44
45
46
47
48
49
50 protected abstract S createSucceededAssert(T actual);
51
52
53
54
55
56 public final S succeeded() {
57 isNotNull();
58 if (!actual.isDone()) {
59 failWithMessage("Promise is not completed");
60 }
61 T result = null;
62 try {
63 result = actual.get();
64 } catch (InterruptedException e) {
65 failWithMessage("Promise was interrupted");
66 } catch (ExecutionException e) {
67 failWithMessage("Promise failed: <%s>", e.getCause());
68 }
69 return createSucceededAssert(result);
70 }
71
72
73
74
75
76
77 public final AbstractThrowableAssert<?, ? extends Throwable> failedWithException() {
78 isNotNull();
79 try {
80 Object value = actual.get();
81 failWithMessage("Promise succeeded with value <%s>", value);
82 } catch (InterruptedException e) {
83 failWithMessage("Promise was interrupted");
84 } catch (ExecutionException e) {
85 return Assertions.assertThat(e.getCause());
86 }
87 throw new IllegalStateException("Shouldn't have reached here");
88 }
89
90 }