Search in sources :

Example 11 with ReactiveMongoTemplate

use of org.springframework.data.mongodb.core.ReactiveMongoTemplate in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTransactionManagerUnitTests method suspendTransactionWhilePropagationRequiresNew.

// DATAMONGO-2265
@Test
void suspendTransactionWhilePropagationRequiresNew() {
    when(databaseFactory.withSession(session2)).thenReturn(databaseFactory2);
    when(databaseFactory2.getMongoDatabase()).thenReturn(Mono.just(db2));
    when(session2.getServerSession()).thenReturn(serverSession);
    ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
    ReactiveMongoTemplate template = new ReactiveMongoTemplate(databaseFactory);
    when(session.commitTransaction()).thenReturn(Mono.empty());
    when(session2.commitTransaction()).thenReturn(Mono.empty());
    TransactionalOperator outer = TransactionalOperator.create(txManager, new DefaultTransactionDefinition());
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    TransactionalOperator inner = TransactionalOperator.create(txManager, definition);
    outer.execute(tx1 -> {
        return template.execute(db -> {
            db.drop();
            return inner.execute(tx2 -> {
                return template.execute(db2 -> {
                    db2.drop();
                    return Mono.empty();
                });
            });
        });
    }).as(StepVerifier::create).verifyComplete();
    verify(session).startTransaction();
    verify(session2).startTransaction();
    verify(databaseFactory, times(1)).withSession(eq(session));
    verify(databaseFactory).withSession(eq(session2));
    verify(db).drop();
    verify(db2).drop();
    verify(session).close();
    verify(session2).close();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.jupiter.api.Test)

Example 12 with ReactiveMongoTemplate

use of org.springframework.data.mongodb.core.ReactiveMongoTemplate in project spring-data-mongodb by spring-projects.

the class ReactivePerformanceTests method setUp.

@BeforeEach
public void setUp() throws Exception {
    mongo = MongoClients.create();
    SimpleReactiveMongoDatabaseFactory mongoDbFactory = new SimpleReactiveMongoDatabaseFactory(this.mongo, DATABASE_NAME);
    MongoMappingContext context = new MongoMappingContext();
    context.setInitialEntitySet(Collections.singleton(Person.class));
    context.afterPropertiesSet();
    converter = new MappingMongoConverter(new DbRefResolver() {

        @Nullable
        @Override
        public Object resolveReference(MongoPersistentProperty property, Object source, ReferenceLookupDelegate referenceLookupDelegate, MongoEntityReader entityReader) {
            return null;
        }

        @Override
        public Object resolveDbRef(MongoPersistentProperty property, DBRef dbref, DbRefResolverCallback callback, DbRefProxyHandler proxyHandler) {
            return null;
        }

        @Override
        public DBRef createDbRef(org.springframework.data.mongodb.core.mapping.DBRef annotation, MongoPersistentEntity<?> entity, Object id) {
            return null;
        }

        @Override
        public Document fetch(DBRef dbRef) {
            return null;
        }

        @Override
        public List<Document> bulkFetch(List<DBRef> dbRefs) {
            return null;
        }
    }, context);
    operations = new ReactiveMongoTemplate(mongoDbFactory, converter);
    ReactiveMongoRepositoryFactory factory = new ReactiveMongoRepositoryFactory(operations);
    repository = factory.getRepository(ReactivePersonRepository.class);
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) ReferenceLookupDelegate(org.springframework.data.mongodb.core.convert.ReferenceLookupDelegate) DbRefResolverCallback(org.springframework.data.mongodb.core.convert.DbRefResolverCallback) DBRef(com.mongodb.DBRef) DbRefResolver(org.springframework.data.mongodb.core.convert.DbRefResolver) Document(org.bson.Document) SimpleReactiveMongoDatabaseFactory(org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) ReactiveMongoRepositoryFactory(org.springframework.data.mongodb.repository.support.ReactiveMongoRepositoryFactory) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) DbRefProxyHandler(org.springframework.data.mongodb.core.convert.DbRefProxyHandler) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 13 with ReactiveMongoTemplate

use of org.springframework.data.mongodb.core.ReactiveMongoTemplate in project spring-data-mongodb by spring-projects.

the class ReactiveMongoPersistentEntityIndexCreatorUnitTests method setUp.

@BeforeEach
@SuppressWarnings("unchecked")
void setUp() {
    when(factory.getExceptionTranslator()).thenReturn(new MongoExceptionTranslator());
    when(factory.getMongoDatabase()).thenReturn(Mono.just(db));
    when(db.getCollection(any(), any(Class.class))).thenReturn(collection);
    indexOperations = new ReactiveMongoTemplate(factory).indexOps("foo");
    keysCaptor = ArgumentCaptor.forClass(org.bson.Document.class);
    optionsCaptor = ArgumentCaptor.forClass(IndexOptions.class);
    collectionCaptor = ArgumentCaptor.forClass(String.class);
    when(collection.createIndex(keysCaptor.capture(), optionsCaptor.capture())).thenReturn(Mono.just("OK"));
}
Also used : ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) IndexOptions(com.mongodb.client.model.IndexOptions) Document(org.springframework.data.mongodb.core.mapping.Document) MongoExceptionTranslator(org.springframework.data.mongodb.core.MongoExceptionTranslator) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ReactiveMongoTemplate (org.springframework.data.mongodb.core.ReactiveMongoTemplate)13 Test (org.junit.jupiter.api.Test)8 BeforeEach (org.junit.jupiter.api.BeforeEach)6 TransactionalOperator (org.springframework.transaction.reactive.TransactionalOperator)6 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)6 StepVerifier (reactor.test.StepVerifier)6 Mono (reactor.core.publisher.Mono)5 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)4 MongoException (com.mongodb.MongoException)3 ClientSession (com.mongodb.reactivestreams.client.ClientSession)3 ServerSession (com.mongodb.session.ServerSession)3 Document (org.bson.Document)3 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)3 Mock (org.mockito.Mock)3 Mockito (org.mockito.Mockito)3 MockitoExtension (org.mockito.junit.jupiter.MockitoExtension)3 TransactionDefinition (org.springframework.transaction.TransactionDefinition)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 BDDMockito.given (org.mockito.BDDMockito.given)2 Mockito.mock (org.mockito.Mockito.mock)2