/
ScaledNumberFieldIT.java
548 lines (477 loc) · 18.8 KB
/
ScaledNumberFieldIT.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
* 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.assertj.core.api.Assertions.assertThatThrownBy;
import java.lang.invoke.MethodHandles;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import org.hibernate.search.engine.backend.types.Aggregable;
import org.hibernate.search.engine.backend.types.Searchable;
import org.hibernate.search.engine.backend.types.dsl.ScaledNumberIndexFieldTypeOptionsStep;
import org.hibernate.search.integrationtest.mapper.pojo.testsupport.util.rule.JavaBeanMappingSetupHelper;
import org.hibernate.search.mapper.javabean.mapping.SearchMapping;
import org.hibernate.search.mapper.javabean.session.SearchSession;
import org.hibernate.search.mapper.javabean.work.SearchIndexingPlan;
import org.hibernate.search.mapper.pojo.bridge.ValueBridge;
import org.hibernate.search.mapper.pojo.bridge.binding.ValueBindingContext;
import org.hibernate.search.mapper.pojo.bridge.mapping.annotation.Parameter;
import org.hibernate.search.mapper.pojo.bridge.mapping.annotation.ValueBinderRef;
import org.hibernate.search.mapper.pojo.bridge.mapping.annotation.ValueBridgeRef;
import org.hibernate.search.mapper.pojo.bridge.mapping.programmatic.ValueBinder;
import org.hibernate.search.mapper.pojo.bridge.runtime.ValueBridgeToIndexedValueContext;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.DocumentId;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.ScaledNumberField;
import org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep;
import org.hibernate.search.util.common.SearchException;
import org.hibernate.search.util.impl.integrationtest.common.FailureReportUtils;
import org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock;
import org.hibernate.search.util.impl.test.annotation.TestForIssue;
import org.junit.Rule;
import org.junit.Test;
public class ScaledNumberFieldIT {
private static final String INDEX_NAME = "IndexName";
@Rule
public BackendMock backendMock = new BackendMock();
@Rule
public JavaBeanMappingSetupHelper setupHelper = JavaBeanMappingSetupHelper.withBackendMock( MethodHandles.lookup(), backendMock );
@Test
public void defaultAttributes() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField
BigDecimal value;
}
backendMock.expectSchema( INDEX_NAME, b -> b
.field( "value", BigDecimal.class )
);
setupHelper.start().setup( IndexedEntity.class );
backendMock.verifyExpectationsMet();
}
@Test
public void name() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField(name = "explicitName")
BigDecimal value;
}
backendMock.expectSchema( INDEX_NAME, b -> b
.field( "explicitName", BigDecimal.class )
);
setupHelper.start().setup( IndexedEntity.class );
backendMock.verifyExpectationsMet();
}
@Test
public void name_invalid_dot() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField(name = "invalid.withdot")
BigDecimal value;
}
assertThatThrownBy(
() -> setupHelper.start().setup( IndexedEntity.class )
)
.isInstanceOf( SearchException.class )
.hasMessageMatching( FailureReportUtils.buildFailureReportPattern()
.typeContext( IndexedEntity.class.getName() )
.pathContext( ".value" )
.annotationContextAnyParameters( ScaledNumberField.class )
.failure( "Invalid index field name 'invalid.withdot': field names cannot contain a dot ('.')" )
.build()
);
}
@Test
public void validDecimalScales_bigDecimals() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField(decimalScale = 7)
BigDecimal scaled;
@ScaledNumberField(decimalScale = 0)
BigDecimal unscaled;
@ScaledNumberField
BigDecimal defaultScaled;
}
backendMock.expectSchema( INDEX_NAME, b -> b
.field( "scaled", BigDecimal.class, f -> f.decimalScale( 7 ) )
.field( "unscaled", BigDecimal.class, f -> f.decimalScale( 0 ) )
.field( "defaultScaled", BigDecimal.class )
);
setupHelper.start().setup( IndexedEntity.class );
backendMock.verifyExpectationsMet();
}
@Test
public void validDecimalScales_bigIntegers() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField(decimalScale = 7)
BigInteger scaled;
@ScaledNumberField(decimalScale = 0)
BigInteger unscaled;
@ScaledNumberField
BigInteger defaultScaled;
}
backendMock.expectSchema( INDEX_NAME, b -> b
.field( "scaled", BigInteger.class, f -> f.decimalScale( 7 ) )
.field( "unscaled", BigInteger.class, f -> f.decimalScale( 0 ) )
.field( "defaultScaled", BigInteger.class )
);
setupHelper.start().setup( IndexedEntity.class );
backendMock.verifyExpectationsMet();
}
@Test
public void defaultBridge_invalidFieldType() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField
Integer notScalable;
}
assertThatThrownBy(
() -> setupHelper.start().setup( IndexedEntity.class )
)
.isInstanceOf( SearchException.class )
.hasMessageMatching( FailureReportUtils.buildFailureReportPattern()
.typeContext( IndexedEntity.class.getName() )
.pathContext( ".notScalable" )
.failure(
"Unable to apply property mapping: this property mapping must target"
+ " an index field of standard, scaled-number type (BigDecimal or BigInteger)",
"but the resolved field type is non-standard or non-scaled",
"This generally means you need to use a different field annotation"
+ " or to convert property values using a custom ValueBridge or ValueBinder.",
"If you are already using a custom ValueBridge or ValueBinder, check its field type.",
"encountered type DSL step '",
"expected interface '" + ScaledNumberIndexFieldTypeOptionsStep.class.getName() + "'"
)
.build()
);
}
@Test
public void searchable() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField(searchable = Searchable.YES)
BigDecimal searchable;
@ScaledNumberField(searchable = Searchable.NO)
BigInteger unsearchable;
@ScaledNumberField(searchable = Searchable.DEFAULT)
BigDecimal useDefault;
@ScaledNumberField
BigInteger implicit;
}
backendMock.expectSchema( INDEX_NAME, b -> b
.field( "searchable", BigDecimal.class, f -> f.searchable( Searchable.YES ) )
.field( "unsearchable", BigInteger.class, f -> f.searchable( Searchable.NO ) )
.field( "useDefault", BigDecimal.class )
.field( "implicit", BigInteger.class )
);
setupHelper.start().setup( IndexedEntity.class );
backendMock.verifyExpectationsMet();
}
@Test
public void aggregable() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField(aggregable = Aggregable.YES)
BigDecimal enabled;
@ScaledNumberField(aggregable = Aggregable.NO)
BigDecimal disabled;
@ScaledNumberField(aggregable = Aggregable.DEFAULT)
BigDecimal explicitDefault;
@ScaledNumberField
BigDecimal implicitDefault;
}
backendMock.expectSchema( INDEX_NAME, b -> b
.field( "enabled", BigDecimal.class, f -> f.aggregable( Aggregable.YES ) )
.field( "disabled", BigDecimal.class, f -> f.aggregable( Aggregable.NO ) )
.field( "explicitDefault", BigDecimal.class )
.field( "implicitDefault", BigDecimal.class )
);
setupHelper.start().setup( IndexedEntity.class );
backendMock.verifyExpectationsMet();
}
@Test
public void customBridge_implicitFieldType() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField(decimalScale = 3, valueBridge = @ValueBridgeRef(type = ValidTypeBridge.class))
WrappedValue wrap;
}
backendMock.expectSchema( INDEX_NAME, b -> b
.field( "wrap", BigDecimal.class, f -> f.decimalScale( 3 ) )
);
setupHelper.start().expectCustomBeans().setup( IndexedEntity.class );
backendMock.verifyExpectationsMet();
}
@Test
public void customBridge_explicitFieldType() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField(decimalScale = 3, valueBinder = @ValueBinderRef(type = ValidTypeBridge.ExplicitFieldTypeBinder.class))
WrappedValue wrap;
}
backendMock.expectSchema( INDEX_NAME, b -> b
.field( "wrap", BigDecimal.class, f -> f.decimalScale( 3 ) )
);
setupHelper.start().expectCustomBeans().setup( IndexedEntity.class );
backendMock.verifyExpectationsMet();
}
@Test
public void customBridge_withParameters_annotationMapping() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField(decimalScale = 2, valueBinder = @ValueBinderRef(type = ParametricBridge.ParametricBinder.class,
params = {
@Parameter(name = "unscaledVal", value = "773"), @Parameter(name = "scale", value = "2")
}))
WrappedValue wrap;
IndexedEntity() {
}
IndexedEntity(Integer id, WrappedValue wrap) {
this.id = id;
this.wrap = wrap;
}
}
backendMock.expectSchema( INDEX_NAME, b -> b.field( "wrap", BigDecimal.class, f -> f.decimalScale( 2 ) ) );
SearchMapping mapping = setupHelper.start().expectCustomBeans().setup( IndexedEntity.class );
backendMock.verifyExpectationsMet();
try ( SearchSession session = mapping.createSession() ) {
IndexedEntity entity1 = new IndexedEntity( 1, new WrappedValue( new BigInteger( "739" ), 2 ) );
IndexedEntity entity2 = new IndexedEntity( 2, new WrappedValue( new BigInteger( "333" ), 2 ) );
IndexedEntity entity3 = new IndexedEntity( 3, new WrappedValue( new BigInteger( "-773" ), 2 ) );
SearchIndexingPlan plan = session.indexingPlan();
plan.add( entity1 );
plan.add( entity2 );
plan.add( entity3 );
backendMock.expectWorks( INDEX_NAME )
.add( "1", b -> b.field( "wrap", new BigDecimal( new BigInteger( "1512" ), 2 ) ) )
.add( "2", b -> b.field( "wrap", new BigDecimal( new BigInteger( "1106" ), 2 ) ) )
.add( "3", b -> b.field( "wrap", new BigDecimal( BigInteger.ZERO, 2 ) ) )
.createdThenExecuted();
}
backendMock.verifyExpectationsMet();
}
@Test
public void customBridge_withParameters_programmaticMapping() {
class IndexedEntity {
Integer id;
WrappedValue wrap;
IndexedEntity() {
}
IndexedEntity(Integer id, WrappedValue wrap) {
this.id = id;
this.wrap = wrap;
}
}
backendMock.expectSchema( INDEX_NAME, b -> b.field( "wrap", BigDecimal.class, f -> f.decimalScale( 2 ) ) );
SearchMapping mapping = setupHelper.start()
.withConfiguration( builder -> {
builder.addEntityType( IndexedEntity.class );
TypeMappingStep indexedEntity = builder.programmaticMapping().type( IndexedEntity.class );
indexedEntity.indexed().index( INDEX_NAME );
indexedEntity.property( "id" ).documentId();
indexedEntity.property( "wrap" ).scaledNumberField().decimalScale( 2 ).valueBinder(
new ParametricBridge.ParametricBinder(),
Collections.singletonMap( "baseDecimal", new BigDecimal( new BigInteger( "773" ), 2 ) )
);
} )
.expectCustomBeans().setup( IndexedEntity.class );
backendMock.verifyExpectationsMet();
try ( SearchSession session = mapping.createSession() ) {
IndexedEntity entity1 = new IndexedEntity( 1, new WrappedValue( new BigInteger( "739" ), 2 ) );
IndexedEntity entity2 = new IndexedEntity( 2, new WrappedValue( new BigInteger( "333" ), 2 ) );
IndexedEntity entity3 = new IndexedEntity( 3, new WrappedValue( new BigInteger( "-773" ), 2 ) );
SearchIndexingPlan plan = session.indexingPlan();
plan.add( entity1 );
plan.add( entity2 );
plan.add( entity3 );
backendMock.expectWorks( INDEX_NAME )
.add( "1", b -> b.field( "wrap", new BigDecimal( new BigInteger( "1512" ), 2 ) ) )
.add( "2", b -> b.field( "wrap", new BigDecimal( new BigInteger( "1106" ), 2 ) ) )
.add( "3", b -> b.field( "wrap", new BigDecimal( BigInteger.ZERO, 2 ) ) )
.createdThenExecuted();
}
backendMock.verifyExpectationsMet();
}
@Test
public void customBridge_implicitFieldType_invalid() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField(decimalScale = 3, valueBridge = @ValueBridgeRef(type = InvalidTypeBridge.class))
WrappedValue wrap;
}
assertThatThrownBy( () -> setupHelper.start().expectCustomBeans().setup( IndexedEntity.class ) )
.isInstanceOf( SearchException.class )
.hasMessageMatching( FailureReportUtils.buildFailureReportPattern()
.typeContext( IndexedEntity.class.getName() )
.pathContext( ".wrap" )
.failure(
"Unable to apply property mapping: this property mapping must target"
+ " an index field of standard, scaled-number type (BigDecimal or BigInteger)",
"but the resolved field type is non-standard or non-scaled",
"This generally means you need to use a different field annotation"
+ " or to convert property values using a custom ValueBridge or ValueBinder.",
"If you are already using a custom ValueBridge or ValueBinder, check its field type.",
"encountered type DSL step '",
"expected interface '" + ScaledNumberIndexFieldTypeOptionsStep.class.getName() + "'"
)
.build()
);
}
@Test
public void customBridge_explicitFieldType_invalid() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField(decimalScale = 3, valueBinder = @ValueBinderRef(type = InvalidTypeBridge.ExplicitFieldTypeBinder.class))
WrappedValue wrap;
}
assertThatThrownBy( () -> setupHelper.start().expectCustomBeans().setup( IndexedEntity.class ) )
.isInstanceOf( SearchException.class )
.hasMessageMatching( FailureReportUtils.buildFailureReportPattern()
.typeContext( IndexedEntity.class.getName() )
.pathContext( ".wrap" )
.failure(
"Unable to apply property mapping: this property mapping must target"
+ " an index field of standard, scaled-number type (BigDecimal or BigInteger)",
"but the resolved field type is non-standard or non-scaled",
"This generally means you need to use a different field annotation"
+ " or to convert property values using a custom ValueBridge or ValueBinder.",
"If you are already using a custom ValueBridge or ValueBinder, check its field type.",
"encountered type DSL step '",
"expected interface '" + ScaledNumberIndexFieldTypeOptionsStep.class.getName() + "'"
)
.build()
);
}
@Test
@TestForIssue(jiraKey = "HSEARCH-3243")
public void customBridge_implicitFieldType_generic() {
@Indexed(index = INDEX_NAME)
class IndexedEntity {
@DocumentId
Integer id;
@ScaledNumberField(valueBridge = @ValueBridgeRef(type = GenericTypeBridge.class))
String property;
}
assertThatThrownBy( () -> setupHelper.start().expectCustomBeans().setup( IndexedEntity.class ) )
.isInstanceOf( SearchException.class )
.hasMessageMatching( FailureReportUtils.buildFailureReportPattern()
.typeContext( IndexedEntity.class.getName() )
.pathContext( ".property" )
.failure( "Unable to infer index field type for value bridge '"
+ GenericTypeBridge.TOSTRING + "':"
+ " this bridge implements ValueBridge<V, F>,"
+ " but sets the generic type parameter F to 'T'."
+ " The index field type can only be inferred automatically"
+ " when this type parameter is set to a raw class."
+ " Use a ValueBinder to set the index field type explicitly,"
+ " or set the type parameter F to a definite, raw type." )
.build() );
}
public static class ValidTypeBridge implements ValueBridge<WrappedValue, BigDecimal> {
@Override
public BigDecimal toIndexedValue(WrappedValue value, ValueBridgeToIndexedValueContext context) {
return value == null ? null : value.wrapped;
}
public static class ExplicitFieldTypeBinder implements ValueBinder {
@Override
public void bind(ValueBindingContext<?> context) {
context.bridge( WrappedValue.class, new ValidTypeBridge(), context.typeFactory().asBigDecimal() );
}
}
}
public static class InvalidTypeBridge implements ValueBridge<WrappedValue, Integer> {
@Override
public Integer toIndexedValue(WrappedValue value, ValueBridgeToIndexedValueContext context) {
throw new UnsupportedOperationException( "Should not be called" );
}
public static class ExplicitFieldTypeBinder implements ValueBinder {
@Override
public void bind(ValueBindingContext<?> context) {
context.bridge( WrappedValue.class, new InvalidTypeBridge(), context.typeFactory().asInteger() );
}
}
}
public static class GenericTypeBridge<T> implements ValueBridge<String, T> {
private static final String TOSTRING = "<GenericTypeBridge toString() result>";
@Override
public String toString() {
return TOSTRING;
}
@Override
public T toIndexedValue(String value, ValueBridgeToIndexedValueContext context) {
throw new UnsupportedOperationException( "Should not be called" );
}
}
public static class ParametricBridge implements ValueBridge<WrappedValue, BigDecimal> {
private final BigDecimal baseDecimal;
private ParametricBridge(BigDecimal baseDecimal) {
this.baseDecimal = baseDecimal;
}
@Override
public BigDecimal toIndexedValue(WrappedValue value, ValueBridgeToIndexedValueContext context) {
return ( value == null ) ? baseDecimal : value.wrapped.add( baseDecimal );
}
public static class ParametricBinder implements ValueBinder {
@Override
public void bind(ValueBindingContext<?> context) {
context.bridge( WrappedValue.class, new ParametricBridge( extractBaseDecimal( context ) ),
context.typeFactory().asBigDecimal()
);
}
}
@SuppressWarnings("uncheked")
private static BigDecimal extractBaseDecimal(ValueBindingContext<?> context) {
BigDecimal baseDecimal = (BigDecimal) context.parameter( "baseDecimal" );
if ( baseDecimal != null ) {
return baseDecimal;
}
Object unscaledValParam = context.parameter( "unscaledVal" );
Object scaleParam = context.parameter( "scale" );
if ( unscaledValParam == null || scaleParam == null ) {
return BigDecimal.ZERO;
}
BigInteger unscaledVal = new BigInteger( (String) unscaledValParam );
int scale = Integer.parseInt( (String) scaleParam );
return new BigDecimal( unscaledVal, scale );
}
}
private static class WrappedValue {
private BigDecimal wrapped;
WrappedValue() {
}
WrappedValue(BigInteger unscaledValue, int scale) {
this.wrapped = new BigDecimal( unscaledValue, scale );
}
}
}