Search in sources :

Example 1 with ChangeStreamRequestOptions

use of org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions in project spring-data-mongodb by spring-projects.

the class ChangeStreamTests method readsOnlyDiffForUpdateWhenOptionsDeclareDefaultExplicitly.

// DATAMONGO-1803
@Test
public void readsOnlyDiffForUpdateWhenOptionsDeclareDefaultExplicitly() throws InterruptedException {
    CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener = new CollectingMessageListener<>();
    ChangeStreamRequest<User> request = new ChangeStreamRequest<>(messageListener, new ChangeStreamRequestOptions("user", ChangeStreamOptions.builder().fullDocumentLookup(FullDocument.DEFAULT).build()));
    Subscription subscription = container.register(request, User.class);
    awaitSubscription(subscription);
    template.save(jellyBelly);
    template.update(User.class).matching(query(where("id").is(jellyBelly.id))).apply(Update.update("age", 8)).first();
    awaitMessages(messageListener, 2);
    assertThat(messageListener.getFirstMessage().getBody()).isEqualTo(jellyBelly);
    assertThat(messageListener.getLastMessage().getBody()).isNull();
}
Also used : ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) ChangeStreamRequestOptions(org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions) Test(org.junit.Test)

Example 2 with ChangeStreamRequestOptions

use of org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions in project spring-data-mongodb by spring-projects.

the class ChangeStreamTests method readsFullDocumentForUpdateWhenNotMappedToDomainTypeButLookupSpecified.

// DATAMONGO-1803
@Test
public void readsFullDocumentForUpdateWhenNotMappedToDomainTypeButLookupSpecified() throws InterruptedException {
    CollectingMessageListener<ChangeStreamDocument<Document>, Document> messageListener = new CollectingMessageListener<>();
    ChangeStreamRequest<Document> request = new ChangeStreamRequest<>(messageListener, new ChangeStreamRequestOptions("user", ChangeStreamOptions.builder().returnFullDocumentOnUpdate().build()));
    Subscription subscription = container.register(request, Document.class);
    awaitSubscription(subscription);
    template.save(jellyBelly);
    template.update(User.class).matching(query(where("id").is(jellyBelly.id))).apply(Update.update("age", 8)).first();
    awaitMessages(messageListener, 2);
    assertThat(messageListener.getFirstMessage().getBody()).isEqualTo(new Document("_id", "id-1").append("user_name", "jellyBelly").append("age", 7).append("_class", User.class.getName()));
    assertThat(messageListener.getLastMessage().getBody()).isEqualTo(new Document("_id", "id-1").append("user_name", "jellyBelly").append("age", 8).append("_class", User.class.getName()));
}
Also used : ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) Document(org.bson.Document) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonDocument(org.bson.BsonDocument) FullDocument(com.mongodb.client.model.changestream.FullDocument) ChangeStreamRequestOptions(org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions) Test(org.junit.Test)

Example 3 with ChangeStreamRequestOptions

use of org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions in project spring-data-mongodb by spring-projects.

the class TaskFactoryUnitTests method createsChangeStreamRequestCorrectly.

// DATAMONGO-1803
@Test
void createsChangeStreamRequestCorrectly() {
    when(template.getConverter()).thenReturn(converter);
    ChangeStreamRequestOptions options = Mockito.mock(ChangeStreamRequestOptions.class);
    Task task = factory.forRequest(new ChangeStreamRequest(messageListener, options), Object.class, errorHandler);
    assertThat(task).isInstanceOf(ChangeStreamTask.class);
}
Also used : ChangeStreamRequestOptions(org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions) Test(org.junit.jupiter.api.Test)

Example 4 with ChangeStreamRequestOptions

use of org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions in project spring-data-mongodb by spring-projects.

the class ChangeStreamTests method readsOnlyDiffForUpdateWhenNotMappedToDomainType.

// DATAMONGO-1803
@Test
void readsOnlyDiffForUpdateWhenNotMappedToDomainType() throws InterruptedException {
    CollectingMessageListener<ChangeStreamDocument<Document>, Document> messageListener = new CollectingMessageListener<>();
    ChangeStreamRequest<Document> request = new ChangeStreamRequest<>(messageListener, new ChangeStreamRequestOptions(null, "user", Duration.ofMillis(10), ChangeStreamOptions.builder().build()));
    Subscription subscription = container.register(request, Document.class);
    awaitSubscription(subscription);
    template.save(jellyBelly);
    template.update(User.class).matching(query(where("id").is(jellyBelly.id))).apply(Update.update("age", 8)).first();
    awaitMessages(messageListener, 2);
    assertThat(messageListener.getFirstMessage().getBody()).isEqualTo(new Document("_id", "id-1").append("user_name", "jellyBelly").append("age", 7).append("_class", User.class.getName()));
    assertThat(messageListener.getLastMessage().getBody()).isNull();
}
Also used : ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) Document(org.bson.Document) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonDocument(org.bson.BsonDocument) FullDocument(com.mongodb.client.model.changestream.FullDocument) ChangeStreamRequestOptions(org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions) RepeatFailedTest(org.junitpioneer.jupiter.RepeatFailedTest) Test(org.junit.jupiter.api.Test)

Example 5 with ChangeStreamRequestOptions

use of org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions in project spring-data-mongodb by spring-projects.

the class ChangeStreamTests method readsAndConvertsUpdateMessageBodyCorrectly.

// DATAMONGO-1803
@Test
void readsAndConvertsUpdateMessageBodyCorrectly() throws InterruptedException {
    CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener = new CollectingMessageListener<>();
    ChangeStreamRequest<User> request = new ChangeStreamRequest<>(messageListener, new ChangeStreamRequestOptions(null, "user", Duration.ofMillis(10), ChangeStreamOptions.builder().build()));
    Subscription subscription = container.register(request, User.class);
    awaitSubscription(subscription);
    template.save(jellyBelly);
    template.update(User.class).matching(query(where("id").is(jellyBelly.id))).apply(Update.update("age", 8)).first();
    awaitMessages(messageListener, 2);
    assertThat(messageListener.getFirstMessage().getBody()).isEqualTo(jellyBelly);
    assertThat(messageListener.getLastMessage().getBody()).isNotNull().hasFieldOrPropertyWithValue("age", 8);
}
Also used : ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) ChangeStreamRequestOptions(org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions) RepeatFailedTest(org.junitpioneer.jupiter.RepeatFailedTest) Test(org.junit.jupiter.api.Test)

Aggregations

ChangeStreamRequestOptions (org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions)10 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)9 Test (org.junit.jupiter.api.Test)7 RepeatFailedTest (org.junitpioneer.jupiter.RepeatFailedTest)6 BsonDocument (org.bson.BsonDocument)5 FullDocument (com.mongodb.client.model.changestream.FullDocument)4 Document (org.bson.Document)4 Test (org.junit.Test)2 MongoDatabase (com.mongodb.client.MongoDatabase)1 Collation (com.mongodb.client.model.Collation)1 Instant (java.time.Instant)1 BsonTimestamp (org.bson.BsonTimestamp)1 ChangeStreamOptions (org.springframework.data.mongodb.core.ChangeStreamOptions)1 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)1 TypedAggregation (org.springframework.data.mongodb.core.aggregation.TypedAggregation)1 ChangeStreamEventMessage (org.springframework.data.mongodb.core.messaging.ChangeStreamTask.ChangeStreamEventMessage)1 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)1