16
16
package org .springframework .data .elasticsearch .repository .query ;
17
17
18
18
import static org .assertj .core .api .Assertions .*;
19
+ import static org .mockito .Mockito .*;
19
20
20
21
import java .lang .reflect .Method ;
21
22
import java .util .ArrayList ;
40
41
import org .springframework .data .elasticsearch .annotations .Query ;
41
42
import org .springframework .data .elasticsearch .core .ElasticsearchOperations ;
42
43
import org .springframework .data .elasticsearch .core .SearchHits ;
43
- import org .springframework .data .elasticsearch .core .convert .ElasticsearchConverter ;
44
- import org .springframework .data .elasticsearch .core .convert .MappingElasticsearchConverter ;
45
- import org .springframework .data .elasticsearch .core .mapping .SimpleElasticsearchMappingContext ;
46
44
import org .springframework .data .elasticsearch .core .query .StringQuery ;
47
45
import org .springframework .data .projection .SpelAwareProxyProjectionFactory ;
48
46
import org .springframework .data .repository .Repository ;
55
53
* @author Niklas Herder
56
54
*/
57
55
@ ExtendWith (MockitoExtension .class )
58
- public class ElasticsearchStringQueryUnitTests {
56
+ public class ElasticsearchStringQueryUnitTests extends ElasticsearchStringQueryUnitTestBase {
59
57
60
58
@ Mock ElasticsearchOperations operations ;
61
- ElasticsearchConverter converter ;
62
59
63
60
@ BeforeEach
64
61
public void setUp () {
65
- converter = new MappingElasticsearchConverter ( new SimpleElasticsearchMappingContext ());
62
+ when ( operations . getElasticsearchConverter ()). thenReturn ( setupConverter ());
66
63
}
67
64
68
65
@ Test // DATAES-552
@@ -141,6 +138,21 @@ private org.springframework.data.elasticsearch.core.query.Query createQuery(Stri
141
138
return elasticsearchStringQuery .createQuery (new ElasticsearchParametersParameterAccessor (queryMethod , args ));
142
139
}
143
140
141
+ @ Test // #1866
142
+ @ DisplayName ("should use converter on parameters" )
143
+ void shouldUseConverterOnParameters () throws NoSuchMethodException {
144
+
145
+ Car car = new Car ();
146
+ car .setName ("Toyota" );
147
+ car .setModel ("Prius" );
148
+
149
+ org .springframework .data .elasticsearch .core .query .Query query = createQuery ("findByCar" , car );
150
+
151
+ assertThat (query ).isInstanceOf (StringQuery .class );
152
+ assertThat (((StringQuery ) query ).getSource ())
153
+ .isEqualTo ("{ 'bool' : { 'must' : { 'term' : { 'car' : 'Toyota-Prius' } } } }" );
154
+ }
155
+
144
156
private ElasticsearchStringQuery queryForMethod (ElasticsearchQueryMethod queryMethod ) {
145
157
return new ElasticsearchStringQuery (queryMethod , operations , queryMethod .getAnnotatedQuery ());
146
158
}
@@ -149,7 +161,7 @@ private ElasticsearchQueryMethod getQueryMethod(String name, Class<?>... paramet
149
161
150
162
Method method = SampleRepository .class .getMethod (name , parameters );
151
163
return new ElasticsearchQueryMethod (method , new DefaultRepositoryMetadata (SampleRepository .class ),
152
- new SpelAwareProxyProjectionFactory (), converter .getMappingContext ());
164
+ new SpelAwareProxyProjectionFactory (), operations . getElasticsearchConverter () .getMappingContext ());
153
165
}
154
166
155
167
private interface SampleRepository extends Repository <Person , String > {
@@ -172,13 +184,16 @@ Person findWithRepeatedPlaceholder(String arg0, String arg1, String arg2, String
172
184
173
185
@ Query ("{\" bool\" :{\" must\" : [{\" match\" : {\" prefix\" : {\" name\" : \" ?0\" }}]}}" )
174
186
SearchHits <Book > findByPrefix (String prefix );
187
+
188
+ @ Query ("{ 'bool' : { 'must' : { 'term' : { 'car' : '?0' } } } }" )
189
+ Person findByCar (Car car );
175
190
}
176
191
177
192
/**
178
193
* @author Rizwan Idrees
179
194
* @author Mohsin Husen
180
195
* @author Artur Konczak
181
- * @author Niklas Herder
196
+ * @author Niklas Herder
182
197
*/
183
198
184
199
@ Document (indexName = "test-index-person-query-unittest" )
@@ -292,29 +307,6 @@ public void setDescription(@Nullable String description) {
292
307
}
293
308
}
294
309
295
- static class Car {
296
- @ Nullable private String name ;
297
- @ Nullable private String model ;
298
-
299
- @ Nullable
300
- public String getName () {
301
- return name ;
302
- }
303
-
304
- public void setName (@ Nullable String name ) {
305
- this .name = name ;
306
- }
307
-
308
- @ Nullable
309
- public String getModel () {
310
- return model ;
311
- }
312
-
313
- public void setModel (@ Nullable String model ) {
314
- this .model = model ;
315
- }
316
- }
317
-
318
310
static class Author {
319
311
320
312
@ Nullable private String id ;
@@ -338,5 +330,4 @@ public void setName(String name) {
338
330
this .name = name ;
339
331
}
340
332
}
341
-
342
333
}
0 commit comments