Skip to content

Commit d8fdc18

Browse files
mp911dechristophstrobl
authored andcommitted
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: spring-projects#444
1 parent 840bde6 commit d8fdc18

File tree

3 files changed

+97
-25
lines changed

3 files changed

+97
-25
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/ReactiveMongoRepository.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.data.domain.Example;
2323
import org.springframework.data.domain.Sort;
2424
import org.springframework.data.repository.NoRepositoryBean;
25+
import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor;
2526
import org.springframework.data.repository.reactive.ReactiveSortingRepository;
2627

2728
/**
@@ -31,7 +32,7 @@
3132
* @since 2.0
3233
*/
3334
@NoRepositoryBean
34-
public interface ReactiveMongoRepository<T, ID> extends ReactiveSortingRepository<T, ID> {
35+
public interface ReactiveMongoRepository<T, ID> extends ReactiveSortingRepository<T, ID>, ReactiveQueryByExampleExecutor<T> {
3536

3637
/**
3738
* Inserts the given entity. Assumes the instance to be new to be able to apply insertion optimizations. Use the

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java

+45-24
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ public Mono<T> findById(Mono<ID> mono) {
7676
id -> mongoOperations.findById(id, entityInformation.getJavaType(), entityInformation.getCollectionName()));
7777
}
7878

79+
/*
80+
* (non-Javadoc)
81+
* @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#findOne(org.springframework.data.domain.Example)
82+
*/
83+
@Override
84+
public <S extends T> Mono<S> findOne(Example<S> example) {
85+
86+
Assert.notNull(example, "Sample must not be null!");
87+
88+
Query q = new Query(new Criteria().alike(example));
89+
return mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName());
90+
}
91+
7992
/*
8093
* (non-Javadoc)
8194
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#existsById(java.lang.Object)
@@ -103,6 +116,23 @@ public Mono<Boolean> existsById(Mono<ID> mono) {
103116

104117
}
105118

119+
/*
120+
* (non-Javadoc)
121+
* @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#exists(org.springframework.data.domain.Example)
122+
*/
123+
@Override
124+
public <S extends T> Mono<Boolean> exists(Example<S> example) {
125+
126+
Assert.notNull(example, "Sample must not be null!");
127+
128+
Query q = new Query(new Criteria().alike(example));
129+
return mongoOperations.exists(q, example.getProbeType(), entityInformation.getCollectionName());
130+
}
131+
132+
/*
133+
* (non-Javadoc)
134+
* @see org.springframework.data.repository.reactive.ReactiveSortingRepository#findAll()
135+
*/
106136
@Override
107137
public Flux<T> findAll() {
108138
return findAll(new Query());
@@ -170,10 +200,24 @@ public <S extends T> Flux<S> findAll(Example<S> example) {
170200
* (non-Javadoc)
171201
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#count()
172202
*/
203+
@Override
173204
public Mono<Long> count() {
174205
return mongoOperations.count(new Query(), entityInformation.getCollectionName());
175206
}
176207

208+
/*
209+
* (non-Javadoc)
210+
* @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#count(org.springframework.data.domain.Example)
211+
*/
212+
@Override
213+
public <S extends T> Mono<Long> count(Example<S> example) {
214+
215+
Assert.notNull(example, "Sample must not be null!");
216+
217+
Query q = new Query(new Criteria().alike(example));
218+
return mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName());
219+
}
220+
177221
/*
178222
* (non-Javadoc)
179223
* @see org.springframework.data.mongodb.repository.ReactiveMongoRepository#insert(java.lang.Object)
@@ -315,34 +359,11 @@ public Mono<Void> deleteAll(Publisher<? extends T> entityStream) {
315359
* (non-Javadoc)
316360
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll()
317361
*/
362+
@Override
318363
public Mono<Void> deleteAll() {
319364
return mongoOperations.remove(new Query(), entityInformation.getCollectionName()).then(Mono.empty());
320365
}
321366

322-
public <S extends T> Mono<Boolean> exists(Example<S> example) {
323-
324-
Assert.notNull(example, "Sample must not be null!");
325-
326-
Query q = new Query(new Criteria().alike(example));
327-
return mongoOperations.exists(q, example.getProbeType(), entityInformation.getCollectionName());
328-
}
329-
330-
public <S extends T> Mono<S> findOne(Example<S> example) {
331-
332-
Assert.notNull(example, "Sample must not be null!");
333-
334-
Query q = new Query(new Criteria().alike(example));
335-
return mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName());
336-
}
337-
338-
public <S extends T> Mono<Long> count(Example<S> example) {
339-
340-
Assert.notNull(example, "Sample must not be null!");
341-
342-
Query q = new Query(new Criteria().alike(example));
343-
return mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName());
344-
}
345-
346367
private Query getIdQuery(Object id) {
347368
return new Query(getIdCriteria(id));
348369
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java

+50
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
20+
import static org.springframework.data.domain.ExampleMatcher.*;
2021

2122
import lombok.Data;
2223
import lombok.NoArgsConstructor;
@@ -35,6 +36,7 @@
3536
import org.springframework.beans.factory.BeanFactoryAware;
3637
import org.springframework.beans.factory.annotation.Autowired;
3738
import org.springframework.data.annotation.Id;
39+
import org.springframework.data.domain.Example;
3840
import org.springframework.data.domain.Sort;
3941
import org.springframework.data.domain.Sort.Direction;
4042
import org.springframework.data.domain.Sort.Order;
@@ -353,6 +355,54 @@ public void deletePublisherOfEntitiesShouldRemoveEntities() {
353355
StepVerifier.create(repository.findByLastname("Matthews")).expectNext(oliver).verifyComplete();
354356
}
355357

358+
@Test // DATAMONGO-1619
359+
public void findOneByExampleShouldReturnObject() {
360+
361+
Example<ReactivePerson> example = Example.of(dave);
362+
363+
StepVerifier.create(repository.findOne(example)).expectNext(dave).verifyComplete();
364+
}
365+
366+
@Test // DATAMONGO-1619
367+
public void findAllByExampleShouldReturnObjects() {
368+
369+
Example<ReactivePerson> example = Example.of(dave, matching().withIgnorePaths("id", "age", "firstname"));
370+
371+
StepVerifier.create(repository.findAll(example)).expectNextCount(2).verifyComplete();
372+
}
373+
374+
@Test // DATAMONGO-1619
375+
public void findAllByExampleAndSortShouldReturnObjects() {
376+
377+
Example<ReactivePerson> example = Example.of(dave, matching().withIgnorePaths("id", "age", "firstname"));
378+
379+
StepVerifier.create(repository.findAll(example, Sort.by("firstname"))).expectNext(dave, oliver).verifyComplete();
380+
}
381+
382+
@Test // DATAMONGO-1619
383+
public void countByExampleShouldCountObjects() {
384+
385+
Example<ReactivePerson> example = Example.of(dave, matching().withIgnorePaths("id", "age", "firstname"));
386+
387+
StepVerifier.create(repository.count(example)).expectNext(2L).verifyComplete();
388+
}
389+
390+
@Test // DATAMONGO-1619
391+
public void existsByExampleShouldReturnExisting() {
392+
393+
Example<ReactivePerson> example = Example.of(dave, matching().withIgnorePaths("id", "age", "firstname"));
394+
395+
StepVerifier.create(repository.exists(example)).expectNext(true).verifyComplete();
396+
}
397+
398+
@Test // DATAMONGO-1619
399+
public void existsByExampleShouldReturnNonExisting() {
400+
401+
Example<ReactivePerson> example = Example.of(new ReactivePerson("foo", "bar", -1));
402+
403+
StepVerifier.create(repository.exists(example)).expectNext(false).verifyComplete();
404+
}
405+
356406
interface ReactivePersonRepostitory extends ReactiveMongoRepository<ReactivePerson, String> {
357407

358408
Flux<ReactivePerson> findByLastname(String lastname);

0 commit comments

Comments
 (0)