Search in sources :

Example 21 with ChangeStreamDocument

use of com.mongodb.client.model.changestream.ChangeStreamDocument in project spring-data-mongodb by spring-projects.

the class ChangeStreamTests method filterOnUpdateDescriptionElement.

// DATAMONGO-1996
@Test
void filterOnUpdateDescriptionElement() throws InterruptedException {
    template.save(jellyBelly);
    template.save(sugarSplashy);
    template.save(huffyFluffy);
    CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener = new CollectingMessageListener<>();
    ChangeStreamRequest<User> request = // 
    ChangeStreamRequest.builder(messageListener).collection(// 
    "user").filter(// 
    newAggregation(User.class, match(where("updateDescription.updatedFields.address").exists(true)))).maxAwaitTime(// 
    Duration.ofMillis(10)).fullDocumentLookup(FullDocument.UPDATE_LOOKUP).build();
    Subscription subscription = container.register(request, User.class);
    awaitSubscription(subscription);
    template.update(User.class).matching(query(where("id").is(jellyBelly.id))).apply(Update.update("address", new Address("candy ave"))).first();
    template.update(User.class).matching(query(where("id").is(sugarSplashy.id))).apply(new Update().inc("age", 1)).first();
    template.update(User.class).matching(query(where("id").is(huffyFluffy.id))).apply(Update.update("address", new Address("flower street"))).first();
    awaitMessages(messageListener);
    List<User> messageBodies = messageListener.getMessages().stream().map(Message::getBody).collect(Collectors.toList());
    assertThat(messageBodies).hasSize(2);
}
Also used : ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) Update(org.springframework.data.mongodb.core.query.Update) RepeatFailedTest(org.junitpioneer.jupiter.RepeatFailedTest) Test(org.junit.jupiter.api.Test)

Example 22 with ChangeStreamDocument

use of com.mongodb.client.model.changestream.ChangeStreamDocument in project spring-data-mongodb by spring-projects.

the class ChangeStreamTests method resumesCorrectly.

// DATAMONGO-1803
@Test
void resumesCorrectly() throws InterruptedException {
    CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener1 = new CollectingMessageListener<>();
    Subscription subscription1 = container.register(new ChangeStreamRequest<>(messageListener1, new ChangeStreamRequestOptions(null, "user", Duration.ofMillis(10), ChangeStreamOptions.builder().build())), User.class);
    awaitSubscription(subscription1);
    template.save(jellyBelly);
    template.save(sugarSplashy);
    template.save(huffyFluffy);
    awaitMessages(messageListener1, 3);
    BsonDocument resumeToken = messageListener1.getFirstMessage().getRaw().getResumeToken();
    CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener2 = new CollectingMessageListener<>();
    ChangeStreamRequest<User> subSequentRequest = ChangeStreamRequest.builder().collection("user").publishTo(messageListener2).resumeToken(resumeToken).maxAwaitTime(Duration.ofMillis(10)).build();
    Subscription subscription2 = container.register(subSequentRequest, User.class);
    awaitSubscription(subscription2);
    awaitMessages(messageListener2, 2);
    List<User> messageBodies = messageListener2.getMessages().stream().map(Message::getBody).collect(Collectors.toList());
    assertThat(messageBodies).hasSize(2).doesNotContain(jellyBelly);
}
Also used : BsonDocument(org.bson.BsonDocument) 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)

Example 23 with ChangeStreamDocument

use of com.mongodb.client.model.changestream.ChangeStreamDocument in project spring-data-mongodb by spring-projects.

the class ChangeStreamTests method readsPlainDocumentMessageCorrectly.

// DATAMONGO-1803
@Test
void readsPlainDocumentMessageCorrectly() 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);
    awaitMessages(messageListener, 1);
    Message<ChangeStreamDocument<Document>, Document> message1 = messageListener.getFirstMessage();
    assertThat(message1.getRaw()).isNotNull();
    assertThat(message1.getProperties()).isEqualTo(MessageProperties.builder().collectionName("user").databaseName("change-stream-tests").build());
    assertThat(message1.getBody()).isEqualTo(new Document("_id", "id-1").append("user_name", "jellyBelly").append("age", 7).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) RepeatFailedTest(org.junitpioneer.jupiter.RepeatFailedTest) Test(org.junit.jupiter.api.Test)

Example 24 with ChangeStreamDocument

use of com.mongodb.client.model.changestream.ChangeStreamDocument in project spring-data-mongodb by spring-projects.

the class ChangeStreamTests method resumeAtTimestampCorrectly.

// DATAMONGO-2012, DATAMONGO-2113
@Test
@MongoVersion(asOf = "4.0")
void resumeAtTimestampCorrectly() throws InterruptedException {
    CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener1 = new CollectingMessageListener<>();
    Subscription subscription1 = container.register(new ChangeStreamRequest<>(messageListener1, new ChangeStreamRequestOptions(null, "user", Duration.ofMillis(10), ChangeStreamOptions.builder().build())), User.class);
    awaitSubscription(subscription1);
    template.save(jellyBelly);
    // cluster timestamp is in seconds, so we need to wait at least one.
    Thread.sleep(1000);
    template.save(sugarSplashy);
    awaitMessages(messageListener1, 12);
    Instant resumeAt = ((ChangeStreamEventMessage) messageListener1.getLastMessage()).getTimestamp();
    template.save(huffyFluffy);
    awaitMessages(messageListener1, 3);
    CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener2 = new CollectingMessageListener<>();
    ChangeStreamRequest<User> subSequentRequest = // 
    ChangeStreamRequest.builder().collection(// 
    "user").resumeAt(// 
    resumeAt).publishTo(// 
    messageListener2).maxAwaitTime(// 
    Duration.ofMillis(10)).build();
    Subscription subscription2 = container.register(subSequentRequest, User.class);
    awaitSubscription(subscription2);
    awaitMessages(messageListener2, 2);
    List<User> messageBodies = messageListener2.getMessages().stream().map(Message::getBody).collect(Collectors.toList());
    assertThat(messageBodies).hasSize(2).doesNotContain(jellyBelly);
}
Also used : Instant(java.time.Instant) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) ChangeStreamRequestOptions(org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions) ChangeStreamEventMessage(org.springframework.data.mongodb.core.messaging.ChangeStreamTask.ChangeStreamEventMessage) RepeatFailedTest(org.junitpioneer.jupiter.RepeatFailedTest) Test(org.junit.jupiter.api.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Aggregations

ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)24 BsonDocument (org.bson.BsonDocument)16 Test (org.junit.Test)11 Test (org.junit.jupiter.api.Test)9 ChangeStreamRequestOptions (org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions)9 Document (org.bson.Document)8 RepeatFailedTest (org.junitpioneer.jupiter.RepeatFailedTest)8 FullDocument (com.mongodb.client.model.changestream.FullDocument)7 MongoDatabase (com.mongodb.client.MongoDatabase)2 BsonString (org.bson.BsonString)2 Bson (org.bson.conversions.Bson)2 Criteria (org.springframework.data.mongodb.core.query.Criteria)2 JsonTestServerVersionChecker.skipTest (com.mongodb.JsonTestServerVersionChecker.skipTest)1 MongoClient (com.mongodb.client.MongoClient)1 Collation (com.mongodb.client.model.Collation)1 Field (com.mongodb.client.model.Field)1 Instant (java.time.Instant)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 BsonDocumentWriter (org.bson.BsonDocumentWriter)1 BsonTimestamp (org.bson.BsonTimestamp)1