Skip to content

Commit 39ee456

Browse files
christophstroblmhalbritter
authored andcommitted
Add tests for default sorting with MongoDB
Smoke test for spring-projects/spring-data-mongodb#4744 See gh-230
1 parent 8678e9b commit 39ee456

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

data/data-mongodb/src/appTest/java/com/example/data/mongodb/DataMongoDbApplicationAotTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,12 @@ void findByLastName(AssertableOutput output) {
8888
});
8989
}
9090

91+
@Test
92+
void findWithDefaultSort(AssertableOutput output) {
93+
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
94+
assertThat(output).hasSingleLineContaining("annotated-query-default-sort(): [last-3, last-2, last-1]");
95+
assertThat(output).hasSingleLineContaining("derived-query-default-sort(): [last-3, last-2, last-1]");
96+
});
97+
}
98+
9199
}

data/data-mongodb/src/main/java/com/example/data/mongodb/CLR.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.List;
77
import java.util.Optional;
88
import java.util.Set;
9+
import java.util.stream.Collectors;
910

1011
import org.bson.Document;
1112
import org.springframework.beans.factory.annotation.Autowired;
@@ -61,18 +62,12 @@ public void run(String... args) {
6162
runQueryByExample(product1);
6263
runInTransaction(product1);
6364

64-
personRepository.save(new Person("first-1", "last-1"));
65-
personRepository.save(new Person("first-2", "last-2"));
66-
personRepository.save(new Person("first-3", "last-3"));
67-
68-
for (Person person : this.personRepository.findAll()) {
69-
System.out.printf("findAll(): %s%n", person);
70-
}
71-
72-
for (Person person : this.personRepository.findByLastname("last-3")) {
73-
System.out.printf("findByLastname(): %s%n", person);
74-
}
65+
Person person1 = new Person("first-1", "last-1");
66+
Person person2 = new Person("first-2", "last-2");
67+
Person person3 = new Person("first-3", "last-3");
7568

69+
runDerivedFinder(person1, person2, person3);
70+
runQueriesWithDefaultSort(person1, person2, person3);
7671
}
7772

7873
// Prepare Collections to avoid timeouts on slow ci/docker/...
@@ -324,6 +319,39 @@ private void runInTransaction(LineItem product1) {
324319
log("-----------------\n\n\n");
325320
}
326321

322+
private void runDerivedFinder(Person person1, Person person2, Person person3) {
323+
324+
log("---- DERIVED FINDER ----");
325+
personRepository.deleteAll();
326+
personRepository.saveAll(List.of(person1, person2, person3));
327+
328+
for (Person person : this.personRepository.findAll()) {
329+
System.out.printf("findAll(): %s%n", person);
330+
}
331+
332+
for (Person person : this.personRepository.findByLastname("last-3")) {
333+
System.out.printf("findByLastname(): %s%n", person);
334+
}
335+
log("-----------------\n\n\n");
336+
}
337+
338+
private void runQueriesWithDefaultSort(Person person1, Person person2, Person person3) {
339+
340+
log("---- DEFAULT SORT ----");
341+
personRepository.deleteAll();
342+
personRepository.saveAll(List.of(person1, person2, person3));
343+
344+
List<Person> annotatedQueryResult = this.personRepository.findAndSortPersonsDescByLastnameViaAnnotation("last");
345+
System.out.printf("annotated-query-default-sort(): %s",
346+
annotatedQueryResult.stream().map(Person::getLastname).collect(Collectors.toList()));
347+
348+
List<Person> derivedQueryResult = this.personRepository.findWithDefaultSortByLastnameStartingWith("last");
349+
System.out.printf("derived-query-default-sort(): %s",
350+
derivedQueryResult.stream().map(Person::getLastname).collect(Collectors.toList()));
351+
352+
log("-----------------\n\n\n");
353+
}
354+
327355
private Order newOrder(String customerId, LineItem... items) {
328356
return newOrder(customerId, new Date(), items);
329357
}

data/data-mongodb/src/main/java/com/example/data/mongodb/PersonRepository.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@
1717

1818
import java.util.List;
1919

20+
import org.springframework.data.mongodb.repository.Query;
2021
import org.springframework.data.repository.ListCrudRepository;
2122

2223
public interface PersonRepository extends ListCrudRepository<Person, String> {
2324

2425
List<Person> findByLastname(String lastname);
2526

27+
@Query(value = "{ 'lastname' : { '$regex' : '?0.*'} }", sort = "{ 'lastname' : -1 }")
28+
List<Person> findAndSortPersonsDescByLastnameViaAnnotation(String lastname);
29+
30+
@Query(sort = "{ 'lastname' : -1 }")
31+
List<Person> findWithDefaultSortByLastnameStartingWith(String lastname);
32+
2633
}

0 commit comments

Comments
 (0)