-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathMappedDataLoaderFactory.java
More file actions
123 lines (107 loc) · 4.47 KB
/
MappedDataLoaderFactory.java
File metadata and controls
123 lines (107 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package org.dataloader.fixtures.parameterized;
import org.dataloader.BatchLoaderEnvironment;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderOptions;
import org.dataloader.fixtures.TestKit;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.dataloader.DataLoaderFactory.newMappedDataLoader;
import static org.dataloader.fixtures.TestKit.futureError;
public class MappedDataLoaderFactory implements TestDataLoaderFactory {
@Override
public <K> DataLoader<K, K> idLoader(
DataLoaderOptions options, List<Collection<K>> loadCalls) {
return newMappedDataLoader((keys) -> {
loadCalls.add(new ArrayList<>(keys));
Map<K, K> map = new HashMap<>();
keys.forEach(k -> map.put(k, k));
return completedFuture(map);
}, options);
}
@Override
public <K> DataLoader<K, K> idLoaderWithContext(DataLoaderOptions options, List<Collection<K>> loadCalls, AtomicReference<BatchLoaderEnvironment> environmentREF) {
return newMappedDataLoader((keys, environment) -> {
environmentREF.set(environment);
loadCalls.add(new ArrayList<>(keys));
Map<K, K> map = new HashMap<>();
keys.forEach(k -> map.put(k, k));
return completedFuture(map);
}, options);
}
@Override
public <K> DataLoader<K, K> idLoaderDelayed(
DataLoaderOptions options, List<Collection<K>> loadCalls, Duration delay) {
return newMappedDataLoader(keys -> CompletableFuture.supplyAsync(() -> {
TestKit.snooze(delay.toMillis());
loadCalls.add(new ArrayList<>(keys));
Map<K, K> map = new HashMap<>();
keys.forEach(k -> map.put(k, k));
return map;
}));
}
@Override
public <K> DataLoader<K, K> idLoaderBlowsUps(DataLoaderOptions options, List<Collection<K>> loadCalls) {
return newMappedDataLoader((keys) -> {
loadCalls.add(new ArrayList<>(keys));
return futureError();
}, options);
}
@Override
public <K> DataLoader<K, Object> idLoaderAllExceptions(
DataLoaderOptions options, List<Collection<K>> loadCalls) {
return newMappedDataLoader(keys -> {
loadCalls.add(new ArrayList<>(keys));
Map<K, Object> errorByKey = new HashMap<>();
keys.forEach(k -> errorByKey.put(k, new IllegalStateException("Error")));
return completedFuture(errorByKey);
}, options);
}
@Override
public DataLoader<Integer, Object> idLoaderOddEvenExceptions(
DataLoaderOptions options, List<Collection<Integer>> loadCalls) {
return newMappedDataLoader(keys -> {
loadCalls.add(new ArrayList<>(keys));
Map<Integer, Object> errorByKey = new HashMap<>();
for (Integer key : keys) {
if (key % 2 == 0) {
errorByKey.put(key, key);
} else {
errorByKey.put(key, new IllegalStateException("Error"));
}
}
return completedFuture(errorByKey);
}, options);
}
@Override
public DataLoader<String, String> onlyReturnsNValues(int N, DataLoaderOptions options, ArrayList<Object> loadCalls) {
return newMappedDataLoader(keys -> {
loadCalls.add(new ArrayList<>(keys));
Map<String, String> collect = List.copyOf(keys).subList(0, N).stream().collect(Collectors.toMap(
k -> k, v -> v
));
return completedFuture(collect);
}, options);
}
@Override
public DataLoader<String, String> idLoaderReturnsTooMany(int howManyMore, DataLoaderOptions options, ArrayList<Object> loadCalls) {
return newMappedDataLoader(keys -> {
loadCalls.add(new ArrayList<>(keys));
List<String> l = new ArrayList<>(keys);
for (int i = 0; i < howManyMore; i++) {
l.add("extra-" + i);
}
Map<String, String> collect = l.stream().collect(Collectors.toMap(
k -> k, v -> v
));
return completedFuture(collect);
}, options);
}
}