-
Notifications
You must be signed in to change notification settings - Fork 242
/
IndexedEmbeddedBaseIT.java
447 lines (413 loc) · 13.5 KB
/
IndexedEmbeddedBaseIT.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
/*
* 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.integrationtest.mapper.pojo.mapping.definition;
import static org.junit.Assert.assertEquals;
import java.lang.invoke.MethodHandles;
import java.util.function.Consumer;
import java.util.function.Function;
import org.hibernate.search.engine.backend.document.model.dsl.ObjectFieldStorage;
import org.hibernate.search.integrationtest.mapper.pojo.smoke.AnnotationMappingSmokeIT;
import org.hibernate.search.integrationtest.mapper.pojo.smoke.ProgrammaticMappingSmokeIT;
import org.hibernate.search.integrationtest.mapper.pojo.test.util.StartupStubBridge;
import org.hibernate.search.integrationtest.mapper.pojo.test.util.rule.JavaBeanMappingSetupHelper;
import org.hibernate.search.mapper.javabean.JavaBeanMapping;
import org.hibernate.search.mapper.pojo.bridge.mapping.BridgeBuilder;
import org.hibernate.search.mapper.pojo.mapping.PojoSearchManager;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.DocumentId;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Field;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.IndexedEmbedded;
import org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock;
import org.hibernate.search.util.impl.integrationtest.common.stub.backend.document.StubDocumentNode;
import org.hibernate.search.util.impl.test.annotation.TestForIssue;
import org.hibernate.search.util.impl.test.rule.StaticCounters;
import org.junit.Rule;
import org.junit.Test;
/**
* Test common use cases of the {@code @IndexedEmbedded} annotation.
* <p>
* Does not test all the corner cases of filtering when using {@link IndexedEmbedded#includePaths()} and
* {@link IndexedEmbedded#maxDepth()}, which are tested in a unit test in the engine module
* (the test is named {@code IndexSchemaNestingContextImplTest} at the time of this writing).
* <p>
* Does not test uses of container value extractors (for now). Some of them are tested in
* {@link AnnotationMappingSmokeIT} and {@link ProgrammaticMappingSmokeIT}.
*/
public class IndexedEmbeddedBaseIT {
private static final String INDEX_NAME = "IndexName";
@Rule
public BackendMock backendMock = new BackendMock( "stubBackend" );
@Rule
public JavaBeanMappingSetupHelper setupHelper = new JavaBeanMappingSetupHelper( MethodHandles.lookup() );
@Rule
public StaticCounters counters = new StaticCounters();
@Test
public void noParameter() {
class IndexedEmbeddedLevel2 {
String level2Property;
@Field
public String getLevel2Property() {
return level2Property;
}
}
class IndexedEmbeddedLevel1 {
String level1Property;
IndexedEmbeddedLevel2 level2;
@Field
public String getLevel1Property() {
return level1Property;
}
@IndexedEmbedded
public IndexedEmbeddedLevel2 getLevel2() {
return level2;
}
}
@Indexed(index = INDEX_NAME)
class IndexedEntity {
Integer id;
IndexedEmbeddedLevel1 level1;
public IndexedEntity(int id, String level1Value, String level2Value) {
this.id = id;
this.level1 = new IndexedEmbeddedLevel1();
this.level1.level1Property = level1Value;
this.level1.level2 = new IndexedEmbeddedLevel2();
this.level1.level2.level2Property = level2Value;
}
@DocumentId
public Integer getId() {
return id;
}
@IndexedEmbedded
public IndexedEmbeddedLevel1 getLevel1() {
return level1;
}
}
backendMock.expectSchema( INDEX_NAME, b -> b
.objectField( "level1", b2 -> b2
.field( "level1Property", String.class )
.objectField( "level2", b3 -> b3
.field( "level2Property", String.class )
)
)
);
JavaBeanMapping mapping = setupHelper.withBackendMock( backendMock )
.withAnnotatedEntityTypes( IndexedEntity.class )
.withAnnotatedTypes( IndexedEmbeddedLevel1.class, IndexedEmbeddedLevel2.class )
.setup();
backendMock.verifyExpectationsMet();
doTestEmbeddedRuntime(
mapping,
id -> new IndexedEntity( id, "level1Value", "level2Value" ),
document -> document.objectField( "level1", b2 -> b2
.field( "level1Property", "level1Value" )
.objectField( "level2", b3 -> b3
.field( "level2Property", "level2Value" )
)
)
);
}
/**
* Check that the "prefix" parameter is at least taken into account.
* <p>
* Details of how filtering handles all corner cases is tested in the engine (see this class' javadoc).
*/
@Test
public void prefix() {
class IndexedEmbeddedLevel1 {
String level1Property;
@Field
public String getLevel1Property() {
return level1Property;
}
}
@Indexed(index = INDEX_NAME)
class IndexedEntity {
Integer id;
IndexedEmbeddedLevel1 level1;
public IndexedEntity(int id, String level1Property) {
this.id = id;
this.level1 = new IndexedEmbeddedLevel1();
this.level1.level1Property = level1Property;
}
@DocumentId
public Integer getId() {
return id;
}
@IndexedEmbedded(prefix = "customPrefix_")
public IndexedEmbeddedLevel1 getLevel1() {
return level1;
}
}
backendMock.expectSchema( INDEX_NAME, b -> b
.field( "customPrefix_level1Property", String.class )
);
JavaBeanMapping mapping = setupHelper.withBackendMock( backendMock )
.withAnnotatedEntityTypes( IndexedEntity.class )
.withAnnotatedTypes( IndexedEmbeddedLevel1.class )
.setup();
backendMock.verifyExpectationsMet();
doTestEmbeddedRuntime(
mapping,
id -> new IndexedEntity( id, "level1Value" ),
document -> document
.field( "customPrefix_level1Property", "level1Value" )
);
}
/**
* Check that the "includePaths" parameter is at least taken into account.
* <p>
* Details of how filtering handles all corner cases is tested in the engine (see this class' javadoc).
*/
@Test
public void includePaths() {
class IndexedEmbeddedLevel1 {
String ignoredProperty;
String includedProperty;
@Field
public String getIgnoredProperty() {
return ignoredProperty;
}
@Field
public String getIncludedProperty() {
return includedProperty;
}
}
@Indexed(index = INDEX_NAME)
class IndexedEntity {
Integer id;
IndexedEmbeddedLevel1 level1;
public IndexedEntity(int id, String ignoredProperty, String includedProperty) {
this.id = id;
this.level1 = new IndexedEmbeddedLevel1();
this.level1.ignoredProperty = ignoredProperty;
this.level1.includedProperty = includedProperty;
}
@DocumentId
public Integer getId() {
return id;
}
@IndexedEmbedded(includePaths = "includedProperty")
public IndexedEmbeddedLevel1 getLevel1() {
return level1;
}
}
backendMock.expectSchema( INDEX_NAME, b -> b
.objectField( "level1", b2 -> b2
.field( "includedProperty", String.class )
)
);
JavaBeanMapping mapping = setupHelper.withBackendMock( backendMock )
.withAnnotatedEntityTypes( IndexedEntity.class )
.withAnnotatedTypes( IndexedEmbeddedLevel1.class )
.setup();
backendMock.verifyExpectationsMet();
doTestEmbeddedRuntime(
mapping,
id -> new IndexedEntity( id, "valueForIgnored", "valueForIncluded" ),
document -> document.objectField( "level1", b2 -> b2
.field( "includedProperty", "valueForIncluded" )
)
);
}
/**
* Check that the "maxDepth" parameter is at least taken into account.
* <p>
* Details of how filtering handles all corner cases is tested in the engine (see this class' javadoc).
*/
@Test
public void maxDepth() {
class IndexedEmbeddedLevel2 {
String level2Property;
@Field
public String getLevel2Property() {
return level2Property;
}
}
class IndexedEmbeddedLevel1 {
String level1Property;
IndexedEmbeddedLevel2 level2;
@Field
public String getLevel1Property() {
return level1Property;
}
@IndexedEmbedded
public IndexedEmbeddedLevel2 getLevel2() {
return level2;
}
}
@Indexed(index = INDEX_NAME)
class IndexedEntity {
Integer id;
IndexedEmbeddedLevel1 level1;
public IndexedEntity(int id, String level1Value, String level2Value) {
this.id = id;
this.level1 = new IndexedEmbeddedLevel1();
this.level1.level1Property = level1Value;
this.level1.level2 = new IndexedEmbeddedLevel2();
this.level1.level2.level2Property = level2Value;
}
@DocumentId
public Integer getId() {
return id;
}
@IndexedEmbedded(maxDepth = 1)
public IndexedEmbeddedLevel1 getLevel1() {
return level1;
}
}
backendMock.expectSchema( INDEX_NAME, b -> b
.objectField( "level1", b2 -> b2
.field( "level1Property", String.class )
)
);
JavaBeanMapping mapping = setupHelper.withBackendMock( backendMock )
.withAnnotatedEntityTypes( IndexedEntity.class )
.withAnnotatedTypes( IndexedEmbeddedLevel1.class, IndexedEmbeddedLevel2.class )
.setup();
backendMock.verifyExpectationsMet();
doTestEmbeddedRuntime(
mapping,
id -> new IndexedEntity( id, "level1Value", "level2Value" ),
document -> document.objectField( "level1", b2 -> b2
.field( "level1Property", "level1Value" )
)
);
}
/**
* Check that the "storage" parameter is at least taken into account.
* <p>
* Details of how filtering handles all corner cases is tested in the engine (see this class' javadoc).
*/
@Test
public void storage() {
class IndexedEmbeddedLevel1 {
String level1Property;
@Field
public String getLevel1Property() {
return level1Property;
}
}
@Indexed(index = INDEX_NAME)
class IndexedEntity {
Integer id;
IndexedEmbeddedLevel1 level1;
public IndexedEntity(int id, String level1Value) {
this.id = id;
this.level1 = new IndexedEmbeddedLevel1();
this.level1.level1Property = level1Value;
}
@DocumentId
public Integer getId() {
return id;
}
@IndexedEmbedded(storage = ObjectFieldStorage.NESTED)
public IndexedEmbeddedLevel1 getLevel1() {
return level1;
}
}
backendMock.expectSchema( INDEX_NAME, b -> b
.objectField( "level1", ObjectFieldStorage.NESTED, b2 -> b2
.field( "level1Property", String.class )
)
);
JavaBeanMapping mapping = setupHelper.withBackendMock( backendMock )
.withAnnotatedEntityTypes( IndexedEntity.class )
.withAnnotatedTypes( IndexedEmbeddedLevel1.class )
.setup();
backendMock.verifyExpectationsMet();
doTestEmbeddedRuntime(
mapping,
id -> new IndexedEntity( id, "level1Value" ),
document -> document.objectField( "level1", b2 -> b2
.field( "level1Property", "level1Value" )
)
);
}
/**
* Check that bridges whose contributed fields are all filtered out are never applied.
*/
@Test
@TestForIssue(jiraKey = "HSEARCH-3212")
public void includePaths_excludesBridges() {
class IndexedEmbeddedLevel1 {
String level1Property;
public String getLevel1Property() {
return level1Property;
}
}
class IndexedEntity {
Integer id;
IndexedEmbeddedLevel1 level1;
public IndexedEntity(int id, String level1Property) {
this.id = id;
this.level1 = new IndexedEmbeddedLevel1();
this.level1.level1Property = level1Property;
}
public Integer getId() {
return id;
}
public IndexedEmbeddedLevel1 getLevel1() {
return level1;
}
}
StartupStubBridge.CounterKeys filteredOutBridgeCounterKeys = StartupStubBridge.createKeys();
BridgeBuilder<StartupStubBridge> filteredOutBridgeBuilder =
c -> new StartupStubBridge( filteredOutBridgeCounterKeys );
backendMock.expectSchema( INDEX_NAME, b -> b
.objectField( "level1", b2 -> b2
.field( "level1IncludedField", String.class )
)
);
JavaBeanMapping mapping = setupHelper.withBackendMock( backendMock )
.withConfiguration( b -> {
b.addEntityType( IndexedEntity.class );
b.programmaticMapping().type( IndexedEntity.class )
.indexed( INDEX_NAME )
.property( "id" )
.documentId()
.property( "level1" )
.indexedEmbedded()
.includePaths( "level1IncludedField" );
b.programmaticMapping().type( IndexedEmbeddedLevel1.class )
.bridge( filteredOutBridgeBuilder )
.property( "level1Property" )
.bridge( filteredOutBridgeBuilder )
.field( "level1IncludedField" )
.field( "filteredOut" )
.valueBridge( filteredOutBridgeBuilder );
} )
.setup();
backendMock.verifyExpectationsMet();
/*
* All the bridges that were filtered out should have been instantiated,
* but then immediately closed.
*/
assertEquals( 3, counters.get( filteredOutBridgeCounterKeys.instance ) );
assertEquals( 0, counters.get( filteredOutBridgeCounterKeys.instance ) - counters.get( filteredOutBridgeCounterKeys.close ) );
doTestEmbeddedRuntime(
mapping,
id -> new IndexedEntity( id, "level1Value" ),
document -> document.objectField( "level1", b2 -> b2
.field( "level1IncludedField", "level1Value" )
)
);
// The bridges that were filtered out should not have been used.
assertEquals( 0, counters.get( filteredOutBridgeCounterKeys.runtimeUse ) );
}
private <E> void doTestEmbeddedRuntime(JavaBeanMapping mapping,
Function<Integer, E> newEntityFunction,
Consumer<StubDocumentNode.Builder> expectedDocumentContributor) {
try ( PojoSearchManager manager = mapping.createSearchManager() ) {
E entity1 = newEntityFunction.apply( 1 );
manager.getMainWorkPlan().add( entity1 );
backendMock.expectWorks( INDEX_NAME )
.add( "1", expectedDocumentContributor )
.preparedThenExecuted();
}
backendMock.verifyExpectationsMet();
}
}