|
| 1 | +package org.dataloader; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | +import java.util.concurrent.CompletableFuture; |
| 7 | +import java.util.concurrent.CompletionStage; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +import static java.util.Arrays.asList; |
| 11 | +import static org.hamcrest.Matchers.equalTo; |
| 12 | +import static org.junit.Assert.assertThat; |
| 13 | + |
| 14 | +/** |
| 15 | + * Tests related to context. DataLoaderTest is getting to big and needs refactoring |
| 16 | + */ |
| 17 | +public class DataLoaderContextTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void context_is_passed_to_batch_loader_function() throws Exception { |
| 21 | + BatchLoader<String, String> batchLoader = new BatchLoader<String, String>() { |
| 22 | + @Override |
| 23 | + public CompletionStage<List<String>> load(List<String> keys) { |
| 24 | + throw new UnsupportedOperationException("this wont be called"); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public CompletionStage<List<String>> load(List<String> keys, Object context) { |
| 29 | + List<String> list = keys.stream().map(k -> k + "-" + context).collect(Collectors.toList()); |
| 30 | + return CompletableFuture.completedFuture(list); |
| 31 | + } |
| 32 | + }; |
| 33 | + DataLoaderOptions options = DataLoaderOptions.newOptions() |
| 34 | + .setBatchContextProvider(() -> "ctx"); |
| 35 | + DataLoader<String, String> loader = new DataLoader<>(batchLoader, options); |
| 36 | + |
| 37 | + loader.load("A"); |
| 38 | + loader.load("B"); |
| 39 | + loader.loadMany(asList("C", "D")); |
| 40 | + |
| 41 | + List<String> results = loader.dispatchAndJoin(); |
| 42 | + |
| 43 | + assertThat(results, equalTo(asList("A-ctx", "B-ctx", "C-ctx", "D-ctx"))); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void null_is_passed_as_context_if_you_do_nothing() throws Exception { |
| 48 | + BatchLoader<String, String> batchLoader = CompletableFuture::completedFuture; |
| 49 | + DataLoader<String, String> loader = new DataLoader<>(batchLoader); |
| 50 | + |
| 51 | + loader.load("A"); |
| 52 | + loader.load("B"); |
| 53 | + loader.loadMany(asList("C", "D")); |
| 54 | + |
| 55 | + List<String> results = loader.dispatchAndJoin(); |
| 56 | + |
| 57 | + assertThat(results, equalTo(asList("A", "B", "C", "D"))); |
| 58 | + } |
| 59 | +} |
0 commit comments