Skip to content

Commit 04ee9af

Browse files
committed
DATAMONGO-1619 - Use ReactiveQueryByExampleExecutor in ReactiveMongoRepository.
Related ticket: DATACMNS-995.
1 parent aecf049 commit 04ee9af

File tree

2 files changed

+74
-26
lines changed

2 files changed

+74
-26
lines changed

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@
1515
*/
1616
package org.springframework.data.mongodb.repository;
1717

18+
import reactor.core.publisher.Flux;
19+
import reactor.core.publisher.Mono;
20+
1821
import java.io.Serializable;
1922

2023
import org.reactivestreams.Publisher;
21-
import org.springframework.data.domain.Example;
22-
import org.springframework.data.domain.Sort;
2324
import org.springframework.data.repository.NoRepositoryBean;
24-
25+
import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor;
2526
import org.springframework.data.repository.reactive.ReactiveSortingRepository;
26-
import reactor.core.publisher.Flux;
27-
import reactor.core.publisher.Mono;
2827

2928
/**
3029
* Mongo specific {@link org.springframework.data.repository.Repository} interface with reactive support.
@@ -33,7 +32,8 @@
3332
* @since 2.0
3433
*/
3534
@NoRepositoryBean
36-
public interface ReactiveMongoRepository<T, ID extends Serializable> extends ReactiveSortingRepository<T, ID> {
35+
public interface ReactiveMongoRepository<T, ID extends Serializable>
36+
extends ReactiveSortingRepository<T, ID>, ReactiveQueryByExampleExecutor<T> {
3737

3838
/**
3939
* Inserts the given entity. Assumes the instance to be new to be able to apply insertion optimizations. Use
@@ -65,14 +65,4 @@ public interface ReactiveMongoRepository<T, ID extends Serializable> extends Rea
6565
*/
6666
<S extends T> Flux<S> insert(Publisher<S> entities);
6767

68-
/* (non-Javadoc)
69-
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example)
70-
*/
71-
<S extends T> Flux<S> findAll(Example<S> example);
72-
73-
/* (non-Javadoc)
74-
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort)
75-
*/
76-
<S extends T> Flux<S> findAll(Example<S> example, Sort sort);
77-
7868
}

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

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@
1818

1919
import static org.hamcrest.Matchers.*;
2020
import static org.junit.Assert.*;
21+
import static org.springframework.data.domain.ExampleMatcher.*;
22+
23+
import lombok.Data;
24+
import lombok.NoArgsConstructor;
25+
import reactor.core.publisher.Flux;
26+
import reactor.core.publisher.Mono;
27+
import reactor.test.TestSubscriber;
2128

2229
import java.util.Arrays;
2330
import java.util.List;
24-
import java.util.stream.Collectors;
2531

2632
import org.junit.Before;
2733
import org.junit.Test;
@@ -32,8 +38,7 @@
3238
import org.springframework.beans.factory.BeanFactoryAware;
3339
import org.springframework.beans.factory.annotation.Autowired;
3440
import org.springframework.data.annotation.Id;
35-
import org.springframework.data.domain.Page;
36-
import org.springframework.data.domain.PageRequest;
41+
import org.springframework.data.domain.Example;
3742
import org.springframework.data.domain.Sort;
3843
import org.springframework.data.domain.Sort.Direction;
3944
import org.springframework.data.domain.Sort.Order;
@@ -45,12 +50,6 @@
4550
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
4651
import org.springframework.util.ClassUtils;
4752

48-
import lombok.Data;
49-
import lombok.NoArgsConstructor;
50-
import reactor.core.publisher.Flux;
51-
import reactor.core.publisher.Mono;
52-
import reactor.test.TestSubscriber;
53-
5453
/**
5554
* Test for {@link ReactiveMongoRepository}.
5655
*
@@ -422,7 +421,6 @@ public void deleteIterableOfEntitiesShouldRemoveEntities() {
422421
List<ReactivePerson> matthews = repository.findByLastname("Matthews").collectList().block();
423422
assertThat(matthews, hasSize(1));
424423
assertThat(matthews, contains(oliver));
425-
426424
}
427425

428426
@Test // DATAMONGO-1444
@@ -438,7 +436,67 @@ public void deletePublisherOfEntitiesShouldRemoveEntities() {
438436
List<ReactivePerson> matthews = repository.findByLastname("Matthews").collectList().block();
439437
assertThat(matthews, hasSize(1));
440438
assertThat(matthews, contains(oliver));
439+
}
440+
441+
@Test // DATAMONGO-1619
442+
public void findOneByExampleShouldReturnObject() {
443+
444+
Example<ReactivePerson> example = Example.of(dave);
445+
446+
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber.subscribe(repository.findOne(example));
447+
448+
testSubscriber.await().assertComplete().assertValues(dave);
449+
}
450+
451+
@Test // DATAMONGO-1619
452+
public void findAllByExampleShouldReturnObjects() {
453+
454+
Example<ReactivePerson> example = Example.of(dave, matching().withIgnorePaths("id", "age", "firstname"));
455+
456+
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber.subscribe(repository.findAll(example));
457+
458+
testSubscriber.await().assertComplete().assertValueCount(2);
459+
}
460+
461+
@Test // DATAMONGO-1619
462+
public void findAllByExampleAndSortShouldReturnObjects() {
463+
464+
Example<ReactivePerson> example = Example.of(dave, matching().withIgnorePaths("id", "age", "firstname"));
465+
466+
TestSubscriber<ReactivePerson> testSubscriber = TestSubscriber
467+
.subscribe(repository.findAll(example, new Sort("firstname")));
468+
469+
testSubscriber.await().assertComplete().assertValues(dave, oliver);
470+
}
471+
472+
@Test // DATAMONGO-1619
473+
public void countByExampleShouldCountObjects() {
474+
475+
Example<ReactivePerson> example = Example.of(dave, matching().withIgnorePaths("id", "age", "firstname"));
476+
477+
TestSubscriber<Long> testSubscriber = TestSubscriber.subscribe(repository.count(example));
478+
479+
testSubscriber.await().assertComplete().assertValues(2L);
480+
}
481+
482+
@Test // DATAMONGO-1619
483+
public void existsByExampleShouldReturnExisting() {
484+
485+
Example<ReactivePerson> example = Example.of(dave, matching().withIgnorePaths("id", "age", "firstname"));
486+
487+
TestSubscriber<Boolean> testSubscriber = TestSubscriber.subscribe(repository.exists(example));
488+
489+
testSubscriber.await().assertComplete().assertValues(true);
490+
}
491+
492+
@Test // DATAMONGO-1619
493+
public void existsByExampleShouldReturnNonExisting() {
494+
495+
Example<ReactivePerson> example = Example.of(new ReactivePerson("foo", "bar", -1));
496+
497+
TestSubscriber<Boolean> testSubscriber = TestSubscriber.subscribe(repository.exists(example));
441498

499+
testSubscriber.await().assertComplete().assertValues(false);
442500
}
443501

444502
interface ReactivePersonRepostitory extends ReactiveMongoRepository<ReactivePerson, String> {

0 commit comments

Comments
 (0)