|
| 1 | +package org.jooby.internal.mapper; |
| 2 | + |
| 3 | +import static org.easymock.EasyMock.expect; |
| 4 | + |
| 5 | +import java.util.concurrent.CompletableFuture; |
| 6 | +import java.util.function.BiConsumer; |
| 7 | + |
| 8 | +import org.jooby.Deferred; |
| 9 | +import org.jooby.Deferred.Initializer0; |
| 10 | +import org.jooby.test.MockUnit; |
| 11 | +import org.jooby.test.MockUnit.Block; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.runner.RunWith; |
| 14 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 15 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 16 | + |
| 17 | +@RunWith(PowerMockRunner.class) |
| 18 | +@PrepareForTest({CompletableFutureMapper.class, Deferred.class }) |
| 19 | +public class CompletableFutureMapperTest { |
| 20 | + |
| 21 | + private Block deferred = unit -> { |
| 22 | + Deferred deferred = unit.constructor(Deferred.class) |
| 23 | + .args(Deferred.Initializer0.class) |
| 24 | + .build(unit.capture(Deferred.Initializer0.class)); |
| 25 | + unit.registerMock(Deferred.class, deferred); |
| 26 | + }; |
| 27 | + |
| 28 | + @SuppressWarnings({"unchecked", "rawtypes" }) |
| 29 | + private Block future = unit -> { |
| 30 | + CompletableFuture future = unit.get(CompletableFuture.class); |
| 31 | + expect(future.whenComplete(unit.capture(BiConsumer.class))).andReturn(future); |
| 32 | + }; |
| 33 | + |
| 34 | + private Block init0 = unit -> { |
| 35 | + Initializer0 next = unit.captured(Deferred.Initializer0.class).iterator().next(); |
| 36 | + next.run(unit.get(Deferred.class)); |
| 37 | + }; |
| 38 | + |
| 39 | + @SuppressWarnings({"rawtypes", "unchecked" }) |
| 40 | + @Test |
| 41 | + public void resolve() throws Exception { |
| 42 | + Object value = new Object(); |
| 43 | + new MockUnit(CompletableFuture.class) |
| 44 | + .expect(deferred) |
| 45 | + .expect(future) |
| 46 | + .expect(unit -> { |
| 47 | + Deferred deferred = unit.get(Deferred.class); |
| 48 | + deferred.resolve(value); |
| 49 | + }) |
| 50 | + .run(unit -> { |
| 51 | + new CompletableFutureMapper() |
| 52 | + .map(unit.get(CompletableFuture.class)); |
| 53 | + }, init0, unit -> { |
| 54 | + BiConsumer next = unit.captured(BiConsumer.class).iterator().next(); |
| 55 | + next.accept(value, null); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + @SuppressWarnings({"rawtypes", "unchecked" }) |
| 60 | + @Test |
| 61 | + public void reject() throws Exception { |
| 62 | + Throwable value = new Throwable(); |
| 63 | + new MockUnit(CompletableFuture.class) |
| 64 | + .expect(deferred) |
| 65 | + .expect(future) |
| 66 | + .expect(unit -> { |
| 67 | + Deferred deferred = unit.get(Deferred.class); |
| 68 | + deferred.reject(value); |
| 69 | + }) |
| 70 | + .run(unit -> { |
| 71 | + new CompletableFutureMapper() |
| 72 | + .map(unit.get(CompletableFuture.class)); |
| 73 | + }, init0, unit -> { |
| 74 | + BiConsumer next = unit.captured(BiConsumer.class).iterator().next(); |
| 75 | + next.accept(null, value); |
| 76 | + }); |
| 77 | + } |
| 78 | +} |
0 commit comments