From d8fdc182654f5c8fbefbd94039d4500ccb927e9b Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 22 Feb 2017 09:59:37 -0500 Subject: [PATCH] DATAMONGO-1619 - Use ReactiveQueryByExampleExecutor in ReactiveMongoRepository. Add ReactiveQueryByExampleExecutor to ReactiveMongoRepository and check by providing tests for the execution invocation. Move methods into order and add some missing @Override annotations along the way. Related ticket: DATACMNS-995 via (spring-projects/spring-data-commons#197) Original Pull Request: #444 --- .../repository/ReactiveMongoRepository.java | 3 +- .../SimpleReactiveMongoRepository.java | 69 ++++++++++++------- .../SimpleReactiveMongoRepositoryTests.java | 50 ++++++++++++++ 3 files changed, 97 insertions(+), 25 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/ReactiveMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/ReactiveMongoRepository.java index 0dd08c263..edb622b3c 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/ReactiveMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/ReactiveMongoRepository.java @@ -22,6 +22,7 @@ import org.reactivestreams.Publisher; import org.springframework.data.domain.Example; import org.springframework.data.domain.Sort; import org.springframework.data.repository.NoRepositoryBean; +import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor; import org.springframework.data.repository.reactive.ReactiveSortingRepository; /** @@ -31,7 +32,7 @@ import org.springframework.data.repository.reactive.ReactiveSortingRepository; * @since 2.0 */ @NoRepositoryBean -public interface ReactiveMongoRepository extends ReactiveSortingRepository { +public interface ReactiveMongoRepository extends ReactiveSortingRepository, ReactiveQueryByExampleExecutor { /** * Inserts the given entity. Assumes the instance to be new to be able to apply insertion optimizations. Use the diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java index b6f1e8678..1ec163411 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java @@ -76,6 +76,19 @@ public class SimpleReactiveMongoRepository implement id -> mongoOperations.findById(id, entityInformation.getJavaType(), entityInformation.getCollectionName())); } + /* + * (non-Javadoc) + * @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#findOne(org.springframework.data.domain.Example) + */ + @Override + public Mono findOne(Example example) { + + Assert.notNull(example, "Sample must not be null!"); + + Query q = new Query(new Criteria().alike(example)); + return mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName()); + } + /* * (non-Javadoc) * @see org.springframework.data.repository.reactive.ReactiveCrudRepository#existsById(java.lang.Object) @@ -103,6 +116,23 @@ public class SimpleReactiveMongoRepository implement } + /* + * (non-Javadoc) + * @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#exists(org.springframework.data.domain.Example) + */ + @Override + public Mono exists(Example example) { + + Assert.notNull(example, "Sample must not be null!"); + + Query q = new Query(new Criteria().alike(example)); + return mongoOperations.exists(q, example.getProbeType(), entityInformation.getCollectionName()); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.reactive.ReactiveSortingRepository#findAll() + */ @Override public Flux findAll() { return findAll(new Query()); @@ -170,10 +200,24 @@ public class SimpleReactiveMongoRepository implement * (non-Javadoc) * @see org.springframework.data.repository.reactive.ReactiveCrudRepository#count() */ + @Override public Mono count() { return mongoOperations.count(new Query(), entityInformation.getCollectionName()); } + /* + * (non-Javadoc) + * @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#count(org.springframework.data.domain.Example) + */ + @Override + public Mono count(Example example) { + + Assert.notNull(example, "Sample must not be null!"); + + Query q = new Query(new Criteria().alike(example)); + return mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName()); + } + /* * (non-Javadoc) * @see org.springframework.data.mongodb.repository.ReactiveMongoRepository#insert(java.lang.Object) @@ -315,34 +359,11 @@ public class SimpleReactiveMongoRepository implement * (non-Javadoc) * @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll() */ + @Override public Mono deleteAll() { return mongoOperations.remove(new Query(), entityInformation.getCollectionName()).then(Mono.empty()); } - public Mono exists(Example example) { - - Assert.notNull(example, "Sample must not be null!"); - - Query q = new Query(new Criteria().alike(example)); - return mongoOperations.exists(q, example.getProbeType(), entityInformation.getCollectionName()); - } - - public Mono findOne(Example example) { - - Assert.notNull(example, "Sample must not be null!"); - - Query q = new Query(new Criteria().alike(example)); - return mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName()); - } - - public Mono count(Example example) { - - Assert.notNull(example, "Sample must not be null!"); - - Query q = new Query(new Criteria().alike(example)); - return mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName()); - } - private Query getIdQuery(Object id) { return new Query(getIdCriteria(id)); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java index e45044f1b..d30162527 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java @@ -17,6 +17,7 @@ package org.springframework.data.mongodb.repository; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import static org.springframework.data.domain.ExampleMatcher.*; import lombok.Data; import lombok.NoArgsConstructor; @@ -35,6 +36,7 @@ import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.annotation.Id; +import org.springframework.data.domain.Example; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.Order; @@ -353,6 +355,54 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware, StepVerifier.create(repository.findByLastname("Matthews")).expectNext(oliver).verifyComplete(); } + @Test // DATAMONGO-1619 + public void findOneByExampleShouldReturnObject() { + + Example example = Example.of(dave); + + StepVerifier.create(repository.findOne(example)).expectNext(dave).verifyComplete(); + } + + @Test // DATAMONGO-1619 + public void findAllByExampleShouldReturnObjects() { + + Example example = Example.of(dave, matching().withIgnorePaths("id", "age", "firstname")); + + StepVerifier.create(repository.findAll(example)).expectNextCount(2).verifyComplete(); + } + + @Test // DATAMONGO-1619 + public void findAllByExampleAndSortShouldReturnObjects() { + + Example example = Example.of(dave, matching().withIgnorePaths("id", "age", "firstname")); + + StepVerifier.create(repository.findAll(example, Sort.by("firstname"))).expectNext(dave, oliver).verifyComplete(); + } + + @Test // DATAMONGO-1619 + public void countByExampleShouldCountObjects() { + + Example example = Example.of(dave, matching().withIgnorePaths("id", "age", "firstname")); + + StepVerifier.create(repository.count(example)).expectNext(2L).verifyComplete(); + } + + @Test // DATAMONGO-1619 + public void existsByExampleShouldReturnExisting() { + + Example example = Example.of(dave, matching().withIgnorePaths("id", "age", "firstname")); + + StepVerifier.create(repository.exists(example)).expectNext(true).verifyComplete(); + } + + @Test // DATAMONGO-1619 + public void existsByExampleShouldReturnNonExisting() { + + Example example = Example.of(new ReactivePerson("foo", "bar", -1)); + + StepVerifier.create(repository.exists(example)).expectNext(false).verifyComplete(); + } + interface ReactivePersonRepostitory extends ReactiveMongoRepository { Flux findByLastname(String lastname);