21
21
import reactor .core .publisher .Mono ;
22
22
23
23
import java .lang .reflect .Method ;
24
+ import java .util .Arrays ;
24
25
25
26
import org .junit .Before ;
26
27
import org .junit .Ignore ;
35
36
import org .springframework .data .elasticsearch .core .mapping .SimpleElasticsearchMappingContext ;
36
37
import org .springframework .data .elasticsearch .core .query .StringQuery ;
37
38
import org .springframework .data .elasticsearch .entities .Person ;
38
- import org .springframework .data .projection .ProjectionFactory ;
39
39
import org .springframework .data .projection .SpelAwareProxyProjectionFactory ;
40
40
import org .springframework .data .repository .Repository ;
41
41
import org .springframework .data .repository .core .support .DefaultRepositoryMetadata ;
@@ -90,27 +90,54 @@ public void bindsExpressionPropertyCorrectly() throws Exception {
90
90
@ Test // DATAES-552
91
91
public void shouldReplaceLotsOfParametersCorrectly () throws Exception {
92
92
93
- ReactiveElasticsearchStringQuery elasticsearchStringQuery = createQueryForMethod ("findWithQuiteSomeParameters" ,
94
- String .class , String .class , String .class , String .class , String .class , String .class , String .class , String .class ,
95
- String .class , String .class , String .class , String .class );
96
- StubParameterAccessor accesor = new StubParameterAccessor ("a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" ,
97
- "l" );
98
- org .springframework .data .elasticsearch .core .query .Query query = elasticsearchStringQuery .createQuery (accesor );
99
- StringQuery reference = new StringQuery ("name:(a, b, c, d, e, f, g, h, i, j, k, l)" );
93
+ org .springframework .data .elasticsearch .core .query .Query query = createQuery ("findWithQuiteSomeParameters" , "zero" ,
94
+ "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" , "ten" , "eleven" );
95
+
100
96
assertThat (query ).isInstanceOf (StringQuery .class );
101
- assertThat (((StringQuery ) query ).getSource ()).isEqualTo (reference .getSource ());
97
+ assertThat (((StringQuery ) query ).getSource ())
98
+ .isEqualTo ("name:(zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven)" );
102
99
}
103
100
104
- private ReactiveElasticsearchStringQuery createQueryForMethod (String name , Class <?>... parameters ) throws Exception {
101
+ @ Test // DATAES-552
102
+ public void shouldReplaceRepeatedParametersCorrectly () throws Exception {
105
103
106
- Method method = SampleRepository .class .getMethod (name , parameters );
107
- ProjectionFactory factory = new SpelAwareProxyProjectionFactory ();
108
- ReactiveElasticsearchQueryMethod queryMethod = new ReactiveElasticsearchQueryMethod (method ,
109
- new DefaultRepositoryMetadata (SampleRepository .class ), factory , converter .getMappingContext ());
104
+ org .springframework .data .elasticsearch .core .query .Query query = createQuery ("findWithRepeatedPlaceholder" , "zero" ,
105
+ "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" , "ten" , "eleven" );
106
+
107
+ assertThat (query ).isInstanceOf (StringQuery .class );
108
+ assertThat (((StringQuery ) query ).getSource ())
109
+ .isEqualTo ("name:(zero, eleven, one, two, three, four, five, six, seven, eight, nine, ten, eleven, zero, one)" );
110
+ }
111
+
112
+ private org .springframework .data .elasticsearch .core .query .Query createQuery (String methodName , String ... args )
113
+ throws NoSuchMethodException {
114
+
115
+ Class <?>[] argTypes = Arrays .stream (args ).map (Object ::getClass ).toArray (size -> new Class [size ]);
116
+ ReactiveElasticsearchQueryMethod queryMethod = getQueryMethod (methodName , argTypes );
117
+ ReactiveElasticsearchStringQuery elasticsearchStringQuery = queryForMethod (queryMethod );
118
+
119
+ return elasticsearchStringQuery .createQuery (new ElasticsearchParametersParameterAccessor (queryMethod , args ));
120
+ }
121
+
122
+ private ReactiveElasticsearchStringQuery queryForMethod (ReactiveElasticsearchQueryMethod queryMethod ) {
110
123
return new ReactiveElasticsearchStringQuery (queryMethod , operations , PARSER ,
111
124
QueryMethodEvaluationContextProvider .DEFAULT );
112
125
}
113
126
127
+ private ReactiveElasticsearchQueryMethod getQueryMethod (String name , Class <?>... parameters )
128
+ throws NoSuchMethodException {
129
+
130
+ Method method = SampleRepository .class .getMethod (name , parameters );
131
+ return new ReactiveElasticsearchQueryMethod (method , new DefaultRepositoryMetadata (SampleRepository .class ),
132
+ new SpelAwareProxyProjectionFactory (), converter .getMappingContext ());
133
+ }
134
+
135
+ private ReactiveElasticsearchStringQuery createQueryForMethod (String name , Class <?>... parameters ) throws Exception {
136
+
137
+ ReactiveElasticsearchQueryMethod queryMethod = getQueryMethod (name , parameters );
138
+ return queryForMethod (queryMethod );
139
+ }
140
+
114
141
private interface SampleRepository extends Repository <Person , String > {
115
142
116
143
@ Query ("{ 'bool' : { 'must' : { 'term' : { 'name' : '?0' } } } }" )
@@ -122,5 +149,9 @@ private interface SampleRepository extends Repository<Person, String> {
122
149
@ Query (value = "name:(?0, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)" )
123
150
Person findWithQuiteSomeParameters (String arg0 , String arg1 , String arg2 , String arg3 , String arg4 , String arg5 ,
124
151
String arg6 , String arg7 , String arg8 , String arg9 , String arg10 , String arg11 );
152
+
153
+ @ Query (value = "name:(?0, ?11, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?0, ?1)" )
154
+ Person findWithRepeatedPlaceholder (String arg0 , String arg1 , String arg2 , String arg3 , String arg4 , String arg5 ,
155
+ String arg6 , String arg7 , String arg8 , String arg9 , String arg10 , String arg11 );
125
156
}
126
157
}
0 commit comments