Search in sources :

Example 6 with ReactiveMongoTemplate

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

the class ReactiveQuerydslMongoPredicateExecutorTests method translatesExceptionsCorrectly.

// DATAMONGO-2182, DATAMONGO-2265
@Test
public void translatesExceptionsCorrectly() {
    ReactiveMongoOperations ops = new ReactiveMongoTemplate(dbFactory) {

        @Override
        protected Mono<MongoDatabase> doGetDatabase() {
            return Mono.error(new MongoException(18, "Authentication Failed"));
        }
    };
    ReactiveMongoRepositoryFactory factory = new ReactiveMongoRepositoryFactory(ops);
    MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
    repository = new ReactiveQuerydslMongoPredicateExecutor<>(entityInformation, ops);
    // 
    repository.findOne(person.firstname.contains("batman")).as(// 
    StepVerifier::create).expectError(// 
    PermissionDeniedDataAccessException.class).verify();
}
Also used : ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) MongoException(com.mongodb.MongoException) ReactiveMongoOperations(org.springframework.data.mongodb.core.ReactiveMongoOperations) PermissionDeniedDataAccessException(org.springframework.dao.PermissionDeniedDataAccessException) StepVerifier(reactor.test.StepVerifier) Person(org.springframework.data.mongodb.repository.Person) QPerson(org.springframework.data.mongodb.repository.QPerson) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Test(org.junit.Test)

Example 7 with ReactiveMongoTemplate

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

the class ReactiveMongoTransactionManagerUnitTests method readonlyShouldInitiateASessionStartAndCommitTransaction.

// DATAMONGO-2265
@Test
void readonlyShouldInitiateASessionStartAndCommitTransaction() {
    ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
    ReactiveMongoTemplate template = new ReactiveMongoTemplate(databaseFactory);
    when(session.commitTransaction()).thenReturn(Mono.empty());
    DefaultTransactionDefinition readonlyTxDefinition = new DefaultTransactionDefinition();
    readonlyTxDefinition.setReadOnly(true);
    TransactionalOperator operator = TransactionalOperator.create(txManager, readonlyTxDefinition);
    template.execute(db -> {
        db.drop();
        return Mono.empty();
    }).as(// 
    operator::transactional).as(// 
    StepVerifier::create).verifyComplete();
    verify(databaseFactory).withSession(eq(session));
    verify(session).startTransaction();
    verify(session).commitTransaction();
    verify(session).close();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) StepVerifier(reactor.test.StepVerifier) Mock(org.mockito.Mock) TransactionDefinition(org.springframework.transaction.TransactionDefinition) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) Mono(reactor.core.publisher.Mono) ClientSession(com.mongodb.reactivestreams.client.ClientSession) ServerSession(com.mongodb.session.ServerSession) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.jupiter.api.Test)

Example 8 with ReactiveMongoTemplate

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

the class ReactiveMongoTransactionManagerUnitTests method triggerCommitCorrectly.

// DATAMONGO-2265
@Test
void triggerCommitCorrectly() {
    ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
    ReactiveMongoTemplate template = new ReactiveMongoTemplate(databaseFactory);
    when(session.commitTransaction()).thenReturn(Mono.empty());
    TransactionalOperator operator = TransactionalOperator.create(txManager, new DefaultTransactionDefinition());
    template.execute(db -> {
        db.drop();
        return Mono.empty();
    }).as(// 
    operator::transactional).as(// 
    StepVerifier::create).verifyComplete();
    verify(databaseFactory).withSession(eq(session));
    verify(session).startTransaction();
    verify(session).commitTransaction();
    verify(session).close();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) StepVerifier(reactor.test.StepVerifier) Mock(org.mockito.Mock) TransactionDefinition(org.springframework.transaction.TransactionDefinition) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) Mono(reactor.core.publisher.Mono) ClientSession(com.mongodb.reactivestreams.client.ClientSession) ServerSession(com.mongodb.session.ServerSession) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.jupiter.api.Test)

Example 9 with ReactiveMongoTemplate

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

the class ReactiveMongoTransactionManagerUnitTests method participateInOnGoingTransactionWithRollbackOnly.

// DATAMONGO-2265
@Test
void participateInOnGoingTransactionWithRollbackOnly() {
    ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
    ReactiveMongoTemplate template = new ReactiveMongoTemplate(databaseFactory);
    when(session.abortTransaction()).thenReturn(Mono.empty());
    TransactionalOperator operator = TransactionalOperator.create(txManager, new DefaultTransactionDefinition());
    operator.execute(tx -> {
        return template.execute(db -> {
            db.drop();
            tx.setRollbackOnly();
            return Mono.empty();
        });
    }).as(StepVerifier::create).verifyComplete();
    verify(databaseFactory, times(1)).withSession(eq(session));
    verify(session).startTransaction();
    verify(session).abortTransaction();
    verify(session).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 10 with ReactiveMongoTemplate

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

the class ReactiveMongoTransactionManagerUnitTests method participateInOnGoingTransactionWithCommit.

// DATAMONGO-2265
@Test
void participateInOnGoingTransactionWithCommit() {
    ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
    ReactiveMongoTemplate template = new ReactiveMongoTemplate(databaseFactory);
    when(session.commitTransaction()).thenReturn(Mono.empty());
    TransactionalOperator operator = TransactionalOperator.create(txManager, new DefaultTransactionDefinition());
    template.execute(db -> {
        db.drop();
        return Mono.empty();
    }).as(StepVerifier::create).verifyComplete();
    template.execute(db -> {
        db.drop();
        return Mono.empty();
    }).as(// 
    operator::transactional).as(// 
    StepVerifier::create).verifyComplete();
    verify(databaseFactory, times(1)).withSession(eq(session));
    verify(session).startTransaction();
    verify(session).commitTransaction();
    verify(session).close();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) StepVerifier(reactor.test.StepVerifier) Mock(org.mockito.Mock) TransactionDefinition(org.springframework.transaction.TransactionDefinition) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) Mono(reactor.core.publisher.Mono) ClientSession(com.mongodb.reactivestreams.client.ClientSession) ServerSession(com.mongodb.session.ServerSession) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.jupiter.api.Test)

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