Browse Source

DATAMONGO-2652 - Polishing.

Reorder implementation methods. Reduce visibility of test methods according to JUnit 5 requirements.

Original pull request: #892.
pull/895/head
Mark Paluch 5 years ago
parent
commit
ab4fe5cb0b
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 12
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/ReactiveMongoRepository.java
  2. 128
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java
  3. 363
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java
  4. 120
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ReactiveMongoRepositoryTests.java
  5. 70
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java

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

@ -64,16 +64,4 @@ public interface ReactiveMongoRepository<T, ID> extends ReactiveSortingRepositor @@ -64,16 +64,4 @@ public interface ReactiveMongoRepository<T, ID> extends ReactiveSortingRepositor
*/
<S extends T> Flux<S> insert(Publisher<S> entities);
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example)
*/
<S extends T> Flux<S> findAll(Example<S> example);
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort)
*/
<S extends T> Flux<S> findAll(Example<S> example, Sort sort);
}

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

@ -73,6 +73,10 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> { @@ -73,6 +73,10 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
this.mongoOperations = mongoOperations;
}
// -------------------------------------------------------------------------
// Methods from CrudRepository
// -------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
@ -136,6 +140,27 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> { @@ -136,6 +140,27 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
entityInformation.getCollectionName());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAll()
*/
@Override
public List<T> findAll() {
return findAll(new Query());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAllById(java.lang.Iterable)
*/
@Override
public Iterable<T> findAllById(Iterable<ID> ids) {
Assert.notNull(ids, "The given Ids of entities not be null!");
return findAll(getIdQuery(ids));
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#count()
@ -178,55 +203,42 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> { @@ -178,55 +203,42 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
* @see org.springframework.data.repository.CrudRepository#deleteAllById(java.lang.Iterable)
*/
@Override
public void deleteAll(Iterable<? extends T> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null!");
entities.forEach(this::delete);
}
@Override
public void deleteAllById(Iterable<? extends ID> ids) {
Assert.notNull(ids, "The given Iterable of ids must not be null!");
ids.forEach(this::deleteById);
mongoOperations.remove(getIdQuery(ids), entityInformation.getJavaType(),
entityInformation.getCollectionName());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteAll()
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
*/
@Override
public void deleteAll() {
mongoOperations.remove(new Query(), entityInformation.getCollectionName());
}
public void deleteAll(Iterable<? extends T> entities) {
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAll()
*/
@Override
public List<T> findAll() {
return findAll(new Query());
Assert.notNull(entities, "The given Iterable of entities must not be null!");
entities.forEach(this::delete);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAllById(java.lang.Iterable)
* @see org.springframework.data.repository.CrudRepository#deleteAll()
*/
@Override
public Iterable<T> findAllById(Iterable<ID> ids) {
Assert.notNull(ids, "The given Ids of entities not be null!");
return findAll(new Query(new Criteria(entityInformation.getIdAttribute())
.in(Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList()))));
public void deleteAll() {
mongoOperations.remove(new Query(), entityInformation.getCollectionName());
}
// -------------------------------------------------------------------------
// Methods from PagingAndSortingRepository
// -------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Pageable)
@ -254,6 +266,10 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> { @@ -254,6 +266,10 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
return findAll(new Query().with(sort));
}
// -------------------------------------------------------------------------
// Methods from MongoRepository
// -------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.MongoRepository#insert(java.lang.Object)
@ -284,23 +300,33 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> { @@ -284,23 +300,33 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
return new ArrayList<>(mongoOperations.insertAll(list));
}
// -------------------------------------------------------------------------
// Methods from QueryByExampleExecutor
// -------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.MongoRepository#findAllByExample(org.springframework.data.domain.Example, org.springframework.data.domain.Pageable)
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findOne(org.springframework.data.domain.Example)
*/
@Override
public <S extends T> Page<S> findAll(final Example<S> example, Pageable pageable) {
public <S extends T> Optional<S> findOne(Example<S> example) {
Assert.notNull(example, "Sample must not be null!");
Assert.notNull(pageable, "Pageable must not be null!");
Query query = new Query(new Criteria().alike(example)) //
.collation(entityInformation.getCollation()).with(pageable); //
.collation(entityInformation.getCollation());
List<S> list = mongoOperations.find(query, example.getProbeType(), entityInformation.getCollectionName());
return Optional
.ofNullable(mongoOperations.findOne(query, example.getProbeType(), entityInformation.getCollectionName()));
}
return PageableExecutionUtils.getPage(list, pageable,
() -> mongoOperations.count(Query.of(query).limit(-1).skip(-1), example.getProbeType(), entityInformation.getCollectionName()));
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.MongoRepository#findAllByExample(org.springframework.data.domain.Example)
*/
@Override
public <S extends T> List<S> findAll(Example<S> example) {
return findAll(example, Sort.unsorted());
}
/*
@ -322,27 +348,21 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> { @@ -322,27 +348,21 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.MongoRepository#findAllByExample(org.springframework.data.domain.Example)
*/
@Override
public <S extends T> List<S> findAll(Example<S> example) {
return findAll(example, Sort.unsorted());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findOne(org.springframework.data.domain.Example)
* @see org.springframework.data.mongodb.repository.MongoRepository#findAllByExample(org.springframework.data.domain.Example, org.springframework.data.domain.Pageable)
*/
@Override
public <S extends T> Optional<S> findOne(Example<S> example) {
public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
Assert.notNull(example, "Sample must not be null!");
Assert.notNull(pageable, "Pageable must not be null!");
Query query = new Query(new Criteria().alike(example)) //
.collation(entityInformation.getCollation());
.collation(entityInformation.getCollation()).with(pageable); //
return Optional
.ofNullable(mongoOperations.findOne(query, example.getProbeType(), entityInformation.getCollectionName()));
List<S> list = mongoOperations.find(query, example.getProbeType(), entityInformation.getCollectionName());
return PageableExecutionUtils.getPage(list, pageable,
() -> mongoOperations.count(Query.of(query).limit(-1).skip(-1), example.getProbeType(), entityInformation.getCollectionName()));
}
/*
@ -375,6 +395,10 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> { @@ -375,6 +395,10 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
return mongoOperations.exists(query, example.getProbeType(), entityInformation.getCollectionName());
}
// -------------------------------------------------------------------------
// Utility methods
// -------------------------------------------------------------------------
private Query getIdQuery(Object id) {
return new Query(getIdCriteria(id));
}
@ -383,6 +407,11 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> { @@ -383,6 +407,11 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
return where(entityInformation.getIdAttribute()).is(id);
}
private Query getIdQuery(Iterable<? extends ID> ids) {
return new Query(new Criteria(entityInformation.getIdAttribute())
.in(Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList())));
}
private List<T> findAll(@Nullable Query query) {
if (query == null) {
@ -391,4 +420,5 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> { @@ -391,4 +420,5 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
return mongoOperations.find(query, entityInformation.getJavaType(), entityInformation.getCollectionName());
}
}

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

@ -17,12 +17,16 @@ package org.springframework.data.mongodb.repository.support; @@ -17,12 +17,16 @@ package org.springframework.data.mongodb.repository.support;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.reactivestreams.Publisher;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.domain.Example;
@ -38,9 +42,6 @@ import org.springframework.util.Assert; @@ -38,9 +42,6 @@ import org.springframework.util.Assert;
import com.mongodb.client.result.DeleteResult;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* Reactive repository base implementation for Mongo.
*
@ -66,52 +67,79 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement @@ -66,52 +67,79 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
this.mongoOperations = mongoOperations;
}
// -------------------------------------------------------------------------
// Methods from ReactiveCrudRepository
// -------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(java.lang.Object)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#save(java.lang.Object)
*/
@Override
public Mono<T> findById(ID id) {
public <S extends T> Mono<S> save(S entity) {
Assert.notNull(id, "The given id must not be null!");
Assert.notNull(entity, "Entity must not be null!");
return mongoOperations.findById(id, entityInformation.getJavaType(), entityInformation.getCollectionName());
if (entityInformation.isNew(entity)) {
return mongoOperations.insert(entity, entityInformation.getCollectionName());
}
return mongoOperations.save(entity, entityInformation.getCollectionName());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(org.reactivestreams.Publisher)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#saveAll(java.lang.Iterable)
*/
@Override
public Mono<T> findById(Publisher<ID> publisher) {
public <S extends T> Flux<S> saveAll(Iterable<S> entities) {
Assert.notNull(publisher, "The given id must not be null!");
Assert.notNull(entities, "The given Iterable of entities must not be null!");
return Mono.from(publisher).flatMap(
id -> mongoOperations.findById(id, entityInformation.getJavaType(), entityInformation.getCollectionName()));
Streamable<S> source = Streamable.of(entities);
return source.stream().allMatch(entityInformation::isNew) ? //
mongoOperations.insert(source.stream().collect(Collectors.toList()), entityInformation.getCollectionName()) : //
Flux.fromIterable(entities).flatMap(this::save);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#findOne(org.springframework.data.domain.Example)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#saveAll(org.reactivestreams.Publisher)
*/
@Override
public <S extends T> Mono<S> findOne(Example<S> example) {
public <S extends T> Flux<S> saveAll(Publisher<S> entityStream) {
Assert.notNull(example, "Sample must not be null!");
Assert.notNull(entityStream, "The given Publisher of entities must not be null!");
Query query = new Query(new Criteria().alike(example)) //
.collation(entityInformation.getCollation()) //
.limit(2);
return Flux.from(entityStream).flatMap(entity -> entityInformation.isNew(entity) ? //
mongoOperations.insert(entity, entityInformation.getCollectionName()).then(Mono.just(entity)) : //
mongoOperations.save(entity, entityInformation.getCollectionName()).then(Mono.just(entity)));
}
return mongoOperations.find(query, example.getProbeType(), entityInformation.getCollectionName()).buffer(2)
.map(vals -> {
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(java.lang.Object)
*/
@Override
public Mono<T> findById(ID id) {
if (vals.size() > 1) {
throw new IncorrectResultSizeDataAccessException(1);
}
return vals.iterator().next();
}).next();
Assert.notNull(id, "The given id must not be null!");
return mongoOperations.findById(id, entityInformation.getJavaType(), entityInformation.getCollectionName());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(org.reactivestreams.Publisher)
*/
@Override
public Mono<T> findById(Publisher<ID> publisher) {
Assert.notNull(publisher, "The given id must not be null!");
return Mono.from(publisher).flatMap(
id -> mongoOperations.findById(id, entityInformation.getJavaType(), entityInformation.getCollectionName()));
}
/*
@ -138,22 +166,6 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement @@ -138,22 +166,6 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
return Mono.from(publisher).flatMap(id -> mongoOperations.exists(getIdQuery(id), entityInformation.getJavaType(),
entityInformation.getCollectionName()));
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#exists(org.springframework.data.domain.Example)
*/
@Override
public <S extends T> Mono<Boolean> exists(Example<S> example) {
Assert.notNull(example, "Sample must not be null!");
Query query = new Query(new Criteria().alike(example)) //
.collation(entityInformation.getCollation());
return mongoOperations.exists(query, example.getProbeType(), entityInformation.getCollectionName());
}
/*
@ -174,8 +186,7 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement @@ -174,8 +186,7 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
Assert.notNull(ids, "The given Iterable of Id's must not be null!");
return findAll(new Query(new Criteria(entityInformation.getIdAttribute())
.in(Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList()))));
return findAll(getIdQuery(ids));
}
/*
@ -192,265 +203,270 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement @@ -192,265 +203,270 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveSortingRepository#findAll(org.springframework.data.domain.Sort)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#count()
*/
@Override
public Flux<T> findAll(Sort sort) {
Assert.notNull(sort, "Sort must not be null!");
return findAll(new Query().with(sort));
public Mono<Long> count() {
return mongoOperations.count(new Query(), entityInformation.getCollectionName());
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.ReactiveMongoRepository#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteById(java.lang.Object)
*/
@Override
public <S extends T> Flux<S> findAll(Example<S> example, Sort sort) {
Assert.notNull(example, "Sample must not be null!");
Assert.notNull(sort, "Sort must not be null!");
public Mono<Void> deleteById(ID id) {
Query query = new Query(new Criteria().alike(example)) //
.collation(entityInformation.getCollation()) //
.with(sort);
Assert.notNull(id, "The given id must not be null!");
return mongoOperations.find(query, example.getProbeType(), entityInformation.getCollectionName());
return mongoOperations
.remove(getIdQuery(id), entityInformation.getJavaType(), entityInformation.getCollectionName()).then();
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.ReactiveMongoRepository#findAll(org.springframework.data.domain.Example)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteById(org.reactivestreams.Publisher)
*/
@Override
public <S extends T> Flux<S> findAll(Example<S> example) {
public Mono<Void> deleteById(Publisher<ID> publisher) {
Assert.notNull(example, "Example must not be null!");
Assert.notNull(publisher, "Id must not be null!");
return findAll(example, Sort.unsorted());
return Mono.from(publisher).flatMap(id -> mongoOperations.remove(getIdQuery(id), entityInformation.getJavaType(),
entityInformation.getCollectionName())).then();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#count()
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#delete(java.lang.Object)
*/
@Override
public Mono<Long> count() {
return mongoOperations.count(new Query(), entityInformation.getCollectionName());
}
public Mono<Void> delete(T entity) {
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#count(org.springframework.data.domain.Example)
*/
@Override
public <S extends T> Mono<Long> count(Example<S> example) {
Assert.notNull(entity, "The given entity must not be null!");
Assert.notNull(example, "Sample must not be null!");
Mono<DeleteResult> remove = mongoOperations.remove(entity, entityInformation.getCollectionName());
Query query = new Query(new Criteria().alike(example)) //
.collation(entityInformation.getCollation());
if (entityInformation.isVersioned()) {
return mongoOperations.count(query, example.getProbeType(), entityInformation.getCollectionName());
remove = remove.handle((deleteResult, sink) -> {
if (deleteResult.wasAcknowledged() && deleteResult.getDeletedCount() == 0) {
sink.error(new OptimisticLockingFailureException(String.format(
"The entity with id %s with version %s in %s cannot be deleted! Was it modified or deleted in the meantime?",
entityInformation.getId(entity), entityInformation.getVersion(entity),
entityInformation.getCollectionName())));
} else {
sink.next(deleteResult);
}
});
}
return remove.then();
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.ReactiveMongoRepository#insert(java.lang.Object)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAllById(java.lang.Iterable)
*/
@Override
public <S extends T> Mono<S> insert(S entity) {
public Mono<Void> deleteAllById(Iterable<? extends ID> ids) {
Assert.notNull(entity, "Entity must not be null!");
Assert.notNull(ids, "The given Iterable of Id's must not be null!");
return mongoOperations.insert(entity, entityInformation.getCollectionName());
return mongoOperations
.remove(getIdQuery(ids), entityInformation.getJavaType(), entityInformation.getCollectionName()).then();
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.ReactiveMongoRepository#insert(java.lang.Iterable)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll(java.lang.Iterable)
*/
@Override
public <S extends T> Flux<S> insert(Iterable<S> entities) {
public Mono<Void> deleteAll(Iterable<? extends T> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null!");
List<S> source = Streamable.of(entities).stream().collect(StreamUtils.toUnmodifiableList());
Collection<?> idCollection = StreamUtils.createStreamFromIterator(entities.iterator()).map(entityInformation::getId)
.collect(Collectors.toList());
return source.isEmpty() ? Flux.empty() : Flux.from(mongoOperations.insertAll(source));
Criteria idsInCriteria = where(entityInformation.getIdAttribute()).in(idCollection);
return mongoOperations
.remove(new Query(idsInCriteria), entityInformation.getJavaType(), entityInformation.getCollectionName())
.then();
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.ReactiveMongoRepository#insert(org.reactivestreams.Publisher)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll(org.reactivestreams.Publisher)
*/
@Override
public <S extends T> Flux<S> insert(Publisher<S> entities) {
public Mono<Void> deleteAll(Publisher<? extends T> entityStream) {
Assert.notNull(entities, "The given Publisher of entities must not be null!");
Assert.notNull(entityStream, "The given Publisher of entities must not be null!");
return Flux.from(entities).flatMap(entity -> mongoOperations.insert(entity, entityInformation.getCollectionName()));
return Flux.from(entityStream)//
.map(entityInformation::getRequiredId)//
.flatMap(this::deleteById)//
.then();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#save(java.lang.Object)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll()
*/
@Override
public <S extends T> Mono<S> save(S entity) {
Assert.notNull(entity, "Entity must not be null!");
if (entityInformation.isNew(entity)) {
return mongoOperations.insert(entity, entityInformation.getCollectionName());
}
return mongoOperations.save(entity, entityInformation.getCollectionName());
public Mono<Void> deleteAll() {
return mongoOperations.remove(new Query(), entityInformation.getCollectionName()).then(Mono.empty());
}
// -------------------------------------------------------------------------
// Methods from ReactiveSortingRepository
// -------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#saveAll(java.lang.Iterable)
* @see org.springframework.data.repository.reactive.ReactiveSortingRepository#findAll(org.springframework.data.domain.Sort)
*/
@Override
public <S extends T> Flux<S> saveAll(Iterable<S> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null!");
public Flux<T> findAll(Sort sort) {
Streamable<S> source = Streamable.of(entities);
Assert.notNull(sort, "Sort must not be null!");
return source.stream().allMatch(entityInformation::isNew) ? //
mongoOperations.insert(source.stream().collect(Collectors.toList()), entityInformation.getCollectionName()) : //
Flux.fromIterable(entities).flatMap(this::save);
return findAll(new Query().with(sort));
}
// -------------------------------------------------------------------------
// Methods from ReactiveMongoRepository
// -------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#saveAll(org.reactivestreams.Publisher)
* @see org.springframework.data.mongodb.repository.ReactiveMongoRepository#insert(java.lang.Object)
*/
@Override
public <S extends T> Flux<S> saveAll(Publisher<S> entityStream) {
public <S extends T> Mono<S> insert(S entity) {
Assert.notNull(entityStream, "The given Publisher of entities must not be null!");
Assert.notNull(entity, "Entity must not be null!");
return Flux.from(entityStream).flatMap(entity -> entityInformation.isNew(entity) ? //
mongoOperations.insert(entity, entityInformation.getCollectionName()).then(Mono.just(entity)) : //
mongoOperations.save(entity, entityInformation.getCollectionName()).then(Mono.just(entity)));
return mongoOperations.insert(entity, entityInformation.getCollectionName());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteById(java.lang.Object)
* @see org.springframework.data.mongodb.repository.ReactiveMongoRepository#insert(java.lang.Iterable)
*/
@Override
public Mono<Void> deleteById(ID id) {
public <S extends T> Flux<S> insert(Iterable<S> entities) {
Assert.notNull(id, "The given id must not be null!");
Assert.notNull(entities, "The given Iterable of entities must not be null!");
return mongoOperations
.remove(getIdQuery(id), entityInformation.getJavaType(), entityInformation.getCollectionName()).then();
List<S> source = Streamable.of(entities).stream().collect(StreamUtils.toUnmodifiableList());
return source.isEmpty() ? Flux.empty() : Flux.from(mongoOperations.insertAll(source));
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteById(org.reactivestreams.Publisher)
* @see org.springframework.data.mongodb.repository.ReactiveMongoRepository#insert(org.reactivestreams.Publisher)
*/
@Override
public Mono<Void> deleteById(Publisher<ID> publisher) {
public <S extends T> Flux<S> insert(Publisher<S> entities) {
Assert.notNull(publisher, "Id must not be null!");
Assert.notNull(entities, "The given Publisher of entities must not be null!");
return Mono.from(publisher).flatMap(id -> mongoOperations.remove(getIdQuery(id), entityInformation.getJavaType(),
entityInformation.getCollectionName())).then();
return Flux.from(entities).flatMap(entity -> mongoOperations.insert(entity, entityInformation.getCollectionName()));
}
// -------------------------------------------------------------------------
// Methods from ReactiveMongoRepository
// -------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#delete(java.lang.Object)
* @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#findOne(org.springframework.data.domain.Example)
*/
@Override
public Mono<Void> delete(T entity) {
Assert.notNull(entity, "The given entity must not be null!");
Mono<DeleteResult> remove = mongoOperations.remove(entity, entityInformation.getCollectionName());
public <S extends T> Mono<S> findOne(Example<S> example) {
if (entityInformation.isVersioned()) {
Assert.notNull(example, "Sample must not be null!");
remove = remove.handle((deleteResult, sink) -> {
Query query = new Query(new Criteria().alike(example)) //
.collation(entityInformation.getCollation()) //
.limit(2);
if (deleteResult.wasAcknowledged() && deleteResult.getDeletedCount() == 0) {
sink.error(new OptimisticLockingFailureException(String.format(
"The entity with id %s with version %s in %s cannot be deleted! Was it modified or deleted in the meantime?",
entityInformation.getId(entity), entityInformation.getVersion(entity),
entityInformation.getCollectionName())));
} else {
sink.next(deleteResult);
}
});
}
return mongoOperations.find(query, example.getProbeType(), entityInformation.getCollectionName()).buffer(2)
.map(vals -> {
return remove.then();
if (vals.size() > 1) {
throw new IncorrectResultSizeDataAccessException(1);
}
return vals.iterator().next();
}).next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll(java.lang.Iterable)
* @see org.springframework.data.mongodb.repository.ReactiveMongoRepository#findAll(org.springframework.data.domain.Example)
*/
@Override
public Mono<Void> deleteAll(Iterable<? extends T> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null!");
Collection<?> idCollection = StreamUtils.createStreamFromIterator(entities.iterator())
.map(entityInformation::getId)
.collect(Collectors.toList());
public <S extends T> Flux<S> findAll(Example<S> example) {
Criteria idsInCriteria = where(entityInformation.getIdAttribute()).in(idCollection);
Assert.notNull(example, "Example must not be null!");
return mongoOperations
.remove(new Query(idsInCriteria), entityInformation.getJavaType(), entityInformation.getCollectionName())
.then();
return findAll(example, Sort.unsorted());
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.ReactiveMongoRepository#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort)
*/
@Override
public Mono<Void> deleteAllById(Iterable<? extends ID> ids) {
public <S extends T> Flux<S> findAll(Example<S> example, Sort sort) {
Assert.notNull(ids, "The given Iterable of ids must not be null!");
Assert.notNull(example, "Sample must not be null!");
Assert.notNull(sort, "Sort must not be null!");
Collection<?> idCollection = StreamUtils.createStreamFromIterator(ids.iterator()).collect(Collectors.toList());
Criteria idsInCriteria = where(entityInformation.getIdAttribute()).in(idCollection);
Query query = new Query(new Criteria().alike(example)) //
.collation(entityInformation.getCollation()) //
.with(sort);
return mongoOperations
.remove(new Query(idsInCriteria), entityInformation.getJavaType(), entityInformation.getCollectionName())
.then();
return mongoOperations.find(query, example.getProbeType(), entityInformation.getCollectionName());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll(org.reactivestreams.Publisher)
* @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#count(org.springframework.data.domain.Example)
*/
@Override
public Mono<Void> deleteAll(Publisher<? extends T> entityStream) {
public <S extends T> Mono<Long> count(Example<S> example) {
Assert.notNull(entityStream, "The given Publisher of entities must not be null!");
Assert.notNull(example, "Sample must not be null!");
return Flux.from(entityStream)//
.map(entityInformation::getRequiredId)//
.flatMap(this::deleteById)//
.then();
Query query = new Query(new Criteria().alike(example)) //
.collation(entityInformation.getCollation());
return mongoOperations.count(query, example.getProbeType(), entityInformation.getCollectionName());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll()
* @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#exists(org.springframework.data.domain.Example)
*/
@Override
public Mono<Void> deleteAll() {
return mongoOperations.remove(new Query(), entityInformation.getCollectionName()).then(Mono.empty());
public <S extends T> Mono<Boolean> exists(Example<S> example) {
Assert.notNull(example, "Sample must not be null!");
Query query = new Query(new Criteria().alike(example)) //
.collation(entityInformation.getCollation());
return mongoOperations.exists(query, example.getProbeType(), entityInformation.getCollectionName());
}
private Query getIdQuery(Object id) {
return new Query(getIdCriteria(id));
}
@ -459,6 +475,13 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement @@ -459,6 +475,13 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
return where(entityInformation.getIdAttribute()).is(id);
}
private Query getIdQuery(Iterable<? extends ID> ids) {
Collection<?> idCollection = StreamUtils.createStreamFromIterator(ids.iterator()).collect(Collectors.toList());
Criteria idsInCriteria = where(entityInformation.getIdAttribute()).in(idCollection);
return new Query(idsInCriteria);
}
private Flux<T> findAll(Query query) {
return mongoOperations.find(query, entityInformation.getJavaType(), entityInformation.getCollectionName());

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

@ -15,7 +15,6 @@ @@ -15,7 +15,6 @@
*/
package org.springframework.data.mongodb.repository;
import static java.util.Arrays.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.domain.Sort.Direction.*;
import static org.springframework.data.mongodb.core.query.Criteria.*;
@ -80,10 +79,10 @@ import com.mongodb.reactivestreams.client.MongoClient; @@ -80,10 +79,10 @@ import com.mongodb.reactivestreams.client.MongoClient;
* @author Jens Schauder
*/
@ExtendWith({ MongoClientExtension.class, SpringExtension.class })
public class ReactiveMongoRepositoryTests {
class ReactiveMongoRepositoryTests {
public static final int PERSON_COUNT = 7;
static @Client MongoClient mongoClient;
private static final int PERSON_COUNT = 7;
private static @Client MongoClient mongoClient;
@Autowired ReactiveMongoTemplate template;
@ -91,8 +90,8 @@ public class ReactiveMongoRepositoryTests { @@ -91,8 +90,8 @@ public class ReactiveMongoRepositoryTests {
@Autowired ReactiveContactRepository contactRepository;
@Autowired ReactiveCappedCollectionRepository cappedRepository;
Person dave, oliver, carter, boyd, stefan, leroi, alicia;
QPerson person = QPerson.person;
private Person dave, oliver, carter, boyd, stefan, leroi, alicia;
private QPerson person = QPerson.person;
@Configuration
static class Config extends AbstractReactiveMongoConfiguration {
@ -142,59 +141,59 @@ public class ReactiveMongoRepositoryTests { @@ -142,59 +141,59 @@ public class ReactiveMongoRepositoryTests {
}
@BeforeAll
public static void cleanDb() {
static void cleanDb() {
MongoTestUtils.createOrReplaceCollectionNow("reactive", "person", mongoClient);
MongoTestUtils.createOrReplaceCollectionNow("reactive", "capped", mongoClient);
}
@BeforeEach
public void setUp() throws Exception {
void setUp() throws Exception {
repository.deleteAll().as(StepVerifier::create).verifyComplete();
dave = new Person("Dave", "Matthews", 42);
oliver = new Person("Oliver August", "Matthews", 4);
carter = new Person("Carter", "Beauford", 49);
carter.setSkills(asList("Drums", "percussion", "vocals"));
carter.setSkills(Arrays.asList("Drums", "percussion", "vocals"));
Thread.sleep(10);
boyd = new Person("Boyd", "Tinsley", 45);
boyd.setSkills(asList("Violin", "Electric Violin", "Viola", "Mandolin", "Vocals", "Guitar"));
boyd.setSkills(Arrays.asList("Violin", "Electric Violin", "Viola", "Mandolin", "Vocals", "Guitar"));
stefan = new Person("Stefan", "Lessard", 34);
leroi = new Person("Leroi", "Moore", 41);
alicia = new Person("Alicia", "Keys", 30, Sex.FEMALE);
repository.saveAll(asList(oliver, carter, boyd, stefan, leroi, alicia, dave)).as(StepVerifier::create) //
repository.saveAll(Arrays.asList(oliver, carter, boyd, stefan, leroi, alicia, dave)).as(StepVerifier::create) //
.expectNextCount(PERSON_COUNT) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void shouldFindByLastName() {
void shouldFindByLastName() {
repository.findByLastname(dave.getLastname()).as(StepVerifier::create).expectNextCount(2).verifyComplete();
}
@Test // DATAMONGO-1444
public void shouldFindOneByLastName() {
void shouldFindOneByLastName() {
repository.findOneByLastname(carter.getLastname()).as(StepVerifier::create).expectNext(carter).verifyComplete();
}
@Test // DATAMONGO-1444
public void shouldFindOneByPublisherOfLastName() {
void shouldFindOneByPublisherOfLastName() {
repository.findByLastname(Mono.just(carter.getLastname())).as(StepVerifier::create).expectNext(carter)
.verifyComplete();
}
@Test // DATAMONGO-1444
public void shouldFindByPublisherOfLastNameIn() {
void shouldFindByPublisherOfLastNameIn() {
repository.findByLastnameIn(Flux.just(carter.getLastname(), dave.getLastname())).as(StepVerifier::create) //
.expectNextCount(3) //
.verifyComplete();
}
@Test // DATAMONGO-1444
public void shouldFindByPublisherOfLastNameInAndAgeGreater() {
void shouldFindByPublisherOfLastNameInAndAgeGreater() {
repository.findByLastnameInAndAgeGreaterThan(Flux.just(carter.getLastname(), dave.getLastname()), 41)
.as(StepVerifier::create) //
@ -203,7 +202,7 @@ public class ReactiveMongoRepositoryTests { @@ -203,7 +202,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-1444
public void shouldFindUsingPublishersInStringQuery() {
void shouldFindUsingPublishersInStringQuery() {
repository.findStringQuery(Flux.just("Beauford", "Matthews"), Mono.just(41)).as(StepVerifier::create) //
.expectNextCount(2) //
@ -211,7 +210,7 @@ public class ReactiveMongoRepositoryTests { @@ -211,7 +210,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-1444
public void shouldFindByLastNameAndSort() {
void shouldFindByLastNameAndSort() {
repository.findByLastname("Matthews", Sort.by(ASC, "age")).as(StepVerifier::create) //
.expectNext(oliver, dave) //
@ -223,7 +222,7 @@ public class ReactiveMongoRepositoryTests { @@ -223,7 +222,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-1444
public void shouldUseTailableCursor() throws Exception {
void shouldUseTailableCursor() throws Exception {
template.dropCollection(Capped.class) //
.then(template.createCollection(Capped.class, //
@ -248,7 +247,7 @@ public class ReactiveMongoRepositoryTests { @@ -248,7 +247,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-1444
public void shouldUseTailableCursorWithProjection() throws Exception {
void shouldUseTailableCursorWithProjection() throws Exception {
template.dropCollection(Capped.class) //
.then(template.createCollection(Capped.class, //
@ -279,7 +278,7 @@ public class ReactiveMongoRepositoryTests { @@ -279,7 +278,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-2080
public void shouldUseTailableCursorWithDtoProjection() {
void shouldUseTailableCursorWithDtoProjection() {
template.dropCollection(Capped.class) //
.then(template.createCollection(Capped.class, //
@ -292,7 +291,7 @@ public class ReactiveMongoRepositoryTests { @@ -292,7 +291,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-1444
public void findsPeopleByLocationWithinCircle() {
void findsPeopleByLocationWithinCircle() {
Point point = new Point(-73.99171, 40.738868);
dave.setLocation(point);
@ -304,7 +303,7 @@ public class ReactiveMongoRepositoryTests { @@ -304,7 +303,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-1444
public void findsPeopleByPageableLocationWithinCircle() {
void findsPeopleByPageableLocationWithinCircle() {
Point point = new Point(-73.99171, 40.738868);
dave.setLocation(point);
@ -317,7 +316,7 @@ public class ReactiveMongoRepositoryTests { @@ -317,7 +316,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-1444
public void findsPeopleGeoresultByLocationWithinBox() {
void findsPeopleGeoresultByLocationWithinBox() {
Point point = new Point(-73.99171, 40.738868);
dave.setLocation(point);
@ -332,7 +331,7 @@ public class ReactiveMongoRepositoryTests { @@ -332,7 +331,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-1444
public void findsPeoplePageableGeoresultByLocationWithinBox() throws InterruptedException {
void findsPeoplePageableGeoresultByLocationWithinBox() throws InterruptedException {
Point point = new Point(-73.99171, 40.738868);
dave.setLocation(point);
@ -352,7 +351,7 @@ public class ReactiveMongoRepositoryTests { @@ -352,7 +351,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-1444
public void findsPeopleByLocationWithinBox() throws InterruptedException {
void findsPeopleByLocationWithinBox() throws InterruptedException {
Point point = new Point(-73.99171, 40.738868);
dave.setLocation(point);
@ -368,23 +367,23 @@ public class ReactiveMongoRepositoryTests { @@ -368,23 +367,23 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-1865
public void shouldErrorOnFindOneWithNonUniqueResult() {
void shouldErrorOnFindOneWithNonUniqueResult() {
repository.findOneByLastname(dave.getLastname()).as(StepVerifier::create)
.expectError(IncorrectResultSizeDataAccessException.class).verify();
}
@Test // DATAMONGO-1865
public void shouldReturnFirstFindFirstWithMoreResults() {
void shouldReturnFirstFindFirstWithMoreResults() {
repository.findFirstByLastname(dave.getLastname()).as(StepVerifier::create).expectNextCount(1).verifyComplete();
}
@Test // DATAMONGO-2030
public void shouldReturnExistsBy() {
void shouldReturnExistsBy() {
repository.existsByLastname(dave.getLastname()).as(StepVerifier::create).expectNext(true).verifyComplete();
}
@Test // DATAMONGO-1979
public void findAppliesAnnotatedSort() {
void findAppliesAnnotatedSort() {
repository.findByAgeGreaterThan(40).collectList().as(StepVerifier::create).consumeNextWith(result -> {
assertThat(result).containsSequence(carter, boyd, dave, leroi);
@ -392,7 +391,7 @@ public class ReactiveMongoRepositoryTests { @@ -392,7 +391,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-1979
public void findWithSortOverwritesAnnotatedSort() {
void findWithSortOverwritesAnnotatedSort() {
repository.findByAgeGreaterThan(40, Sort.by(Direction.ASC, "age")).collectList().as(StepVerifier::create)
.consumeNextWith(result -> {
@ -401,7 +400,7 @@ public class ReactiveMongoRepositoryTests { @@ -401,7 +400,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-2181
public void considersRepositoryCollectionName() {
void considersRepositoryCollectionName() {
repository.deleteAll() //
.as(StepVerifier::create) //
@ -413,7 +412,7 @@ public class ReactiveMongoRepositoryTests { @@ -413,7 +412,7 @@ public class ReactiveMongoRepositoryTests {
leroi.id = null;
boyd.id = null;
contactRepository.saveAll(asList(leroi, boyd)) //
contactRepository.saveAll(Arrays.asList(leroi, boyd)) //
.as(StepVerifier::create) //
.expectNextCount(2) //
.verifyComplete();
@ -430,9 +429,9 @@ public class ReactiveMongoRepositoryTests { @@ -430,9 +429,9 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-2182
public void shouldFindPersonsWhenUsingQueryDslPerdicatedOnIdProperty() {
void shouldFindPersonsWhenUsingQueryDslPerdicatedOnIdProperty() {
repository.findAll(person.id.in(asList(dave.id, carter.id))) //
repository.findAll(person.id.in(Arrays.asList(dave.id, carter.id))) //
.collectList() //
.as(StepVerifier::create) //
.assertNext(actual -> {
@ -441,24 +440,19 @@ public class ReactiveMongoRepositoryTests { @@ -441,24 +440,19 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-2153
public void findListOfSingleValue() {
void findListOfSingleValue() {
repository.findAllLastnames() //
.collectList() //
.as(StepVerifier::create) //
.assertNext(actual -> {
assertThat(actual) //
.contains("Lessard") //
.contains("Keys") //
.contains("Tinsley") //
.contains("Beauford") //
.contains("Moore") //
.contains("Matthews");
assertThat(actual)
.contains("Lessard", "Keys", "Tinsley", "Beauford", "Moore", "Matthews");
}).verifyComplete();
}
@Test // DATAMONGO-2153
public void annotatedAggregationWithPlaceholderValue() {
void annotatedAggregationWithPlaceholderValue() {
repository.groupByLastnameAnd("firstname") //
.collectList() //
@ -470,12 +464,12 @@ public class ReactiveMongoRepositoryTests { @@ -470,12 +464,12 @@ public class ReactiveMongoRepositoryTests {
.contains(new PersonAggregate("Tinsley", "Boyd")) //
.contains(new PersonAggregate("Beauford", "Carter")) //
.contains(new PersonAggregate("Moore", "Leroi")) //
.contains(new PersonAggregate("Matthews", asList("Dave", "Oliver August")));
.contains(new PersonAggregate("Matthews", Arrays.asList("Dave", "Oliver August")));
}).verifyComplete();
}
@Test // DATAMONGO-2153
public void annotatedAggregationWithSort() {
void annotatedAggregationWithSort() {
repository.groupByLastnameAnd("firstname", Sort.by("lastname")) //
.collectList() //
@ -486,7 +480,7 @@ public class ReactiveMongoRepositoryTests { @@ -486,7 +480,7 @@ public class ReactiveMongoRepositoryTests {
new PersonAggregate("Beauford", "Carter"), //
new PersonAggregate("Keys", "Alicia"), //
new PersonAggregate("Lessard", "Stefan"), //
new PersonAggregate("Matthews", asList("Dave", "Oliver August")), //
new PersonAggregate("Matthews", Arrays.asList("Dave", "Oliver August")), //
new PersonAggregate("Moore", "Leroi"), //
new PersonAggregate("Tinsley", "Boyd"));
}) //
@ -494,7 +488,7 @@ public class ReactiveMongoRepositoryTests { @@ -494,7 +488,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-2153
public void annotatedAggregationWithPageable() {
void annotatedAggregationWithPageable() {
repository.groupByLastnameAnd("firstname", PageRequest.of(1, 2, Sort.by("lastname"))) //
.collectList() //
@ -503,13 +497,13 @@ public class ReactiveMongoRepositoryTests { @@ -503,13 +497,13 @@ public class ReactiveMongoRepositoryTests {
assertThat(actual) //
.containsExactly( //
new PersonAggregate("Lessard", "Stefan"), //
new PersonAggregate("Matthews", asList("Dave", "Oliver August")));
new PersonAggregate("Matthews", Arrays.asList("Dave", "Oliver August")));
}) //
.verifyComplete();
}
@Test // DATAMONGO-2153
public void annotatedAggregationWithSingleSimpleResult() {
void annotatedAggregationWithSingleSimpleResult() {
repository.sumAge() //
.as(StepVerifier::create) //
@ -518,7 +512,7 @@ public class ReactiveMongoRepositoryTests { @@ -518,7 +512,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-2153
public void annotatedAggregationWithAggregationResultAsReturnType() {
void annotatedAggregationWithAggregationResultAsReturnType() {
repository.sumAgeAndReturnRawResult() //
.as(StepVerifier::create) //
@ -527,7 +521,7 @@ public class ReactiveMongoRepositoryTests { @@ -527,7 +521,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-2153
public void annotatedAggregationWithAggregationResultAsReturnTypeAndProjection() {
void annotatedAggregationWithAggregationResultAsReturnTypeAndProjection() {
repository.sumAgeAndReturnSumWrapper() //
.as(StepVerifier::create) //
@ -536,7 +530,7 @@ public class ReactiveMongoRepositoryTests { @@ -536,7 +530,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-2374
public void findsWithNativeProjection() {
void findsWithNativeProjection() {
repository.findDocumentById(dave.getId()) //
.as(StepVerifier::create) //
@ -546,7 +540,7 @@ public class ReactiveMongoRepositoryTests { @@ -546,7 +540,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-2153
public void annotatedAggregationWithAggregationResultAsMap() {
void annotatedAggregationWithAggregationResultAsMap() {
repository.sumAgeAndReturnSumAsMap() //
.as(StepVerifier::create) //
@ -556,7 +550,7 @@ public class ReactiveMongoRepositoryTests { @@ -556,7 +550,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-2403
public void annotatedAggregationExtractingSimpleValueIsEmptyForEmptyDocument() {
void annotatedAggregationExtractingSimpleValueIsEmptyForEmptyDocument() {
Person p = new Person("project-on-lastanme", null);
repository.save(p).then().as(StepVerifier::create).verifyComplete();
@ -567,7 +561,7 @@ public class ReactiveMongoRepositoryTests { @@ -567,7 +561,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-2403
public void annotatedAggregationSkipsEmptyDocumentsWhenExtractingSimpleValue() {
void annotatedAggregationSkipsEmptyDocumentsWhenExtractingSimpleValue() {
String firstname = "project-on-lastanme";
@ -578,7 +572,7 @@ public class ReactiveMongoRepositoryTests { @@ -578,7 +572,7 @@ public class ReactiveMongoRepositoryTests {
Person p3 = new Person(firstname, null);
p3.setEmail("p3@example.com");
repository.saveAll(asList(p1, p2, p3)).then().as(StepVerifier::create).verifyComplete();
repository.saveAll(Arrays.asList(p1, p2, p3)).then().as(StepVerifier::create).verifyComplete();
repository.projectToLastnameAndRemoveId(firstname) //
.as(StepVerifier::create) //
@ -586,7 +580,7 @@ public class ReactiveMongoRepositoryTests { @@ -586,7 +580,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-2406
public void deleteByShouldHandleVoidResultTypeCorrectly() {
void deleteByShouldHandleVoidResultTypeCorrectly() {
repository.deleteByLastname(dave.getLastname()) //
.as(StepVerifier::create) //
@ -598,7 +592,7 @@ public class ReactiveMongoRepositoryTests { @@ -598,7 +592,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-1997
public void deleteByShouldAllowDeletedCountAsResult() {
void deleteByShouldAllowDeletedCountAsResult() {
repository.deleteCountByLastname(dave.getLastname()) //
.as(StepVerifier::create) //
@ -607,7 +601,7 @@ public class ReactiveMongoRepositoryTests { @@ -607,7 +601,7 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-1997
public void deleteByShouldAllowSingleDocumentRemovalCorrectly() {
void deleteByShouldAllowSingleDocumentRemovalCorrectly() {
repository.deleteSinglePersonByLastname(carter.getLastname()) //
.as(StepVerifier::create) //
@ -620,9 +614,9 @@ public class ReactiveMongoRepositoryTests { @@ -620,9 +614,9 @@ public class ReactiveMongoRepositoryTests {
}
@Test // DATAMONGO-2652
public void deleteAllById() {
void deleteAllById() {
repository.deleteAllById(asList(carter.id, dave.id)) //
repository.deleteAllById(Arrays.asList(carter.id, dave.id)) //
.as(StepVerifier::create) //
.verifyComplete();
@ -731,7 +725,7 @@ public class ReactiveMongoRepositoryTests { @@ -731,7 +725,7 @@ public class ReactiveMongoRepositoryTests {
String key;
double random;
public Capped(String key, double random) {
Capped(String key, double random) {
this.key = key;
this.random = random;
}

70
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java

@ -61,10 +61,10 @@ import org.springframework.transaction.support.TransactionTemplate; @@ -61,10 +61,10 @@ import org.springframework.transaction.support.TransactionTemplate;
* @author Jens Schauder
*/
@ExtendWith({ MongoTemplateExtension.class, MongoServerCondition.class })
public class SimpleMongoRepositoryTests {
class SimpleMongoRepositoryTests {
@Template(initialEntitySet = Person.class) //
static MongoTestTemplate template;
private static MongoTestTemplate template;
private Person oliver, dave, carter, boyd, stefan, leroi, alicia;
private List<Person> all;
@ -74,7 +74,7 @@ public class SimpleMongoRepositoryTests { @@ -74,7 +74,7 @@ public class SimpleMongoRepositoryTests {
template);
@BeforeEach
public void setUp() {
void setUp() {
repository.deleteAll();
@ -90,17 +90,17 @@ public class SimpleMongoRepositoryTests { @@ -90,17 +90,17 @@ public class SimpleMongoRepositoryTests {
}
@Test
public void findAllFromCustomCollectionName() {
assertThat(repository.findAll()).hasSize(all.size());
void findAllFromCustomCollectionName() {
assertThat(repository.findAll()).hasSameSizeAs(all);
}
@Test
public void findOneFromCustomCollectionName() {
assertThat(repository.findById(dave.getId()).get()).isEqualTo(dave);
void findOneFromCustomCollectionName() {
assertThat(repository.findById(dave.getId())).contains(dave);
}
@Test
public void deleteFromCustomCollectionName() {
void deleteFromCustomCollectionName() {
repository.delete(dave);
@ -108,7 +108,7 @@ public class SimpleMongoRepositoryTests { @@ -108,7 +108,7 @@ public class SimpleMongoRepositoryTests {
}
@Test
public void deleteByIdFromCustomCollectionName() {
void deleteByIdFromCustomCollectionName() {
repository.deleteById(dave.getId());
@ -116,7 +116,7 @@ public class SimpleMongoRepositoryTests { @@ -116,7 +116,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1054
public void shouldInsertSingle() {
void shouldInsertSingle() {
String randomId = UUID.randomUUID().toString();
@ -127,7 +127,7 @@ public class SimpleMongoRepositoryTests { @@ -127,7 +127,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1054
public void shouldInsertMultipleFromList() {
void shouldInsertMultipleFromList() {
String randomId = UUID.randomUUID().toString();
Map<String, Person> idToPerson = new HashMap<String, Person>();
@ -141,12 +141,12 @@ public class SimpleMongoRepositoryTests { @@ -141,12 +141,12 @@ public class SimpleMongoRepositoryTests {
List<Person> saved = repository.insert(persons);
assertThat(saved).hasSize(persons.size());
assertThat(saved).hasSameSizeAs(persons);
assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved);
}
@Test // DATAMONGO-1054
public void shouldInsertMutlipleFromSet() {
void shouldInsertMutlipleFromSet() {
String randomId = UUID.randomUUID().toString();
Map<String, Person> idToPerson = new HashMap<String, Person>();
@ -160,12 +160,12 @@ public class SimpleMongoRepositoryTests { @@ -160,12 +160,12 @@ public class SimpleMongoRepositoryTests {
List<Person> saved = repository.insert(persons);
assertThat(saved).hasSize(persons.size());
assertThat(saved).hasSameSizeAs(persons);
assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved);
}
@Test // DATAMONGO-1245, DATAMONGO-1464
public void findByExampleShouldLookUpEntriesCorrectly() {
void findByExampleShouldLookUpEntriesCorrectly() {
Person sample = new Person();
sample.setLastname("Matthews");
@ -178,7 +178,7 @@ public class SimpleMongoRepositoryTests { @@ -178,7 +178,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1464
public void findByExampleMultiplePagesShouldLookUpEntriesCorrectly() {
void findByExampleMultiplePagesShouldLookUpEntriesCorrectly() {
Person sample = new Person();
sample.setLastname("Matthews");
@ -191,7 +191,7 @@ public class SimpleMongoRepositoryTests { @@ -191,7 +191,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1245
public void findAllByExampleShouldLookUpEntriesCorrectly() {
void findAllByExampleShouldLookUpEntriesCorrectly() {
Person sample = new Person();
sample.setLastname("Matthews");
@ -201,7 +201,7 @@ public class SimpleMongoRepositoryTests { @@ -201,7 +201,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1245
public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingNestedObject() {
void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingNestedObject() {
dave.setAddress(new Address("1600 Pennsylvania Ave NW", "20500", "Washington"));
repository.save(dave);
@ -217,7 +217,7 @@ public class SimpleMongoRepositoryTests { @@ -217,7 +217,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1245
public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingPartialNestedObject() {
void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingPartialNestedObject() {
dave.setAddress(new Address("1600 Pennsylvania Ave NW", "20500", "Washington"));
repository.save(dave);
@ -233,7 +233,7 @@ public class SimpleMongoRepositoryTests { @@ -233,7 +233,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1245
public void findAllByExampleShouldNotFindEntriesWhenUsingPartialNestedObjectInStrictMode() {
void findAllByExampleShouldNotFindEntriesWhenUsingPartialNestedObjectInStrictMode() {
dave.setAddress(new Address("1600 Pennsylvania Ave NW", "20500", "Washington"));
repository.save(dave);
@ -248,7 +248,7 @@ public class SimpleMongoRepositoryTests { @@ -248,7 +248,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1245
public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingNestedObjectInStrictMode() {
void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingNestedObjectInStrictMode() {
dave.setAddress(new Address("1600 Pennsylvania Ave NW", "20500", "Washington"));
repository.save(dave);
@ -263,7 +263,7 @@ public class SimpleMongoRepositoryTests { @@ -263,7 +263,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1245
public void findAllByExampleShouldRespectStringMatchMode() {
void findAllByExampleShouldRespectStringMatchMode() {
Person sample = new Person();
sample.setLastname("Mat");
@ -275,7 +275,7 @@ public class SimpleMongoRepositoryTests { @@ -275,7 +275,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1245
public void findAllByExampleShouldResolveDbRefCorrectly() {
void findAllByExampleShouldResolveDbRefCorrectly() {
User user = new User();
user.setId("c0nf1ux");
@ -295,7 +295,7 @@ public class SimpleMongoRepositoryTests { @@ -295,7 +295,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1245
public void findAllByExampleShouldResolveLegacyCoordinatesCorrectly() {
void findAllByExampleShouldResolveLegacyCoordinatesCorrectly() {
Person megan = new Person("megan", "tarash");
megan.setLocation(new Point(41.85003D, -87.65005D));
@ -310,7 +310,7 @@ public class SimpleMongoRepositoryTests { @@ -310,7 +310,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1245
public void findAllByExampleShouldResolveGeoJsonCoordinatesCorrectly() {
void findAllByExampleShouldResolveGeoJsonCoordinatesCorrectly() {
Person megan = new Person("megan", "tarash");
megan.setLocation(new GeoJsonPoint(41.85003D, -87.65005D));
@ -325,7 +325,7 @@ public class SimpleMongoRepositoryTests { @@ -325,7 +325,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1245
public void findAllByExampleShouldProcessInheritanceCorrectly() {
void findAllByExampleShouldProcessInheritanceCorrectly() {
PersonExtended reference = new PersonExtended();
reference.setLastname("Matthews");
@ -341,7 +341,7 @@ public class SimpleMongoRepositoryTests { @@ -341,7 +341,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1245
public void findOneByExampleShouldLookUpEntriesCorrectly() {
void findOneByExampleShouldLookUpEntriesCorrectly() {
Person sample = new Person();
sample.setFirstname("Dave");
@ -352,7 +352,7 @@ public class SimpleMongoRepositoryTests { @@ -352,7 +352,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1245
public void existsByExampleShouldLookUpEntriesCorrectly() {
void existsByExampleShouldLookUpEntriesCorrectly() {
Person sample = new Person();
sample.setFirstname("Dave");
@ -363,7 +363,7 @@ public class SimpleMongoRepositoryTests { @@ -363,7 +363,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1245
public void countByExampleShouldLookUpEntriesCorrectly() {
void countByExampleShouldLookUpEntriesCorrectly() {
Person sample = new Person();
sample.setLastname("Matthews");
@ -373,7 +373,7 @@ public class SimpleMongoRepositoryTests { @@ -373,7 +373,7 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-1896
public void saveAllUsesEntityCollection() {
void saveAllUsesEntityCollection() {
Person first = new PersonExtended();
first.setEmail("foo@bar.com");
@ -393,7 +393,7 @@ public class SimpleMongoRepositoryTests { @@ -393,7 +393,7 @@ public class SimpleMongoRepositoryTests {
@Test // DATAMONGO-2130
@EnableIfReplicaSetAvailable
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
public void countShouldBePossibleInTransaction() {
void countShouldBePossibleInTransaction() {
MongoTransactionManager txmgr = new MongoTransactionManager(template.getMongoDbFactory());
TransactionTemplate tt = new TransactionTemplate(txmgr);
@ -417,7 +417,7 @@ public class SimpleMongoRepositoryTests { @@ -417,7 +417,7 @@ public class SimpleMongoRepositoryTests {
@Test // DATAMONGO-2130
@EnableIfReplicaSetAvailable
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
public void existsShouldBePossibleInTransaction() {
void existsShouldBePossibleInTransaction() {
MongoTransactionManager txmgr = new MongoTransactionManager(template.getMongoDbFactory());
TransactionTemplate tt = new TransactionTemplate(txmgr);
@ -437,14 +437,12 @@ public class SimpleMongoRepositoryTests { @@ -437,14 +437,12 @@ public class SimpleMongoRepositoryTests {
}
@Test // DATAMONGO-2652
public void deleteAllByIds() {
void deleteAllByIds() {
repository.deleteAllById(asList(dave.getId(), carter.getId()));
assertThat(repository.findAll()) //
.hasSize(all.size() - 2) //
.doesNotContain(dave) //
.doesNotContain(carter);
.hasSize(all.size() - 2).doesNotContain(dave, carter);
}
private void assertThatAllReferencePersonsWereStoredCorrectly(Map<String, Person> references, List<Person> saved) {

Loading…
Cancel
Save