-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathDataLoaderOptionsTest.java
More file actions
230 lines (191 loc) · 10 KB
/
DataLoaderOptionsTest.java
File metadata and controls
230 lines (191 loc) · 10 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package org.dataloader;
import org.dataloader.impl.DefaultCacheMap;
import org.dataloader.impl.NoOpValueCache;
import org.dataloader.instrumentation.DataLoaderInstrumentation;
import org.dataloader.scheduler.BatchLoaderScheduler;
import org.dataloader.stats.NoOpStatisticsCollector;
import org.dataloader.stats.StatisticsCollector;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import java.util.function.Supplier;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
@SuppressWarnings("OptionalGetWithoutIsPresent")
class DataLoaderOptionsTest {
DataLoaderOptions optionsDefault = new DataLoaderOptions();
@Test
void canCreateDefaultOptions() {
assertThat(optionsDefault.batchingEnabled(), equalTo(true));
assertThat(optionsDefault.cachingEnabled(), equalTo(true));
assertThat(optionsDefault.cachingExceptionsEnabled(), equalTo(true));
assertThat(optionsDefault.maxBatchSize(), equalTo(-1));
assertThat(optionsDefault.getBatchLoaderScheduler(), equalTo(null));
DataLoaderOptions builtOptions = DataLoaderOptions.newOptionsBuilder().build();
assertThat(builtOptions, equalTo(optionsDefault));
assertThat(builtOptions == optionsDefault, equalTo(false));
DataLoaderOptions transformedOptions = optionsDefault.transform(builder -> {
});
assertThat(transformedOptions, equalTo(optionsDefault));
assertThat(transformedOptions == optionsDefault, equalTo(false));
}
@Test
void canCopyOk() {
DataLoaderOptions optionsNext = new DataLoaderOptions(optionsDefault);
assertThat(optionsNext, equalTo(optionsDefault));
assertThat(optionsNext == optionsDefault, equalTo(false));
optionsNext = DataLoaderOptions.newDataLoaderOptions(optionsDefault).build();
assertThat(optionsNext, equalTo(optionsDefault));
assertThat(optionsNext == optionsDefault, equalTo(false));
}
BatchLoaderScheduler testBatchLoaderScheduler = new BatchLoaderScheduler() {
@Override
public <K, V> CompletionStage<List<V>> scheduleBatchLoader(ScheduledBatchLoaderCall<V> scheduledCall, List<K> keys, BatchLoaderEnvironment environment) {
return null;
}
@Override
public <K, V> CompletionStage<Map<K, V>> scheduleMappedBatchLoader(ScheduledMappedBatchLoaderCall<K, V> scheduledCall, List<K> keys, BatchLoaderEnvironment environment) {
return null;
}
@Override
public <K> void scheduleBatchPublisher(ScheduledBatchPublisherCall scheduledCall, List<K> keys, BatchLoaderEnvironment environment) {
}
};
BatchLoaderContextProvider testBatchLoaderContextProvider = () -> null;
CacheMap<Object, Object> testCacheMap = new DefaultCacheMap<>();
ValueCache<Object, Object> testValueCache = new NoOpValueCache<>();
CacheKey<Object> testCacheKey = new CacheKey<Object>() {
@Override
public Object getKey(Object input) {
return null;
}
};
ValueCacheOptions testValueCacheOptions = ValueCacheOptions.newOptions();
NoOpStatisticsCollector noOpStatisticsCollector = new NoOpStatisticsCollector();
Supplier<StatisticsCollector> testStatisticsCollectorSupplier = () -> noOpStatisticsCollector;
@Test
void canBuildOk() {
assertThat(optionsDefault.setBatchingEnabled(false).batchingEnabled(),
equalTo(false));
assertThat(optionsDefault.setBatchLoaderScheduler(testBatchLoaderScheduler).getBatchLoaderScheduler(),
equalTo(testBatchLoaderScheduler));
assertThat(optionsDefault.setBatchLoaderContextProvider(testBatchLoaderContextProvider).getBatchLoaderContextProvider(),
equalTo(testBatchLoaderContextProvider));
assertThat(optionsDefault.setCacheMap(testCacheMap).cacheMap().get(),
equalTo(testCacheMap));
assertThat(optionsDefault.setCachingEnabled(false).cachingEnabled(),
equalTo(false));
assertThat(optionsDefault.setValueCacheOptions(testValueCacheOptions).getValueCacheOptions(),
equalTo(testValueCacheOptions));
assertThat(optionsDefault.setCacheKeyFunction(testCacheKey).cacheKeyFunction().get(),
equalTo(testCacheKey));
assertThat(optionsDefault.setValueCache(testValueCache).valueCache().get(),
equalTo(testValueCache));
assertThat(optionsDefault.setMaxBatchSize(10).maxBatchSize(),
equalTo(10));
assertThat(optionsDefault.setStatisticsCollector(testStatisticsCollectorSupplier).getStatisticsCollector(),
equalTo(testStatisticsCollectorSupplier.get()));
DataLoaderOptions builtOptions = optionsDefault.transform(builder -> {
builder.setBatchingEnabled(false);
builder.setCachingExceptionsEnabled(false);
builder.setCachingEnabled(false);
builder.setBatchLoaderScheduler(testBatchLoaderScheduler);
builder.setBatchLoaderContextProvider(testBatchLoaderContextProvider);
builder.setCacheMap(testCacheMap);
builder.setValueCache(testValueCache);
builder.setCacheKeyFunction(testCacheKey);
builder.setValueCacheOptions(testValueCacheOptions);
builder.setMaxBatchSize(10);
builder.setStatisticsCollector(testStatisticsCollectorSupplier);
});
assertThat(builtOptions.batchingEnabled(),
equalTo(false));
assertThat(builtOptions.getBatchLoaderScheduler(),
equalTo(testBatchLoaderScheduler));
assertThat(builtOptions.getBatchLoaderContextProvider(),
equalTo(testBatchLoaderContextProvider));
assertThat(builtOptions.cacheMap().get(),
equalTo(testCacheMap));
assertThat(builtOptions.cachingEnabled(),
equalTo(false));
assertThat(builtOptions.getValueCacheOptions(),
equalTo(testValueCacheOptions));
assertThat(builtOptions.cacheKeyFunction().get(),
equalTo(testCacheKey));
assertThat(builtOptions.valueCache().get(),
equalTo(testValueCache));
assertThat(builtOptions.maxBatchSize(),
equalTo(10));
assertThat(builtOptions.getStatisticsCollector(),
equalTo(testStatisticsCollectorSupplier.get()));
}
@Test
void canBuildViaBuilderOk() {
DataLoaderOptions.Builder builder = DataLoaderOptions.newOptionsBuilder();
builder.setBatchingEnabled(false);
builder.setCachingExceptionsEnabled(false);
builder.setCachingEnabled(false);
builder.setBatchLoaderScheduler(testBatchLoaderScheduler);
builder.setBatchLoaderContextProvider(testBatchLoaderContextProvider);
builder.setCacheMap(testCacheMap);
builder.setValueCache(testValueCache);
builder.setCacheKeyFunction(testCacheKey);
builder.setValueCacheOptions(testValueCacheOptions);
builder.setMaxBatchSize(10);
builder.setStatisticsCollector(testStatisticsCollectorSupplier);
DataLoaderOptions builtOptions = builder.build();
assertThat(builtOptions.batchingEnabled(),
equalTo(false));
assertThat(builtOptions.getBatchLoaderScheduler(),
equalTo(testBatchLoaderScheduler));
assertThat(builtOptions.getBatchLoaderContextProvider(),
equalTo(testBatchLoaderContextProvider));
assertThat(builtOptions.cacheMap().get(),
equalTo(testCacheMap));
assertThat(builtOptions.cachingEnabled(),
equalTo(false));
assertThat(builtOptions.getValueCacheOptions(),
equalTo(testValueCacheOptions));
assertThat(builtOptions.cacheKeyFunction().get(),
equalTo(testCacheKey));
assertThat(builtOptions.valueCache().get(),
equalTo(testValueCache));
assertThat(builtOptions.maxBatchSize(),
equalTo(10));
assertThat(builtOptions.getStatisticsCollector(),
equalTo(testStatisticsCollectorSupplier.get()));
}
@Test
void canCopyExistingOptionValuesOnTransform() {
DataLoaderInstrumentation instrumentation1 = new DataLoaderInstrumentation() {
};
DataLoaderInstrumentation instrumentation2 = new DataLoaderInstrumentation() {
};
BatchLoaderContextProvider contextProvider1 = () -> null;
DataLoaderOptions startingOptions = DataLoaderOptions.newOptionsBuilder().setBatchingEnabled(false)
.setCachingEnabled(false)
.setInstrumentation(instrumentation1)
.setBatchLoaderContextProvider(contextProvider1)
.build();
assertThat(startingOptions.batchingEnabled(), equalTo(false));
assertThat(startingOptions.cachingEnabled(), equalTo(false));
assertThat(startingOptions.getInstrumentation(), equalTo(instrumentation1));
assertThat(startingOptions.getBatchLoaderContextProvider(), equalTo(contextProvider1));
DataLoaderOptions newOptions = startingOptions.transform(builder ->
builder.setBatchingEnabled(true).setInstrumentation(instrumentation2));
// immutable
assertThat(newOptions, CoreMatchers.not(startingOptions));
assertThat(startingOptions.batchingEnabled(), equalTo(false));
assertThat(startingOptions.cachingEnabled(), equalTo(false));
assertThat(startingOptions.getInstrumentation(), equalTo(instrumentation1));
assertThat(startingOptions.getBatchLoaderContextProvider(), equalTo(contextProvider1));
// stayed the same
assertThat(newOptions.cachingEnabled(), equalTo(false));
assertThat(newOptions.getBatchLoaderContextProvider(), equalTo(contextProvider1));
// was changed
assertThat(newOptions.batchingEnabled(), equalTo(true));
assertThat(newOptions.getInstrumentation(), equalTo(instrumentation2));
}
}