Browse Source

Return saved entity reference instead of original reference.

Make SimpleReactiveMongoRepository#saveAll(Publisher<S>) return the saved entity references instead of the original references.

Closes #3609
Original pull request: #3611.
pull/3615/head
Clément Petit 5 years ago committed by Mark Paluch
parent
commit
3e78ce212a
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 5
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java
  2. 59
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java

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

@ -50,6 +50,7 @@ import com.mongodb.client.result.DeleteResult;
* @author Christoph Strobl * @author Christoph Strobl
* @author Ruben J Garcia * @author Ruben J Garcia
* @author Jens Schauder * @author Jens Schauder
* @author Clément Petit
* @since 2.0 * @since 2.0
*/ */
public class SimpleReactiveMongoRepository<T, ID extends Serializable> implements ReactiveMongoRepository<T, ID> { public class SimpleReactiveMongoRepository<T, ID extends Serializable> implements ReactiveMongoRepository<T, ID> {
@ -113,8 +114,8 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
Assert.notNull(entityStream, "The given Publisher of entities must not be null!"); Assert.notNull(entityStream, "The given Publisher of entities must not be null!");
return Flux.from(entityStream).flatMap(entity -> entityInformation.isNew(entity) ? // return Flux.from(entityStream).flatMap(entity -> entityInformation.isNew(entity) ? //
mongoOperations.insert(entity, entityInformation.getCollectionName()).then(Mono.just(entity)) : // mongoOperations.insert(entity, entityInformation.getCollectionName()) : //
mongoOperations.save(entity, entityInformation.getCollectionName()).then(Mono.just(entity))); mongoOperations.save(entity, entityInformation.getCollectionName()));
} }
/* /*

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

@ -20,16 +20,19 @@ import static org.springframework.data.domain.ExampleMatcher.*;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Value;
import lombok.With;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.test.StepVerifier; import reactor.test.StepVerifier;
import java.util.Arrays; import java.util.Arrays;
import javax.annotation.Nullable;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
@ -44,7 +47,6 @@ import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate; import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.repository.support.ReactiveMongoRepositoryFactory; import org.springframework.data.mongodb.repository.support.ReactiveMongoRepositoryFactory;
import org.springframework.data.mongodb.repository.support.SimpleReactiveMongoRepository; import org.springframework.data.mongodb.repository.support.SimpleReactiveMongoRepository;
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
import org.springframework.data.repository.query.ReactiveQueryMethodEvaluationContextProvider; import org.springframework.data.repository.query.ReactiveQueryMethodEvaluationContextProvider;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
@ -56,6 +58,7 @@ import org.springframework.util.ClassUtils;
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl * @author Christoph Strobl
* @author Ruben J Garcia * @author Ruben J Garcia
* @author Clément Petit
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration("classpath:reactive-infrastructure.xml") @ContextConfiguration("classpath:reactive-infrastructure.xml")
@ -66,9 +69,11 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
ReactiveMongoRepositoryFactory factory; ReactiveMongoRepositoryFactory factory;
ClassLoader classLoader; ClassLoader classLoader;
BeanFactory beanFactory; BeanFactory beanFactory;
ReactivePersonRepostitory repository; ReactivePersonRepository repository;
ReactiveImmutablePersonRepository immutableRepository;
private ReactivePerson dave, oliver, carter, boyd, stefan, leroi, alicia; private ReactivePerson dave, oliver, carter, boyd, stefan, leroi, alicia;
private ImmutableReactivePerson keith, james, mariah;
@Override @Override
public void setBeanClassLoader(ClassLoader classLoader) { public void setBeanClassLoader(ClassLoader classLoader) {
@ -89,9 +94,11 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
factory.setBeanFactory(beanFactory); factory.setBeanFactory(beanFactory);
factory.setEvaluationContextProvider(ReactiveQueryMethodEvaluationContextProvider.DEFAULT); factory.setEvaluationContextProvider(ReactiveQueryMethodEvaluationContextProvider.DEFAULT);
repository = factory.getRepository(ReactivePersonRepostitory.class); repository = factory.getRepository(ReactivePersonRepository.class);
immutableRepository = factory.getRepository(ReactiveImmutablePersonRepository.class);
repository.deleteAll().as(StepVerifier::create).verifyComplete(); repository.deleteAll().as(StepVerifier::create).verifyComplete();
immutableRepository.deleteAll().as(StepVerifier::create).verifyComplete();
dave = new ReactivePerson("Dave", "Matthews", 42); dave = new ReactivePerson("Dave", "Matthews", 42);
oliver = new ReactivePerson("Oliver August", "Matthews", 4); oliver = new ReactivePerson("Oliver August", "Matthews", 4);
@ -100,6 +107,9 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
stefan = new ReactivePerson("Stefan", "Lessard", 34); stefan = new ReactivePerson("Stefan", "Lessard", 34);
leroi = new ReactivePerson("Leroi", "Moore", 41); leroi = new ReactivePerson("Leroi", "Moore", 41);
alicia = new ReactivePerson("Alicia", "Keys", 30); alicia = new ReactivePerson("Alicia", "Keys", 30);
keith = new ImmutableReactivePerson(null, "Keith", "Urban", 53);
james = new ImmutableReactivePerson(null, "James", "Arthur", 33);
mariah = new ImmutableReactivePerson(null, "Mariah", "Carey", 51);
repository.saveAll(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia)).as(StepVerifier::create) // repository.saveAll(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia)).as(StepVerifier::create) //
.expectNextCount(7) // .expectNextCount(7) //
@ -325,6 +335,22 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
assertThat(boyd.getId()).isNotNull(); assertThat(boyd.getId()).isNotNull();
} }
@Test // GH-3609
public void savePublisherOfImmutableEntitiesShouldInsertEntity() {
immutableRepository.deleteAll().as(StepVerifier::create).verifyComplete();
immutableRepository.saveAll(Flux.just(keith, james, mariah)).as(StepVerifier::create)
.consumeNextWith(e -> keith = e)
.consumeNextWith(e -> james = e)
.consumeNextWith(e -> mariah = e)
.verifyComplete();
assertThat(keith.getId()).isNotNull();
assertThat(james.getId()).isNotNull();
assertThat(mariah.getId()).isNotNull();
}
@Test // DATAMONGO-1444 @Test // DATAMONGO-1444
public void deleteAllShouldRemoveEntities() { public void deleteAllShouldRemoveEntities() {
@ -453,12 +479,16 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
repository.findOne(example).as(StepVerifier::create).verifyComplete(); repository.findOne(example).as(StepVerifier::create).verifyComplete();
} }
interface ReactivePersonRepostitory extends ReactiveMongoRepository<ReactivePerson, String> { interface ReactivePersonRepository extends ReactiveMongoRepository<ReactivePerson, String> {
Flux<ReactivePerson> findByLastname(String lastname); Flux<ReactivePerson> findByLastname(String lastname);
} }
interface ReactiveImmutablePersonRepository extends ReactiveMongoRepository<ImmutableReactivePerson, String> {
}
@Data @Data
@NoArgsConstructor @NoArgsConstructor
static class ReactivePerson { static class ReactivePerson {
@ -476,4 +506,23 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
this.age = age; this.age = age;
} }
} }
@With
@Value
static class ImmutableReactivePerson {
@Id String id;
String firstname;
String lastname;
int age;
public ImmutableReactivePerson(@Nullable String id, String firstname, String lastname, int age) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
}
}
} }

Loading…
Cancel
Save