Search in sources :

Example 1 with ReactiveMongoTemplate

use of org.springframework.data.mongodb.core.ReactiveMongoTemplate in project JavaForFun by gumartinm.

the class MongoConfiguration method reactiveMongoTemplate.

@Bean
@Override
public ReactiveMongoTemplate reactiveMongoTemplate() throws Exception {
    ReactiveMongoTemplate reactiveMongoTemplate = new ReactiveMongoTemplate(reactiveMongoDbFactory(), super.mappingMongoConverter());
    reactiveMongoTemplate.setWriteConcern(WriteConcern.ACKNOWLEDGED);
    return reactiveMongoTemplate;
}
Also used : ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) Bean(org.springframework.context.annotation.Bean)

Example 2 with ReactiveMongoTemplate

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

the class MongoReactiveHealthIndicatorTests method testMongoIsUp.

@Test
void testMongoIsUp() {
    Document buildInfo = mock(Document.class);
    given(buildInfo.getString("version")).willReturn("2.6.4");
    ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class);
    given(reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }")).willReturn(Mono.just(buildInfo));
    MongoReactiveHealthIndicator mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator(reactiveMongoTemplate);
    Mono<Health> health = mongoReactiveHealthIndicator.health();
    StepVerifier.create(health).consumeNextWith((h) -> {
        assertThat(h.getStatus()).isEqualTo(Status.UP);
        assertThat(h.getDetails()).containsOnlyKeys("version");
        assertThat(h.getDetails().get("version")).isEqualTo("2.6.4");
    }).verifyComplete();
}
Also used : Document(org.bson.Document) Test(org.junit.jupiter.api.Test) Status(org.springframework.boot.actuate.health.Status) StepVerifier(reactor.test.StepVerifier) BDDMockito.given(org.mockito.BDDMockito.given) MongoException(com.mongodb.MongoException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) Mono(reactor.core.publisher.Mono) Health(org.springframework.boot.actuate.health.Health) Mockito.mock(org.mockito.Mockito.mock) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) Health(org.springframework.boot.actuate.health.Health) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 3 with ReactiveMongoTemplate

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

the class MongoReactiveHealthIndicatorTests method testMongoIsDown.

@Test
void testMongoIsDown() {
    ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class);
    given(reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }")).willThrow(new MongoException("Connection failed"));
    MongoReactiveHealthIndicator mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator(reactiveMongoTemplate);
    Mono<Health> health = mongoReactiveHealthIndicator.health();
    StepVerifier.create(health).consumeNextWith((h) -> {
        assertThat(h.getStatus()).isEqualTo(Status.DOWN);
        assertThat(h.getDetails()).containsOnlyKeys("error");
        assertThat(h.getDetails().get("error")).isEqualTo(MongoException.class.getName() + ": Connection failed");
    }).verifyComplete();
}
Also used : Document(org.bson.Document) Test(org.junit.jupiter.api.Test) Status(org.springframework.boot.actuate.health.Status) StepVerifier(reactor.test.StepVerifier) BDDMockito.given(org.mockito.BDDMockito.given) MongoException(com.mongodb.MongoException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) Mono(reactor.core.publisher.Mono) Health(org.springframework.boot.actuate.health.Health) Mockito.mock(org.mockito.Mockito.mock) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) MongoException(com.mongodb.MongoException) Health(org.springframework.boot.actuate.health.Health) Test(org.junit.jupiter.api.Test)

Example 4 with ReactiveMongoTemplate

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

the class ReactiveMongoTransactionManagerUnitTests method suspendTransactionWhilePropagationNotSupported.

// DATAMONGO-2265
@Test
void suspendTransactionWhilePropagationNotSupported() {
    ReactiveMongoTransactionManager txManager = new ReactiveMongoTransactionManager(databaseFactory);
    ReactiveMongoTemplate template = new ReactiveMongoTemplate(databaseFactory);
    when(session.commitTransaction()).thenReturn(Mono.empty());
    TransactionalOperator outer = TransactionalOperator.create(txManager, new DefaultTransactionDefinition());
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
    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, never()).startTransaction();
    verify(databaseFactory, times(1)).withSession(eq(session));
    verify(databaseFactory, never()).withSession(eq(session2));
    verify(db, times(2)).drop();
    verify(session2, never()).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 5 with ReactiveMongoTemplate

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

the class ReactiveAggregationUnitTests method setUp.

@BeforeEach
void setUp() {
    factory = new SimpleReactiveMongoDatabaseFactory(mongoClient, "db");
    template = new ReactiveMongoTemplate(factory);
    when(mongoClient.getDatabase("db")).thenReturn(db);
    when(db.getCollection(eq(INPUT_COLLECTION), any(Class.class))).thenReturn(collection);
    when(collection.aggregate(anyList(), any(Class.class))).thenReturn(publisher);
    when(publisher.allowDiskUse(any())).thenReturn(publisher);
    when(publisher.collation(any())).thenReturn(publisher);
}
Also used : SimpleReactiveMongoDatabaseFactory(org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) 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