24
24
import java .util .List ;
25
25
import java .util .stream .Stream ;
26
26
27
- import org .junit .Before ;
28
27
import org .junit .Test ;
29
- import org .junit .runner .RunWith ;
30
28
import org .springframework .beans .factory .annotation .Autowired ;
31
29
import org .springframework .data .domain .Page ;
32
30
import org .springframework .data .domain .PageRequest ;
33
31
import org .springframework .data .domain .Sort ;
34
- import org .springframework .data .elasticsearch . core . ElasticsearchTemplate ;
32
+ import org .springframework .data .domain . Sort . Order ;
35
33
import org .springframework .data .elasticsearch .core .geo .GeoPoint ;
36
34
import org .springframework .data .elasticsearch .entities .SampleEntity ;
37
35
import org .springframework .data .elasticsearch .repositories .custom .SampleCustomMethodRepository ;
38
36
import org .springframework .data .geo .Box ;
39
37
import org .springframework .data .geo .Distance ;
40
38
import org .springframework .data .geo .Metrics ;
41
39
import org .springframework .data .geo .Point ;
42
- import org .springframework .test .context .ContextConfiguration ;
43
- import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
44
40
45
41
/**
46
42
* @author Rizwan Idrees
@@ -65,7 +61,7 @@ public void shouldExecuteCustomMethod() {
65
61
sampleEntity .setMessage ("some message" );
66
62
repository .save (sampleEntity );
67
63
// when
68
- Page <SampleEntity > page = repository .findByType ("test" , new PageRequest (0 , 10 ));
64
+ Page <SampleEntity > page = repository .findByType ("test" , PageRequest . of (0 , 10 ));
69
65
// then
70
66
assertThat (page , is (notNullValue ()));
71
67
assertThat (page .getTotalElements (), is (greaterThanOrEqualTo (1L )));
@@ -81,7 +77,7 @@ public void shouldExecuteCustomMethodForNot() {
81
77
sampleEntity .setMessage ("some message" );
82
78
repository .save (sampleEntity );
83
79
// when
84
- Page <SampleEntity > page = repository .findByTypeNot ("test" , new PageRequest (0 , 10 ));
80
+ Page <SampleEntity > page = repository .findByTypeNot ("test" , PageRequest . of (0 , 10 ));
85
81
// then
86
82
assertThat (page , is (notNullValue ()));
87
83
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -98,7 +94,7 @@ public void shouldExecuteCustomMethodWithQuery() {
98
94
sampleEntity .setMessage (searchTerm );
99
95
repository .save (sampleEntity );
100
96
// when
101
- Page <SampleEntity > page = repository .findByMessage (searchTerm .toLowerCase (), new PageRequest (0 , 10 ));
97
+ Page <SampleEntity > page = repository .findByMessage (searchTerm .toLowerCase (), PageRequest . of (0 , 10 ));
102
98
// then
103
99
assertThat (page , is (notNullValue ()));
104
100
assertThat (page .getTotalElements (), is (greaterThanOrEqualTo (1L )));
@@ -124,7 +120,7 @@ public void shouldExecuteCustomMethodWithLessThan() {
124
120
repository .save (sampleEntity2 );
125
121
126
122
// when
127
- Page <SampleEntity > page = repository .findByRateLessThan (10 , new PageRequest (0 , 10 ));
123
+ Page <SampleEntity > page = repository .findByRateLessThan (10 , PageRequest . of (0 , 10 ));
128
124
// then
129
125
assertThat (page , is (notNullValue ()));
130
126
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -142,7 +138,7 @@ public void shouldExecuteCustomMethodWithBefore() {
142
138
repository .save (sampleEntity );
143
139
144
140
// when
145
- Page <SampleEntity > page = repository .findByRateBefore (10 , new PageRequest (0 , 10 ));
141
+ Page <SampleEntity > page = repository .findByRateBefore (10 , PageRequest . of (0 , 10 ));
146
142
// then
147
143
assertThat (page , is (notNullValue ()));
148
144
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -160,7 +156,7 @@ public void shouldExecuteCustomMethodWithAfter() {
160
156
repository .save (sampleEntity );
161
157
162
158
// when
163
- Page <SampleEntity > page = repository .findByRateAfter (10 , new PageRequest (0 , 10 ));
159
+ Page <SampleEntity > page = repository .findByRateAfter (10 , PageRequest . of (0 , 10 ));
164
160
// then
165
161
assertThat (page , is (notNullValue ()));
166
162
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -178,7 +174,7 @@ public void shouldExecuteCustomMethodWithLike() {
178
174
repository .save (sampleEntity );
179
175
180
176
// when
181
- Page <SampleEntity > page = repository .findByMessageLike ("fo" , new PageRequest (0 , 10 ));
177
+ Page <SampleEntity > page = repository .findByMessageLike ("fo" , PageRequest . of (0 , 10 ));
182
178
// then
183
179
assertThat (page , is (notNullValue ()));
184
180
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -196,7 +192,7 @@ public void shouldExecuteCustomMethodForStartingWith() {
196
192
repository .save (sampleEntity );
197
193
198
194
// when
199
- Page <SampleEntity > page = repository .findByMessageStartingWith ("fo" , new PageRequest (0 , 10 ));
195
+ Page <SampleEntity > page = repository .findByMessageStartingWith ("fo" , PageRequest . of (0 , 10 ));
200
196
// then
201
197
assertThat (page , is (notNullValue ()));
202
198
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -214,7 +210,7 @@ public void shouldExecuteCustomMethodForEndingWith() {
214
210
repository .save (sampleEntity );
215
211
216
212
// when
217
- Page <SampleEntity > page = repository .findByMessageEndingWith ("o" , new PageRequest (0 , 10 ));
213
+ Page <SampleEntity > page = repository .findByMessageEndingWith ("o" , PageRequest . of (0 , 10 ));
218
214
// then
219
215
assertThat (page , is (notNullValue ()));
220
216
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -232,7 +228,7 @@ public void shouldExecuteCustomMethodForContains() {
232
228
repository .save (sampleEntity );
233
229
234
230
// when
235
- Page <SampleEntity > page = repository .findByMessageContaining ("fo" , new PageRequest (0 , 10 ));
231
+ Page <SampleEntity > page = repository .findByMessageContaining ("fo" , PageRequest . of (0 , 10 ));
236
232
// then
237
233
assertThat (page , is (notNullValue ()));
238
234
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -259,7 +255,7 @@ public void shouldExecuteCustomMethodForIn() {
259
255
List <String > ids = Arrays .asList (documentId , documentId2 );
260
256
261
257
// when
262
- Page <SampleEntity > page = repository .findByIdIn (ids , new PageRequest (0 , 10 ));
258
+ Page <SampleEntity > page = repository .findByIdIn (ids , PageRequest . of (0 , 10 ));
263
259
// then
264
260
assertThat (page , is (notNullValue ()));
265
261
assertThat (page .getTotalElements (), is (equalTo (2L )));
@@ -286,7 +282,7 @@ public void shouldExecuteCustomMethodForNotIn() {
286
282
List <String > ids = Arrays .asList (documentId );
287
283
288
284
// when
289
- Page <SampleEntity > page = repository .findByIdNotIn (ids , new PageRequest (0 , 10 ));
285
+ Page <SampleEntity > page = repository .findByIdNotIn (ids , PageRequest . of (0 , 10 ));
290
286
// then
291
287
assertThat (page , is (notNullValue ()));
292
288
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -313,7 +309,7 @@ public void shouldExecuteCustomMethodForTrue() {
313
309
sampleEntity2 .setAvailable (false );
314
310
repository .save (sampleEntity2 );
315
311
// when
316
- Page <SampleEntity > page = repository .findByAvailableTrue (new PageRequest (0 , 10 ));
312
+ Page <SampleEntity > page = repository .findByAvailableTrue (PageRequest . of (0 , 10 ));
317
313
// then
318
314
assertThat (page , is (notNullValue ()));
319
315
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -339,7 +335,7 @@ public void shouldExecuteCustomMethodForFalse() {
339
335
sampleEntity2 .setAvailable (false );
340
336
repository .save (sampleEntity2 );
341
337
// when
342
- Page <SampleEntity > page = repository .findByAvailableFalse (new PageRequest (0 , 10 ));
338
+ Page <SampleEntity > page = repository .findByAvailableFalse (PageRequest . of (0 , 10 ));
343
339
// then
344
340
assertThat (page , is (notNullValue ()));
345
341
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -375,7 +371,7 @@ public void shouldExecuteCustomMethodForOrderBy() {
375
371
repository .save (sampleEntity3 );
376
372
377
373
// when
378
- Page <SampleEntity > page = repository .findByMessageOrderByTypeAsc ("foo" , new PageRequest (0 , 10 ));
374
+ Page <SampleEntity > page = repository .findByMessageOrderByTypeAsc ("foo" , PageRequest . of (0 , 10 ));
379
375
// then
380
376
assertThat (page , is (notNullValue ()));
381
377
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -401,7 +397,7 @@ public void shouldExecuteCustomMethodWithBooleanParameter() {
401
397
sampleEntity2 .setAvailable (false );
402
398
repository .save (sampleEntity2 );
403
399
// when
404
- Page <SampleEntity > page = repository .findByAvailable (false , new PageRequest (0 , 10 ));
400
+ Page <SampleEntity > page = repository .findByAvailable (false , PageRequest . of (0 , 10 ));
405
401
// then
406
402
assertThat (page , is (notNullValue ()));
407
403
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -419,7 +415,7 @@ public void shouldReturnPageableResultsWithQueryAnnotationExpectedPageSize() {
419
415
repository .save (sampleEntity );
420
416
}
421
417
// when
422
- Page <SampleEntity > pageResult = repository .findByMessage ("message" , new PageRequest (0 , 23 ));
418
+ Page <SampleEntity > pageResult = repository .findByMessage ("message" , PageRequest . of (0 , 23 ));
423
419
// then
424
420
assertThat (pageResult .getTotalElements (), is (equalTo (30L )));
425
421
assertThat (pageResult .getContent ().size (), is (equalTo (23 )));
@@ -450,7 +446,7 @@ public void shouldReturnPageableResultsWithGivenSortingOrder() {
450
446
repository .save (sampleEntity3 );
451
447
// when
452
448
Page <SampleEntity > pageResult = repository .findByMessageContaining ("a" ,
453
- new PageRequest (0 , 23 , new Sort ( new Sort . Order ( Sort . Direction . DESC , "message" ))));
449
+ PageRequest . of (0 , 23 , Sort . by ( Order . desc ( "message" ))));
454
450
// then
455
451
assertThat (pageResult .getContent ().isEmpty (), is (false ));
456
452
assertThat (pageResult .getContent ().get (0 ).getMessage (), is (sampleEntity3 .getMessage ()));
@@ -500,7 +496,7 @@ public void shouldExecuteCustomMethodWithGeoPoint() {
500
496
repository .save (sampleEntity );
501
497
502
498
// when
503
- Page <SampleEntity > page = repository .findByLocation (new GeoPoint (45.7806d , 3.0875d ), new PageRequest (0 , 10 ));
499
+ Page <SampleEntity > page = repository .findByLocation (new GeoPoint (45.7806d , 3.0875d ), PageRequest . of (0 , 10 ));
504
500
// then
505
501
assertThat (page , is (notNullValue ()));
506
502
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -531,7 +527,7 @@ public void shouldExecuteCustomMethodWithGeoPointAndString() {
531
527
532
528
// when
533
529
Page <SampleEntity > page = repository .findByLocationAndMessage (new GeoPoint (45.7806d , 3.0875d ), "foo" ,
534
- new PageRequest (0 , 10 ));
530
+ PageRequest . of (0 , 10 ));
535
531
// then
536
532
assertThat (page , is (notNullValue ()));
537
533
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -552,7 +548,7 @@ public void shouldExecuteCustomMethodWithWithinGeoPoint() {
552
548
553
549
// when
554
550
Page <SampleEntity > page = repository .findByLocationWithin (new GeoPoint (45.7806d , 3.0875d ), "2km" ,
555
- new PageRequest (0 , 10 ));
551
+ PageRequest . of (0 , 10 ));
556
552
// then
557
553
assertThat (page , is (notNullValue ()));
558
554
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -573,7 +569,7 @@ public void shouldExecuteCustomMethodWithWithinPoint() {
573
569
574
570
// when
575
571
Page <SampleEntity > page = repository .findByLocationWithin (new Point (45.7806d , 3.0875d ),
576
- new Distance (2 , Metrics .KILOMETERS ), new PageRequest (0 , 10 ));
572
+ new Distance (2 , Metrics .KILOMETERS ), PageRequest . of (0 , 10 ));
577
573
// then
578
574
assertThat (page , is (notNullValue ()));
579
575
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -603,14 +599,14 @@ public void shouldExecuteCustomMethodWithNearBox() {
603
599
repository .save (sampleEntity2 );
604
600
605
601
// when
606
- Page <SampleEntity > pageAll = repository .findAll (new PageRequest (0 , 10 ));
602
+ Page <SampleEntity > pageAll = repository .findAll (PageRequest . of (0 , 10 ));
607
603
// then
608
604
assertThat (pageAll , is (notNullValue ()));
609
605
assertThat (pageAll .getTotalElements (), is (equalTo (2L )));
610
606
611
607
// when
612
608
Page <SampleEntity > page = repository .findByLocationNear (new Box (new Point (46d , 3d ), new Point (45d , 4d )),
613
- new PageRequest (0 , 10 ));
609
+ PageRequest . of (0 , 10 ));
614
610
// then
615
611
assertThat (page , is (notNullValue ()));
616
612
assertThat (page .getTotalElements (), is (equalTo (1L )));
@@ -631,7 +627,7 @@ public void shouldExecuteCustomMethodWithNearPointAndDistance() {
631
627
632
628
// when
633
629
Page <SampleEntity > page = repository .findByLocationNear (new Point (45.7806d , 3.0875d ),
634
- new Distance (2 , Metrics .KILOMETERS ), new PageRequest (0 , 10 ));
630
+ new Distance (2 , Metrics .KILOMETERS ), PageRequest . of (0 , 10 ));
635
631
// then
636
632
assertThat (page , is (notNullValue ()));
637
633
assertThat (page .getTotalElements (), is (equalTo (1L )));
0 commit comments