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);
}
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);
}
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()));
}
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);
}
Aggregations