Skip to content

Commit 13d369d

Browse files
committed
DATAES-545 - Move off deprecations in Spring Data Commons.
Related ticket: DATACMNS-1496.
1 parent 11ba4c5 commit 13d369d

File tree

4 files changed

+65
-64
lines changed

4 files changed

+65
-64
lines changed

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.springframework.data.domain.PageRequest;
5656
import org.springframework.data.domain.Pageable;
5757
import org.springframework.data.domain.Sort;
58+
import org.springframework.data.domain.Sort.Order;
5859
import org.springframework.data.elasticsearch.ElasticsearchException;
5960
import org.springframework.data.elasticsearch.annotations.Document;
6061
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
@@ -688,7 +689,7 @@ public void shouldReturnPageableResultsGivenStringQuery() {
688689
elasticsearchTemplate.index(indexQuery);
689690
elasticsearchTemplate.refresh(SampleEntity.class);
690691

691-
StringQuery stringQuery = new StringQuery(matchAllQuery().toString(), new PageRequest(0, 10));
692+
StringQuery stringQuery = new StringQuery(matchAllQuery().toString(), PageRequest.of(0, 10));
692693
// when
693694
Page<SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(stringQuery, SampleEntity.class);
694695

@@ -712,8 +713,8 @@ public void shouldReturnSortedPageableResultsGivenStringQuery() {
712713
elasticsearchTemplate.index(indexQuery);
713714
elasticsearchTemplate.refresh(SampleEntity.class);
714715

715-
StringQuery stringQuery = new StringQuery(matchAllQuery().toString(), new PageRequest(0, 10), new Sort(
716-
new Sort.Order(Sort.Direction.ASC, "message")));
716+
StringQuery stringQuery = new StringQuery(matchAllQuery().toString(), PageRequest.of(0, 10), Sort.by(
717+
Order.asc("message")));
717718
// when
718719
Page<SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(stringQuery, SampleEntity.class);
719720
// then
@@ -902,7 +903,7 @@ public void shouldReturnResultsWithScanAndScrollForGivenCriteriaQuery() {
902903
CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria());
903904
criteriaQuery.addIndices(INDEX_NAME);
904905
criteriaQuery.addTypes(TYPE_NAME);
905-
criteriaQuery.setPageable(new PageRequest(0, 10));
906+
criteriaQuery.setPageable(PageRequest.of(0, 10));
906907

907908
ScrolledPage<SampleEntity> scroll = (ScrolledPage<SampleEntity>) elasticsearchTemplate.startScroll( 1000, criteriaQuery, SampleEntity.class);
908909
List<SampleEntity> sampleEntities = new ArrayList<>();
@@ -924,7 +925,7 @@ public void shouldReturnResultsWithScanAndScrollForGivenSearchQuery() {
924925
// then
925926

926927
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withIndices(INDEX_NAME)
927-
.withTypes(TYPE_NAME).withPageable(new PageRequest(0, 10)).build();
928+
.withTypes(TYPE_NAME).withPageable(PageRequest.of(0, 10)).build();
928929

929930
ScrolledPage<SampleEntity> scroll = (ScrolledPage<SampleEntity>) elasticsearchTemplate.startScroll(1000, searchQuery, SampleEntity.class);
930931
List<SampleEntity> sampleEntities = new ArrayList<>();
@@ -975,7 +976,7 @@ public void shouldReturnResultsWithScanAndScrollForSpecifiedFieldsForCriteriaQue
975976
criteriaQuery.addIndices(INDEX_NAME);
976977
criteriaQuery.addTypes(TYPE_NAME);
977978
criteriaQuery.addFields("message");
978-
criteriaQuery.setPageable(new PageRequest(0, 10));
979+
criteriaQuery.setPageable(PageRequest.of(0, 10));
979980

980981
Page<SampleEntity> scroll = elasticsearchTemplate.startScroll(1000, criteriaQuery, SampleEntity.class, searchResultMapper);
981982
String scrollId = ((ScrolledPage<?>)scroll).getScrollId();
@@ -1006,7 +1007,7 @@ public void shouldReturnResultsWithScanAndScrollForSpecifiedFieldsForSearchCrite
10061007
.withTypes(TYPE_NAME)
10071008
.withFields("message")
10081009
.withQuery(matchAllQuery())
1009-
.withPageable(new PageRequest(0, 10))
1010+
.withPageable(PageRequest.of(0, 10))
10101011
.build();
10111012

10121013
Page<SampleEntity> scroll = elasticsearchTemplate.startScroll(1000, searchQuery, SampleEntity.class, searchResultMapper);
@@ -1036,7 +1037,7 @@ public void shouldReturnResultsForScanAndScrollWithCustomResultMapperForGivenCri
10361037
CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria());
10371038
criteriaQuery.addIndices(INDEX_NAME);
10381039
criteriaQuery.addTypes(TYPE_NAME);
1039-
criteriaQuery.setPageable(new PageRequest(0, 10));
1040+
criteriaQuery.setPageable(PageRequest.of(0, 10));
10401041

10411042
Page<SampleEntity> scroll = elasticsearchTemplate.startScroll(1000, criteriaQuery, SampleEntity.class, searchResultMapper);
10421043
String scrollId = ((ScrolledPage) scroll).getScrollId();
@@ -1060,7 +1061,7 @@ public void shouldReturnResultsForScanAndScrollWithCustomResultMapperForGivenSea
10601061
// then
10611062

10621063
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withIndices(INDEX_NAME)
1063-
.withTypes(TYPE_NAME).withPageable(new PageRequest(0, 10)).build();
1064+
.withTypes(TYPE_NAME).withPageable(PageRequest.of(0, 10)).build();
10641065

10651066
Page<SampleEntity> scroll = elasticsearchTemplate.startScroll(1000, searchQuery, SampleEntity.class,searchResultMapper);
10661067
String scrollId = ((ScrolledPage) scroll).getScrollId();
@@ -1087,7 +1088,7 @@ public void shouldReturnResultsWithScanAndScrollForGivenCriteriaQueryAndClass()
10871088
// then
10881089

10891090
CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria());
1090-
criteriaQuery.setPageable(new PageRequest(0, 10));
1091+
criteriaQuery.setPageable(PageRequest.of(0, 10));
10911092

10921093
Page<SampleEntity> scroll = elasticsearchTemplate.startScroll(1000, criteriaQuery, SampleEntity.class);
10931094
String scrollId = ((ScrolledPage) scroll).getScrollId();
@@ -1114,7 +1115,7 @@ public void shouldReturnResultsWithScanAndScrollForGivenSearchQueryAndClass() {
11141115
// then
11151116

11161117
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
1117-
.withPageable(new PageRequest(0, 10)).build();
1118+
.withPageable(PageRequest.of(0, 10)).build();
11181119

11191120
Page<SampleEntity> scroll = elasticsearchTemplate.startScroll(1000, searchQuery, SampleEntity.class);
11201121
String scrollId = ((ScrolledPage) scroll).getScrollId();
@@ -1143,7 +1144,7 @@ public void shouldReturnResultsWithStreamForGivenCriteriaQuery() {
11431144
CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria());
11441145
criteriaQuery.addIndices(INDEX_NAME);
11451146
criteriaQuery.addTypes(TYPE_NAME);
1146-
criteriaQuery.setPageable(new PageRequest(0, 10));
1147+
criteriaQuery.setPageable(PageRequest.of(0, 10));
11471148

11481149
CloseableIterator<SampleEntity> stream = elasticsearchTemplate.stream(criteriaQuery, SampleEntity.class);
11491150
List<SampleEntity> sampleEntities = new ArrayList<>();
@@ -1640,7 +1641,7 @@ public void shouldReturnIds() {
16401641
.withQuery(termQuery("message", "message"))
16411642
.withIndices(INDEX_NAME)
16421643
.withTypes(TYPE_NAME)
1643-
.withPageable(new PageRequest(0, 100))
1644+
.withPageable(PageRequest.of(0, 100))
16441645
.build();
16451646
// then
16461647
List<String> ids = elasticsearchTemplate.queryForIds(searchQuery);

src/test/java/org/springframework/data/elasticsearch/repositories/CustomMethodRepositoryBaseTests.java

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,19 @@
2424
import java.util.List;
2525
import java.util.stream.Stream;
2626

27-
import org.junit.Before;
2827
import org.junit.Test;
29-
import org.junit.runner.RunWith;
3028
import org.springframework.beans.factory.annotation.Autowired;
3129
import org.springframework.data.domain.Page;
3230
import org.springframework.data.domain.PageRequest;
3331
import org.springframework.data.domain.Sort;
34-
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
32+
import org.springframework.data.domain.Sort.Order;
3533
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
3634
import org.springframework.data.elasticsearch.entities.SampleEntity;
3735
import org.springframework.data.elasticsearch.repositories.custom.SampleCustomMethodRepository;
3836
import org.springframework.data.geo.Box;
3937
import org.springframework.data.geo.Distance;
4038
import org.springframework.data.geo.Metrics;
4139
import org.springframework.data.geo.Point;
42-
import org.springframework.test.context.ContextConfiguration;
43-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
4440

4541
/**
4642
* @author Rizwan Idrees
@@ -65,7 +61,7 @@ public void shouldExecuteCustomMethod() {
6561
sampleEntity.setMessage("some message");
6662
repository.save(sampleEntity);
6763
// when
68-
Page<SampleEntity> page = repository.findByType("test", new PageRequest(0, 10));
64+
Page<SampleEntity> page = repository.findByType("test", PageRequest.of(0, 10));
6965
// then
7066
assertThat(page, is(notNullValue()));
7167
assertThat(page.getTotalElements(), is(greaterThanOrEqualTo(1L)));
@@ -81,7 +77,7 @@ public void shouldExecuteCustomMethodForNot() {
8177
sampleEntity.setMessage("some message");
8278
repository.save(sampleEntity);
8379
// when
84-
Page<SampleEntity> page = repository.findByTypeNot("test", new PageRequest(0, 10));
80+
Page<SampleEntity> page = repository.findByTypeNot("test", PageRequest.of(0, 10));
8581
// then
8682
assertThat(page, is(notNullValue()));
8783
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -98,7 +94,7 @@ public void shouldExecuteCustomMethodWithQuery() {
9894
sampleEntity.setMessage(searchTerm);
9995
repository.save(sampleEntity);
10096
// 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));
10298
// then
10399
assertThat(page, is(notNullValue()));
104100
assertThat(page.getTotalElements(), is(greaterThanOrEqualTo(1L)));
@@ -124,7 +120,7 @@ public void shouldExecuteCustomMethodWithLessThan() {
124120
repository.save(sampleEntity2);
125121

126122
// when
127-
Page<SampleEntity> page = repository.findByRateLessThan(10, new PageRequest(0, 10));
123+
Page<SampleEntity> page = repository.findByRateLessThan(10, PageRequest.of(0, 10));
128124
// then
129125
assertThat(page, is(notNullValue()));
130126
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -142,7 +138,7 @@ public void shouldExecuteCustomMethodWithBefore() {
142138
repository.save(sampleEntity);
143139

144140
// when
145-
Page<SampleEntity> page = repository.findByRateBefore(10, new PageRequest(0, 10));
141+
Page<SampleEntity> page = repository.findByRateBefore(10, PageRequest.of(0, 10));
146142
// then
147143
assertThat(page, is(notNullValue()));
148144
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -160,7 +156,7 @@ public void shouldExecuteCustomMethodWithAfter() {
160156
repository.save(sampleEntity);
161157

162158
// when
163-
Page<SampleEntity> page = repository.findByRateAfter(10, new PageRequest(0, 10));
159+
Page<SampleEntity> page = repository.findByRateAfter(10, PageRequest.of(0, 10));
164160
// then
165161
assertThat(page, is(notNullValue()));
166162
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -178,7 +174,7 @@ public void shouldExecuteCustomMethodWithLike() {
178174
repository.save(sampleEntity);
179175

180176
// when
181-
Page<SampleEntity> page = repository.findByMessageLike("fo", new PageRequest(0, 10));
177+
Page<SampleEntity> page = repository.findByMessageLike("fo", PageRequest.of(0, 10));
182178
// then
183179
assertThat(page, is(notNullValue()));
184180
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -196,7 +192,7 @@ public void shouldExecuteCustomMethodForStartingWith() {
196192
repository.save(sampleEntity);
197193

198194
// when
199-
Page<SampleEntity> page = repository.findByMessageStartingWith("fo", new PageRequest(0, 10));
195+
Page<SampleEntity> page = repository.findByMessageStartingWith("fo", PageRequest.of(0, 10));
200196
// then
201197
assertThat(page, is(notNullValue()));
202198
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -214,7 +210,7 @@ public void shouldExecuteCustomMethodForEndingWith() {
214210
repository.save(sampleEntity);
215211

216212
// when
217-
Page<SampleEntity> page = repository.findByMessageEndingWith("o", new PageRequest(0, 10));
213+
Page<SampleEntity> page = repository.findByMessageEndingWith("o", PageRequest.of(0, 10));
218214
// then
219215
assertThat(page, is(notNullValue()));
220216
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -232,7 +228,7 @@ public void shouldExecuteCustomMethodForContains() {
232228
repository.save(sampleEntity);
233229

234230
// when
235-
Page<SampleEntity> page = repository.findByMessageContaining("fo", new PageRequest(0, 10));
231+
Page<SampleEntity> page = repository.findByMessageContaining("fo", PageRequest.of(0, 10));
236232
// then
237233
assertThat(page, is(notNullValue()));
238234
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -259,7 +255,7 @@ public void shouldExecuteCustomMethodForIn() {
259255
List<String> ids = Arrays.asList(documentId, documentId2);
260256

261257
// when
262-
Page<SampleEntity> page = repository.findByIdIn(ids, new PageRequest(0, 10));
258+
Page<SampleEntity> page = repository.findByIdIn(ids, PageRequest.of(0, 10));
263259
// then
264260
assertThat(page, is(notNullValue()));
265261
assertThat(page.getTotalElements(), is(equalTo(2L)));
@@ -286,7 +282,7 @@ public void shouldExecuteCustomMethodForNotIn() {
286282
List<String> ids = Arrays.asList(documentId);
287283

288284
// when
289-
Page<SampleEntity> page = repository.findByIdNotIn(ids, new PageRequest(0, 10));
285+
Page<SampleEntity> page = repository.findByIdNotIn(ids, PageRequest.of(0, 10));
290286
// then
291287
assertThat(page, is(notNullValue()));
292288
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -313,7 +309,7 @@ public void shouldExecuteCustomMethodForTrue() {
313309
sampleEntity2.setAvailable(false);
314310
repository.save(sampleEntity2);
315311
// when
316-
Page<SampleEntity> page = repository.findByAvailableTrue(new PageRequest(0, 10));
312+
Page<SampleEntity> page = repository.findByAvailableTrue(PageRequest.of(0, 10));
317313
// then
318314
assertThat(page, is(notNullValue()));
319315
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -339,7 +335,7 @@ public void shouldExecuteCustomMethodForFalse() {
339335
sampleEntity2.setAvailable(false);
340336
repository.save(sampleEntity2);
341337
// when
342-
Page<SampleEntity> page = repository.findByAvailableFalse(new PageRequest(0, 10));
338+
Page<SampleEntity> page = repository.findByAvailableFalse(PageRequest.of(0, 10));
343339
// then
344340
assertThat(page, is(notNullValue()));
345341
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -375,7 +371,7 @@ public void shouldExecuteCustomMethodForOrderBy() {
375371
repository.save(sampleEntity3);
376372

377373
// when
378-
Page<SampleEntity> page = repository.findByMessageOrderByTypeAsc("foo", new PageRequest(0, 10));
374+
Page<SampleEntity> page = repository.findByMessageOrderByTypeAsc("foo", PageRequest.of(0, 10));
379375
// then
380376
assertThat(page, is(notNullValue()));
381377
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -401,7 +397,7 @@ public void shouldExecuteCustomMethodWithBooleanParameter() {
401397
sampleEntity2.setAvailable(false);
402398
repository.save(sampleEntity2);
403399
// when
404-
Page<SampleEntity> page = repository.findByAvailable(false, new PageRequest(0, 10));
400+
Page<SampleEntity> page = repository.findByAvailable(false, PageRequest.of(0, 10));
405401
// then
406402
assertThat(page, is(notNullValue()));
407403
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -419,7 +415,7 @@ public void shouldReturnPageableResultsWithQueryAnnotationExpectedPageSize() {
419415
repository.save(sampleEntity);
420416
}
421417
// when
422-
Page<SampleEntity> pageResult = repository.findByMessage("message", new PageRequest(0, 23));
418+
Page<SampleEntity> pageResult = repository.findByMessage("message", PageRequest.of(0, 23));
423419
// then
424420
assertThat(pageResult.getTotalElements(), is(equalTo(30L)));
425421
assertThat(pageResult.getContent().size(), is(equalTo(23)));
@@ -450,7 +446,7 @@ public void shouldReturnPageableResultsWithGivenSortingOrder() {
450446
repository.save(sampleEntity3);
451447
// when
452448
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"))));
454450
// then
455451
assertThat(pageResult.getContent().isEmpty(), is(false));
456452
assertThat(pageResult.getContent().get(0).getMessage(), is(sampleEntity3.getMessage()));
@@ -500,7 +496,7 @@ public void shouldExecuteCustomMethodWithGeoPoint() {
500496
repository.save(sampleEntity);
501497

502498
// 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));
504500
// then
505501
assertThat(page, is(notNullValue()));
506502
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -531,7 +527,7 @@ public void shouldExecuteCustomMethodWithGeoPointAndString() {
531527

532528
// when
533529
Page<SampleEntity> page = repository.findByLocationAndMessage(new GeoPoint(45.7806d, 3.0875d), "foo",
534-
new PageRequest(0, 10));
530+
PageRequest.of(0, 10));
535531
// then
536532
assertThat(page, is(notNullValue()));
537533
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -552,7 +548,7 @@ public void shouldExecuteCustomMethodWithWithinGeoPoint() {
552548

553549
// when
554550
Page<SampleEntity> page = repository.findByLocationWithin(new GeoPoint(45.7806d, 3.0875d), "2km",
555-
new PageRequest(0, 10));
551+
PageRequest.of(0, 10));
556552
// then
557553
assertThat(page, is(notNullValue()));
558554
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -573,7 +569,7 @@ public void shouldExecuteCustomMethodWithWithinPoint() {
573569

574570
// when
575571
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));
577573
// then
578574
assertThat(page, is(notNullValue()));
579575
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -603,14 +599,14 @@ public void shouldExecuteCustomMethodWithNearBox() {
603599
repository.save(sampleEntity2);
604600

605601
// when
606-
Page<SampleEntity> pageAll = repository.findAll(new PageRequest(0, 10));
602+
Page<SampleEntity> pageAll = repository.findAll(PageRequest.of(0, 10));
607603
// then
608604
assertThat(pageAll, is(notNullValue()));
609605
assertThat(pageAll.getTotalElements(), is(equalTo(2L)));
610606

611607
// when
612608
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));
614610
// then
615611
assertThat(page, is(notNullValue()));
616612
assertThat(page.getTotalElements(), is(equalTo(1L)));
@@ -631,7 +627,7 @@ public void shouldExecuteCustomMethodWithNearPointAndDistance() {
631627

632628
// when
633629
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));
635631
// then
636632
assertThat(page, is(notNullValue()));
637633
assertThat(page.getTotalElements(), is(equalTo(1L)));

0 commit comments

Comments
 (0)