Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for using default sort with data-mongodb query method #230

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,12 @@ void findByLastName(AssertableOutput output) {
});
}

@Test
void findWithDefaultSort(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining("annotated-query-default-sort(): [last-3, last-2, last-1]");
assertThat(output).hasSingleLineContaining("derived-query-default-sort(): [last-3, last-2, last-1]");
});
}

}
50 changes: 39 additions & 11 deletions data/data-mongodb/src/main/java/com/example/data/mongodb/CLR.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -61,18 +62,12 @@ public void run(String... args) {
runQueryByExample(product1);
runInTransaction(product1);

personRepository.save(new Person("first-1", "last-1"));
personRepository.save(new Person("first-2", "last-2"));
personRepository.save(new Person("first-3", "last-3"));

for (Person person : this.personRepository.findAll()) {
System.out.printf("findAll(): %s%n", person);
}

for (Person person : this.personRepository.findByLastname("last-3")) {
System.out.printf("findByLastname(): %s%n", person);
}
Person person1 = new Person("first-1", "last-1");
Person person2 = new Person("first-2", "last-2");
Person person3 = new Person("first-3", "last-3");

runDerivedFinder(person1, person2, person3);
runQueriesWithDefaultSort(person1, person2, person3);
}

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

private void runDerivedFinder(Person person1, Person person2, Person person3) {

log("---- DERIVED FINDER ----");
personRepository.deleteAll();
personRepository.saveAll(List.of(person1, person2, person3));

for (Person person : this.personRepository.findAll()) {
System.out.printf("findAll(): %s%n", person);
}

for (Person person : this.personRepository.findByLastname("last-3")) {
System.out.printf("findByLastname(): %s%n", person);
}
log("-----------------\n\n\n");
}

private void runQueriesWithDefaultSort(Person person1, Person person2, Person person3) {

log("---- DEFAULT SORT ----");
personRepository.deleteAll();
personRepository.saveAll(List.of(person1, person2, person3));

List<Person> annotatedQueryResult = this.personRepository.findAndSortPersonsDescByLastnameViaAnnotation("last");
System.out.printf("annotated-query-default-sort(): %s",
annotatedQueryResult.stream().map(Person::getLastname).collect(Collectors.toList()));

List<Person> derivedQueryResult = this.personRepository.findWithDefaultSortByLastnameStartingWith("last");
System.out.printf("derived-query-default-sort(): %s",
derivedQueryResult.stream().map(Person::getLastname).collect(Collectors.toList()));

log("-----------------\n\n\n");
}

private Order newOrder(String customerId, LineItem... items) {
return newOrder(customerId, new Date(), items);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@

import java.util.List;

import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.ListCrudRepository;

public interface PersonRepository extends ListCrudRepository<Person, String> {

List<Person> findByLastname(String lastname);

@Query(value = "{ 'lastname' : { '$regex' : '?0.*'} }", sort = "{ 'lastname' : -1 }")
List<Person> findAndSortPersonsDescByLastnameViaAnnotation(String lastname);

@Query(sort = "{ 'lastname' : -1 }")
List<Person> findWithDefaultSortByLastnameStartingWith(String lastname);

}