/
BackendMock.java
494 lines (414 loc) · 19.3 KB
/
BackendMock.java
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.util.impl.integrationtest.common.rule;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.hibernate.search.engine.backend.common.DocumentReference;
import org.hibernate.search.engine.backend.spi.BackendBuildContext;
import org.hibernate.search.engine.backend.work.execution.DocumentCommitStrategy;
import org.hibernate.search.engine.backend.work.execution.DocumentRefreshStrategy;
import org.hibernate.search.util.common.AssertionFailure;
import org.hibernate.search.util.common.logging.impl.Log;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;
import org.hibernate.search.util.impl.integrationtest.common.stub.backend.document.StubDocumentNode;
import org.hibernate.search.util.impl.integrationtest.common.stub.backend.document.model.StubIndexSchemaNode;
import org.hibernate.search.util.impl.integrationtest.common.stub.backend.index.StubDocumentWork;
import org.hibernate.search.util.impl.integrationtest.common.stub.backend.index.StubIndexScaleWork;
import org.hibernate.search.util.impl.integrationtest.common.stub.backend.index.StubSchemaManagementWork;
import org.hibernate.search.util.impl.integrationtest.common.stub.backend.index.impl.StubBackendFactory;
import org.hibernate.search.util.impl.integrationtest.common.stub.backend.search.StubSearchWork;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public class BackendMock implements TestRule {
private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );
private final VerifyingStubBackendBehavior backendBehavior = new VerifyingStubBackendBehavior();
private boolean started = false;
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
started = true;
try {
base.evaluate();
verifyExpectationsMet();
}
finally {
resetExpectations();
started = false;
}
}
};
}
public BackendMock ignoreSchema() {
backendBehavior.ignoreSchema( true );
return this;
}
public StubBackendFactory factory() {
return new StubBackendFactory( backendBehavior );
}
public void resetExpectations() {
backendBehavior().resetExpectations();
}
public void verifyExpectationsMet() {
backendBehavior().verifyExpectationsMet();
}
public void inLenientMode(Runnable action) {
backendBehavior().lenient( true );
try {
action.run();
}
finally {
backendBehavior().lenient( false );
}
}
public BackendMock onCreate(Consumer<BackendBuildContext> behavior) {
backendBehavior().addCreateBackendBehavior( context -> {
behavior.accept( context );
return null;
} );
return this;
}
public BackendMock onStop(Runnable behavior) {
backendBehavior().addStopBackendBehavior( () -> {
behavior.run();
return null;
} );
return this;
}
public BackendMock expectFailingField(String indexName, String absoluteFieldPath,
Supplier<RuntimeException> exceptionSupplier) {
backendBehavior().setIndexFieldAddBehavior( indexName, absoluteFieldPath, () -> {
throw exceptionSupplier.get();
} );
return this;
}
public BackendMock expectSchema(String indexName, Consumer<StubIndexSchemaNode.Builder> contributor) {
return expectSchema( indexName, contributor, ignored -> { } );
}
public BackendMock expectSchema(String indexName, Consumer<StubIndexSchemaNode.Builder> contributor,
Consumer<StubIndexSchemaNode> capture) {
CallQueue<SchemaDefinitionCall> callQueue = backendBehavior().getSchemaDefinitionCalls( indexName );
StubIndexSchemaNode.Builder builder = StubIndexSchemaNode.schema();
contributor.accept( builder );
callQueue.expectOutOfOrder( new SchemaDefinitionCall( indexName, builder.build(), capture ) );
return this;
}
public BackendMock expectAnySchema(String indexName) {
CallQueue<SchemaDefinitionCall> callQueue = backendBehavior().getSchemaDefinitionCalls( indexName );
callQueue.expectOutOfOrder( new SchemaDefinitionCall( indexName, null ) );
return this;
}
public SchemaManagementWorkCallListContext expectSchemaManagementWorks(String indexName) {
CallQueue<SchemaManagementWorkCall> callQueue = backendBehavior().getSchemaManagementWorkCalls( indexName );
return new SchemaManagementWorkCallListContext(
indexName,
callQueue::expectInOrder
);
}
public DocumentWorkCallListContext expectWorks(String indexName) {
return expectWorks( indexName, null );
}
public DocumentWorkCallListContext expectWorks(String indexName, String tenantId) {
// Default to force commit and no refresh, which is what the mapper should use by default
return expectWorks( indexName, tenantId, DocumentCommitStrategy.FORCE, DocumentRefreshStrategy.NONE );
}
public DocumentWorkCallListContext expectWorks(String indexName,
DocumentCommitStrategy commitStrategy, DocumentRefreshStrategy refreshStrategy) {
return expectWorks( indexName, null, commitStrategy, refreshStrategy );
}
public DocumentWorkCallListContext expectWorks(String indexName, String tenantId,
DocumentCommitStrategy commitStrategy, DocumentRefreshStrategy refreshStrategy) {
CallQueue<DocumentWorkCall> callQueue = backendBehavior().getDocumentWorkCalls( indexName );
return new DocumentWorkCallListContext(
indexName, tenantId,
commitStrategy, refreshStrategy,
callQueue::expectInOrder
);
}
public DocumentWorkCallListContext expectWorksAnyOrder(String indexName,
DocumentCommitStrategy commitStrategy,
DocumentRefreshStrategy refreshStrategy) {
CallQueue<DocumentWorkCall> callQueue = backendBehavior().getDocumentWorkCalls( indexName );
return new DocumentWorkCallListContext(
indexName, null,
commitStrategy, refreshStrategy,
callQueue::expectOutOfOrder
);
}
public IndexScaleWorkCallListContext expectIndexScaleWorks(String indexName) {
return expectIndexScaleWorks( indexName , null );
}
public IndexScaleWorkCallListContext expectIndexScaleWorks(String indexName, String tenantId) {
CallQueue<IndexScaleWorkCall> callQueue = backendBehavior().getIndexScaleWorkCalls( indexName );
return new IndexScaleWorkCallListContext(
indexName, tenantId,
callQueue::expectInOrder
);
}
public BackendMock expectSearchReferences(Collection<String> indexNames, Consumer<StubSearchWork.Builder> contributor,
StubSearchWorkBehavior<DocumentReference> behavior) {
return expectSearch( indexNames, contributor, StubSearchWork.ResultType.REFERENCES, behavior );
}
public BackendMock expectSearchObjects(String indexName, StubSearchWorkBehavior<DocumentReference> behavior) {
return expectSearch( Collections.singleton( indexName ), ignored -> { },
StubSearchWork.ResultType.OBJECTS, behavior );
}
public BackendMock expectSearchObjects(Collection<String> indexNames, Consumer<StubSearchWork.Builder> contributor,
StubSearchWorkBehavior<DocumentReference> behavior) {
return expectSearch( indexNames, contributor, StubSearchWork.ResultType.OBJECTS, behavior );
}
public BackendMock expectSearchProjections(Collection<String> indexNames, Consumer<StubSearchWork.Builder> contributor,
StubSearchWorkBehavior<List<?>> behavior) {
return expectSearch( indexNames, contributor, StubSearchWork.ResultType.PROJECTIONS, behavior );
}
public BackendMock expectSearchProjection(Collection<String> indexNames, Consumer<StubSearchWork.Builder> contributor,
StubSearchWorkBehavior<?> behavior) {
return expectSearch( indexNames, contributor, StubSearchWork.ResultType.PROJECTIONS, behavior );
}
private BackendMock expectSearch(Collection<String> indexNames, Consumer<StubSearchWork.Builder> contributor,
StubSearchWork.ResultType resultType, StubSearchWorkBehavior<?> behavior) {
CallQueue<SearchWorkCall<?>> callQueue = backendBehavior().getSearchWorkCalls();
StubSearchWork.Builder builder = StubSearchWork.builder( resultType );
contributor.accept( builder );
callQueue.expectInOrder( new SearchWorkCall<>( new LinkedHashSet<>( indexNames ), builder.build(), behavior ) );
return this;
}
public BackendMock expectCount(Collection<String> indexNames, long expectedResult) {
CallQueue<CountWorkCall> callQueue = backendBehavior().getCountWorkCalls();
callQueue.expectInOrder( new CountWorkCall( new LinkedHashSet<>( indexNames ), expectedResult ) );
return this;
}
private VerifyingStubBackendBehavior backendBehavior() {
if ( !started ) {
throw new AssertionFailure( "The backend mock was not configured as a JUnit @Rule/@ClassRule,"
+ " or its statement wrapper hasn't started executing yet,"
+ " or its statement wrapper has finished executing."
+ " Double check the @Rule/@ClassRule annotations and the execution order of rules." );
}
return backendBehavior;
}
public BackendMock expectScrollObjects(Collection<String> indexNames, int chunkSize,
Consumer<StubSearchWork.Builder> contributor) {
return expectScroll( indexNames, contributor, StubSearchWork.ResultType.OBJECTS, chunkSize );
}
public BackendMock expectScrollProjections(Collection<String> indexNames, int chunkSize,
Consumer<StubSearchWork.Builder> contributor) {
return expectScroll( indexNames, contributor, StubSearchWork.ResultType.PROJECTIONS, chunkSize );
}
public BackendMock expectScrollProjection(Collection<String> indexNames, int chunkSize,
Consumer<StubSearchWork.Builder> contributor) {
return expectScroll( indexNames, contributor, StubSearchWork.ResultType.PROJECTIONS, chunkSize );
}
private BackendMock expectScroll(Collection<String> indexNames, Consumer<StubSearchWork.Builder> contributor,
StubSearchWork.ResultType resultType, int chunkSize) {
CallQueue<ScrollWorkCall<?>> callQueue = backendBehavior().getScrollCalls();
StubSearchWork.Builder builder = StubSearchWork.builder( resultType );
contributor.accept( builder );
callQueue.expectInOrder( new ScrollWorkCall<>( new LinkedHashSet<>( indexNames ), builder.build(), chunkSize ) );
return this;
}
public BackendMock expectCloseScroll(Collection<String> indexNames) {
CallQueue<CloseScrollWorkCall> callQueue = backendBehavior().getCloseScrollCalls();
callQueue.expectInOrder( new CloseScrollWorkCall( new LinkedHashSet<>( indexNames ) ) );
return this;
}
public BackendMock expectNextScroll(Collection<String> indexNames, StubNextScrollWorkBehavior<?> behavior) {
CallQueue<NextScrollWorkCall<?>> callQueue = backendBehavior().getNextScrollCalls();
callQueue.expectInOrder( new NextScrollWorkCall<>( new LinkedHashSet<>( indexNames ), behavior ) );
return this;
}
public static class SchemaManagementWorkCallListContext {
private final String indexName;
private final Consumer<SchemaManagementWorkCall> expectationConsumer;
private SchemaManagementWorkCallListContext(String indexName,
Consumer<SchemaManagementWorkCall> expectationConsumer) {
this.indexName = indexName;
this.expectationConsumer = expectationConsumer;
}
public SchemaManagementWorkCallListContext work(StubSchemaManagementWork.Type type) {
return work( type, CompletableFuture.completedFuture( null ) );
}
public SchemaManagementWorkCallListContext work(StubSchemaManagementWork.Type type, CompletableFuture<?> future) {
return work( type, failureCollector -> future );
}
public SchemaManagementWorkCallListContext work(StubSchemaManagementWork.Type type,
SchemaManagementWorkBehavior behavior) {
StubSchemaManagementWork work = StubSchemaManagementWork.builder( type )
.build();
expectationConsumer.accept( new SchemaManagementWorkCall( indexName, work, behavior ) );
return this;
}
}
public static class DocumentWorkCallListContext {
private final String indexName;
private final String tenantId;
private final DocumentCommitStrategy commitStrategyForDocumentWorks;
private final DocumentRefreshStrategy refreshStrategyForDocumentWorks;
private final Consumer<DocumentWorkCall> expectationConsumer;
private final List<StubDocumentWork> works = new ArrayList<>();
private DocumentWorkCallListContext(String indexName, String tenantId,
DocumentCommitStrategy commitStrategyForDocumentWorks,
DocumentRefreshStrategy refreshStrategyForDocumentWorks,
Consumer<DocumentWorkCall> expectationConsumer) {
this.indexName = indexName;
this.tenantId = tenantId;
this.commitStrategyForDocumentWorks = commitStrategyForDocumentWorks;
this.refreshStrategyForDocumentWorks = refreshStrategyForDocumentWorks;
this.expectationConsumer = expectationConsumer;
}
public DocumentWorkCallListContext add(Consumer<StubDocumentWork.Builder> contributor) {
return documentWork( StubDocumentWork.Type.ADD, contributor );
}
public DocumentWorkCallListContext add(String id, Consumer<StubDocumentNode.Builder> documentContributor) {
return documentWork( StubDocumentWork.Type.ADD, id, documentContributor );
}
public DocumentWorkCallListContext addOrUpdate(Consumer<StubDocumentWork.Builder> contributor) {
return documentWork( StubDocumentWork.Type.ADD_OR_UPDATE, contributor );
}
public DocumentWorkCallListContext addOrUpdate(String id, Consumer<StubDocumentNode.Builder> documentContributor) {
return documentWork( StubDocumentWork.Type.ADD_OR_UPDATE, id, documentContributor );
}
public DocumentWorkCallListContext delete(String id) {
return documentWork( StubDocumentWork.Type.DELETE, b -> b.identifier( id ) );
}
public DocumentWorkCallListContext delete(Consumer<StubDocumentWork.Builder> contributor) {
return documentWork( StubDocumentWork.Type.DELETE, contributor );
}
DocumentWorkCallListContext documentWork(StubDocumentWork.Type type,
Consumer<StubDocumentWork.Builder> contributor) {
StubDocumentWork.Builder builder = StubDocumentWork.builder( type );
builder.tenantIdentifier( tenantId );
contributor.accept( builder );
builder.commit( commitStrategyForDocumentWorks );
builder.refresh( refreshStrategyForDocumentWorks );
return work( builder.build() );
}
DocumentWorkCallListContext documentWork(StubDocumentWork.Type type, String id,
Consumer<StubDocumentNode.Builder> documentContributor) {
return documentWork( type, b -> {
b.identifier( id );
StubDocumentNode.Builder documentBuilder = StubDocumentNode.document();
documentContributor.accept( documentBuilder );
b.document( documentBuilder.build() );
} );
}
public DocumentWorkCallListContext work(StubDocumentWork work) {
works.add( work );
return this;
}
public DocumentWorkCallListContext processedThenExecuted(CompletableFuture<?> future) {
log.debugf( "Expecting %d works to be prepared, then executed", works.size() );
// First expect all works to be prepared, then expect all works to be executed
works.stream()
.map( work -> new DocumentWorkCall( indexName, DocumentWorkCall.WorkPhase.PROCESS, work ) )
.forEach( expectationConsumer );
works.stream()
.map( work -> new DocumentWorkCall( indexName, DocumentWorkCall.WorkPhase.EXECUTE, work, future ) )
.forEach( expectationConsumer );
works.clear();
return this;
}
public DocumentWorkCallListContext processedThenExecuted() {
return processedThenExecuted( CompletableFuture.completedFuture( null ) );
}
public DocumentWorkCallListContext executed(CompletableFuture<?> future) {
log.debugf( "Expecting %d works to be executed", works.size() );
works.stream()
.map( work -> new DocumentWorkCall( indexName, DocumentWorkCall.WorkPhase.EXECUTE, work, future ) )
.forEach( expectationConsumer );
works.clear();
return this;
}
public DocumentWorkCallListContext executed() {
return executed( CompletableFuture.completedFuture( null ) );
}
public DocumentWorkCallListContext processed() {
log.debugf( "Expecting %d works to be prepared", works.size() );
works.stream()
.map( work -> new DocumentWorkCall( indexName, DocumentWorkCall.WorkPhase.PROCESS, work ) )
.forEach( expectationConsumer );
return this;
}
public DocumentWorkCallListContext discarded() {
log.debugf( "Expecting %d works to be discarded", works.size() );
works.stream()
.map( work -> new DocumentWorkCall( indexName, DocumentWorkCall.WorkPhase.DISCARD, work ) )
.forEach( expectationConsumer );
return this;
}
}
public static class IndexScaleWorkCallListContext {
private final String indexName;
private final String tenantIdentifier;
private final Consumer<IndexScaleWorkCall> expectationConsumer;
private IndexScaleWorkCallListContext(String indexName,
String tenantIdentifier,
Consumer<IndexScaleWorkCall> expectationConsumer) {
this.indexName = indexName;
this.tenantIdentifier = tenantIdentifier;
this.expectationConsumer = expectationConsumer;
}
public IndexScaleWorkCallListContext mergeSegments() {
return indexScaleWork( StubIndexScaleWork.Type.MERGE_SEGMENTS );
}
public IndexScaleWorkCallListContext mergeSegments(CompletableFuture<?> future) {
return indexScaleWork( StubIndexScaleWork.Type.MERGE_SEGMENTS, future );
}
public IndexScaleWorkCallListContext purge() {
return indexScaleWork( StubIndexScaleWork.Type.PURGE );
}
public IndexScaleWorkCallListContext purge(CompletableFuture<?> future) {
return indexScaleWork( StubIndexScaleWork.Type.PURGE, future );
}
public IndexScaleWorkCallListContext purge(Set<String> routingKeys) {
return indexScaleWork( StubIndexScaleWork.Type.PURGE, routingKeys );
}
public IndexScaleWorkCallListContext purge(Set<String> routingKeys, CompletableFuture<?> future) {
return indexScaleWork( StubIndexScaleWork.Type.PURGE, routingKeys, future );
}
public IndexScaleWorkCallListContext flush() {
return indexScaleWork( StubIndexScaleWork.Type.FLUSH );
}
public IndexScaleWorkCallListContext flush(CompletableFuture<?> future) {
return indexScaleWork( StubIndexScaleWork.Type.FLUSH, future );
}
public IndexScaleWorkCallListContext refresh() {
return indexScaleWork( StubIndexScaleWork.Type.REFRESH );
}
public IndexScaleWorkCallListContext refresh(CompletableFuture<?> future) {
return indexScaleWork( StubIndexScaleWork.Type.REFRESH, future );
}
public IndexScaleWorkCallListContext indexScaleWork(StubIndexScaleWork.Type type) {
return indexScaleWork( type, Collections.emptySet() );
}
public IndexScaleWorkCallListContext indexScaleWork(StubIndexScaleWork.Type type, CompletableFuture<?> future) {
return indexScaleWork( type, Collections.emptySet(), future );
}
public IndexScaleWorkCallListContext indexScaleWork(StubIndexScaleWork.Type type, Set<String> routingKeys) {
return indexScaleWork( type, routingKeys, CompletableFuture.completedFuture( null ) );
}
public IndexScaleWorkCallListContext indexScaleWork(StubIndexScaleWork.Type type, Set<String> routingKeys,
CompletableFuture<?> future) {
StubIndexScaleWork work = StubIndexScaleWork.builder( type )
.tenantIdentifier( tenantIdentifier )
.routingKeys( routingKeys )
.build();
expectationConsumer.accept( new IndexScaleWorkCall( indexName, work, future ) );
return this;
}
}
}