use of com.mongodb.client.model.changestream.ChangeStreamDocument 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();
}
use of com.mongodb.client.model.changestream.ChangeStreamDocument 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()));
}
use of com.mongodb.client.model.changestream.ChangeStreamDocument in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method close.
public OperationResult close(final BsonDocument operation) {
String id = operation.getString("object").getValue();
if (entities.hasCursor(id)) {
MongoCursor<BsonDocument> cursor = entities.getCursor(id);
cursor.close();
} else {
MongoCursor<ChangeStreamDocument<BsonDocument>> cursor = entities.getChangeStreamCursor(id);
cursor.close();
}
return OperationResult.NONE;
}
use of com.mongodb.client.model.changestream.ChangeStreamDocument in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeIterateUntilDocumentOrError.
public OperationResult executeIterateUntilDocumentOrError(final BsonDocument operation) {
String id = operation.getString("object").getValue();
if (entities.hasCursor(id)) {
MongoCursor<BsonDocument> cursor = entities.getCursor(id);
if (operation.containsKey("arguments")) {
throw new UnsupportedOperationException("Unexpected arguments");
}
return resultOf(cursor::next);
} else {
MongoCursor<ChangeStreamDocument<BsonDocument>> cursor = entities.getChangeStreamCursor(id);
if (operation.containsKey("arguments")) {
throw new UnsupportedOperationException("Unexpected arguments");
}
return resultOf(() -> {
BsonDocumentWriter bsonDocumentWriter = new BsonDocumentWriter(new BsonDocument());
changeStreamDocumentCodec.encode(bsonDocumentWriter, cursor.next(), EncoderContext.builder().build());
return bsonDocumentWriter.getDocument();
});
}
}
use of com.mongodb.client.model.changestream.ChangeStreamDocument in project mongo-java-driver by mongodb.
the class ChangeStreamsCancellationTest method testCancelReleasesSessions.
@Test
public void testCancelReleasesSessions() {
Mono.from(collection.insertOne(new Document())).block(TIMEOUT_DURATION);
TestSubscriber<ChangeStreamDocument<Document>> subscriber = new TestSubscriber<>();
subscriber.doOnSubscribe(sub -> {
sub.request(Integer.MAX_VALUE);
new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Sleep interrupted");
}
sub.cancel();
}).start();
});
collection.watch().subscribe(subscriber);
assertDoesNotThrow(Fixture::waitForLastServerSessionPoolRelease);
}
Aggregations