Search in sources :

Example 1 with Template

use of org.springframework.data.mongodb.test.util.Template in project spring-data-mongodb by spring-projects.

the class DefaultReactiveIndexOperationsTests method shouldFavorExplicitMappingHintViaClass.

// DATAMONGO-1682, DATAMONGO-2198
@Test
public void shouldFavorExplicitMappingHintViaClass() {
    IndexDefinition id = new Index().named("partial-with-inheritance").on("k3y", Direction.ASC).partial(of(where("age").gte(10)));
    indexOps = new DefaultReactiveIndexOperations(template, this.template.getCollectionName(DefaultIndexOperationsIntegrationTestsSample.class), new QueryMapper(template.getConverter()), MappingToSameCollection.class);
    indexOps.ensureIndex(id).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    // 
    indexOps.getIndexInfo().filter(this.indexByName("partial-with-inheritance")).as(StepVerifier::create).consumeNextWith(indexInfo -> {
        assertThat(Document.parse(indexInfo.getPartialFilterExpression())).isEqualTo(Document.parse("{ \"a_g_e\" : { \"$gte\" : 10 } }"));
    }).verifyComplete();
}
Also used : Document(org.bson.Document) BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Predicate(java.util.function.Predicate) ReactiveMongoTestTemplate(org.springframework.data.mongodb.test.util.ReactiveMongoTestTemplate) Collation(org.springframework.data.mongodb.core.query.Collation) Template(org.springframework.data.mongodb.test.util.Template) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Index(org.springframework.data.mongodb.core.index.Index) Criteria(org.springframework.data.mongodb.core.query.Criteria) Field(org.springframework.data.mongodb.core.mapping.Field) Test(org.junit.jupiter.api.Test) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PartialIndexFilter(org.springframework.data.mongodb.core.index.PartialIndexFilter) Assertions(org.assertj.core.api.Assertions) Direction(org.springframework.data.domain.Sort.Direction) CaseFirst(org.springframework.data.mongodb.core.query.Collation.CaseFirst) MongoTemplateExtension(org.springframework.data.mongodb.test.util.MongoTemplateExtension) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 2 with Template

use of org.springframework.data.mongodb.test.util.Template in project spring-data-mongodb by spring-projects.

the class DefaultMessageListenerContainerTests method shouldNotifyErrorHandlerOnErrorInListener.

// DATAMONGO-2322
@Test
@EnableIfReplicaSetAvailable
public void shouldNotifyErrorHandlerOnErrorInListener() throws InterruptedException {
    ErrorHandler errorHandler = mock(ErrorHandler.class);
    MessageListenerContainer container = new DefaultMessageListenerContainer(template);
    AtomicBoolean thrownException = new AtomicBoolean();
    Subscription subscription = container.register(new ChangeStreamRequest(message -> {
        try {
            if (thrownException.compareAndSet(false, true)) {
                throw new IllegalStateException("Boom!");
            }
        } finally {
            messageListener.onMessage(message);
        }
    }, options()), Person.class, errorHandler);
    container.start();
    awaitSubscription(subscription, TIMEOUT);
    collection.insertOne(new Document("_id", "id-1").append("firstname", "foo"));
    collection.insertOne(new Document("_id", "id-2").append("firstname", "bar"));
    awaitMessages(messageListener, 2, TIMEOUT);
    verify(errorHandler, atLeast(1)).handleError(any(IllegalStateException.class));
    assertThat(messageListener.getTotalNumberMessagesReceived()).isEqualTo(2);
}
Also used : Document(org.bson.Document) BeforeEach(org.junit.jupiter.api.BeforeEach) DataAccessException(org.springframework.dao.DataAccessException) ArgumentMatchers(org.mockito.ArgumentMatchers) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) ChangeStreamOptions(org.springframework.data.mongodb.core.ChangeStreamOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) ErrorHandler(org.springframework.util.ErrorHandler) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Duration(java.time.Duration) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) Assertions(org.assertj.core.api.Assertions) RequestOptions(org.springframework.data.mongodb.core.messaging.SubscriptionRequest.RequestOptions) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) MongoTestUtils(org.springframework.data.mongodb.test.util.MongoTestUtils) Template(org.springframework.data.mongodb.test.util.Template) Collectors(java.util.stream.Collectors) Client(org.springframework.data.mongodb.test.util.Client) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ChangeStreamRequestOptions(org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions) Data(lombok.Data) MongoTemplateExtension(org.springframework.data.mongodb.test.util.MongoTemplateExtension) Id(org.springframework.data.annotation.Id) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) SubscriptionUtils(org.springframework.data.mongodb.core.messaging.SubscriptionUtils) MongoServerCondition(org.springframework.data.mongodb.test.util.MongoServerCondition) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) ErrorHandler(org.springframework.util.ErrorHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Document(org.bson.Document) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) Test(org.junit.jupiter.api.Test)

Aggregations

Assertions (org.assertj.core.api.Assertions)2 Document (org.bson.Document)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)2 MongoTemplateExtension (org.springframework.data.mongodb.test.util.MongoTemplateExtension)2 Template (org.springframework.data.mongodb.test.util.Template)2 MongoClient (com.mongodb.client.MongoClient)1 MongoCollection (com.mongodb.client.MongoCollection)1 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)1 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)1 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)1 Duration (java.time.Duration)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Data (lombok.Data)1 ArgumentMatchers (org.mockito.ArgumentMatchers)1 Mockito (org.mockito.Mockito)1 SimpleAsyncTaskExecutor (org.springframework.core.task.SimpleAsyncTaskExecutor)1