10
10
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
11
11
12
12
import java .lang .invoke .MethodHandles ;
13
+ import java .util .Arrays ;
13
14
import java .util .Collections ;
14
15
import java .util .HashMap ;
15
16
import java .util .Map ;
16
17
18
+ import org .hibernate .search .engine .backend .types .converter .runtime .FromDocumentValueConvertContext ;
19
+ import org .hibernate .search .engine .backend .types .converter .runtime .ToDocumentValueConvertContext ;
20
+ import org .hibernate .search .engine .backend .types .converter .runtime .spi .FromDocumentValueConvertContextImpl ;
21
+ import org .hibernate .search .engine .backend .types .converter .runtime .spi .ToDocumentValueConvertContextImpl ;
22
+ import org .hibernate .search .engine .backend .types .converter .spi .DslConverter ;
23
+ import org .hibernate .search .engine .backend .types .converter .spi .ProjectionConverter ;
17
24
import org .hibernate .search .engine .environment .bean .BeanReference ;
18
- import org .hibernate .search .mapper .pojo .common .spi .PojoEntityReference ;
19
- import org .hibernate .search .util .impl .integrationtest .mapper .pojo .standalone .StandalonePojoMappingSetupHelper ;
20
- import org .hibernate .search .mapper .pojo .standalone .mapping .SearchMapping ;
21
- import org .hibernate .search .engine .search .query .SearchQuery ;
22
- import org .hibernate .search .mapper .pojo .standalone .session .SearchSession ;
25
+ import org .hibernate .search .integrationtest .mapper .pojo .mapping .definition .BridgeTestUtils ;
23
26
import org .hibernate .search .mapper .pojo .bridge .IdentifierBridge ;
24
27
import org .hibernate .search .mapper .pojo .bridge .runtime .IdentifierBridgeFromDocumentIdentifierContext ;
25
28
import org .hibernate .search .mapper .pojo .bridge .runtime .IdentifierBridgeToDocumentIdentifierContext ;
29
+ import org .hibernate .search .mapper .pojo .common .spi .PojoEntityReference ;
26
30
import org .hibernate .search .mapper .pojo .mapping .definition .annotation .Indexed ;
27
- import org .hibernate .search .engine .common .EntityReference ;
31
+ import org .hibernate .search .mapper .pojo .standalone .mapping .SearchMapping ;
32
+ import org .hibernate .search .mapper .pojo .standalone .session .SearchSession ;
28
33
import org .hibernate .search .util .common .SearchException ;
29
34
import org .hibernate .search .util .impl .integrationtest .common .rule .BackendMock ;
30
35
import org .hibernate .search .util .impl .integrationtest .common .rule .StubSearchWorkBehavior ;
31
36
import org .hibernate .search .util .impl .integrationtest .common .stub .backend .StubBackendUtils ;
37
+ import org .hibernate .search .util .impl .integrationtest .common .stub .backend .document .model .impl .StubIndexModel ;
38
+ import org .hibernate .search .util .impl .integrationtest .mapper .pojo .standalone .StandalonePojoMappingSetupHelper ;
32
39
33
40
import org .junit .Rule ;
34
41
import org .junit .Test ;
@@ -41,6 +48,46 @@ public class ProvidedIdIT {
41
48
@ Rule
42
49
public StandalonePojoMappingSetupHelper setupHelper = StandalonePojoMappingSetupHelper .withBackendMock ( MethodHandles .lookup (), backendMock );
43
50
51
+ private StubIndexModel indexModel ;
52
+
53
+ @ Test
54
+ public void converters () {
55
+ final String entityAndIndexName = "indexed" ;
56
+ @ Indexed
57
+ class IndexedEntity {
58
+ }
59
+
60
+ // Schema
61
+ backendMock .expectSchema ( entityAndIndexName , b -> { },
62
+ indexModel -> this .indexModel = indexModel );
63
+ SearchMapping mapping = withBaseConfiguration ()
64
+ .withAnnotatedEntityType ( IndexedEntity .class , entityAndIndexName )
65
+ .setup ();
66
+ backendMock .verifyExpectationsMet ();
67
+
68
+ // DslConverter
69
+ @ SuppressWarnings ("unchecked" )
70
+ DslConverter <Object , String > dslConverter =
71
+ (DslConverter <Object , String >) indexModel .identifier ().dslConverter ();
72
+ ToDocumentValueConvertContext toDocumentConvertContext =
73
+ new ToDocumentValueConvertContextImpl ( BridgeTestUtils .toBackendMappingContext ( mapping ) );
74
+ assertThat ( dslConverter .toDocumentValue ( 120 , toDocumentConvertContext ) )
75
+ .isEqualTo ( "120" );
76
+ assertThat ( dslConverter .unknownTypeToDocumentValue ( 120 , toDocumentConvertContext ) )
77
+ .isEqualTo ( "120" );
78
+
79
+ // ProjectionConverter
80
+ @ SuppressWarnings ("unchecked" )
81
+ ProjectionConverter <String , Object > projectionConverter =
82
+ (ProjectionConverter <String , Object >) indexModel .identifier ().projectionConverter ();
83
+ try ( SearchSession searchSession = mapping .createSession () ) {
84
+ FromDocumentValueConvertContext fromDocumentConvertContext =
85
+ new FromDocumentValueConvertContextImpl ( BridgeTestUtils .toBackendSessionContext ( searchSession ) );
86
+ assertThat ( projectionConverter .fromDocumentValue ( "120" , fromDocumentConvertContext ) )
87
+ .isEqualTo ( 120 );
88
+ }
89
+ }
90
+
44
91
@ Test
45
92
public void indexAndSearch () {
46
93
final String entityAndIndexName = "indexed" ;
@@ -59,7 +106,7 @@ class IndexedEntity {
59
106
try ( SearchSession session = mapping .createSession () ) {
60
107
IndexedEntity entity1 = new IndexedEntity ();
61
108
62
- session .indexingPlan ().add ( "42" , null , entity1 );
109
+ session .indexingPlan ().add ( 42 , null , entity1 );
63
110
64
111
backendMock .expectWorks ( entityAndIndexName )
65
112
.add ( "42" , b -> { } );
@@ -68,21 +115,32 @@ class IndexedEntity {
68
115
69
116
// Searching
70
117
try ( SearchSession session = mapping .createSession () ) {
118
+ // Check provided ID bridge is applied when fetching entity references
71
119
backendMock .expectSearchReferences (
72
120
Collections .singletonList ( entityAndIndexName ),
73
121
StubSearchWorkBehavior .of (
74
122
1L ,
75
123
StubBackendUtils .reference ( entityAndIndexName , "42" )
76
124
)
77
125
);
78
-
79
- SearchQuery <EntityReference > query = session .search ( IndexedEntity .class )
126
+ assertThat ( session .search ( IndexedEntity .class )
80
127
.selectEntityReference ()
81
- .where ( f -> f .matchAll () )
82
- .toQuery ( );
128
+ .where ( f -> f .matchAll () ). fetchAllHits () )
129
+ .containsExactly ( PojoEntityReference . withName ( IndexedEntity . class , entityAndIndexName , 42 ) );
83
130
84
- assertThat ( query .fetchAll ().hits () )
85
- .containsExactly ( PojoEntityReference .withName ( IndexedEntity .class , entityAndIndexName , "42" ) );
131
+ // Check provided ID bridge is applied when fetching IDs
132
+ backendMock .expectSearchIds (
133
+ Collections .singletonList ( entityAndIndexName ),
134
+ b -> { },
135
+ StubSearchWorkBehavior .of (
136
+ 1L ,
137
+ Arrays .asList ( "42" )
138
+ )
139
+ );
140
+ assertThat ( session .search ( IndexedEntity .class )
141
+ .select ( f -> f .id () )
142
+ .where ( f -> f .matchAll () ).fetchAllHits () )
143
+ .containsExactly ( 42 );
86
144
}
87
145
backendMock .verifyExpectationsMet ();
88
146
}
0 commit comments