Search in sources :

Example 1 with ChangeStreamDocument

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

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

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;
}
Also used : BsonDocument(org.bson.BsonDocument) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonString(org.bson.BsonString)

Example 4 with ChangeStreamDocument

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();
        });
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonString(org.bson.BsonString)

Example 5 with ChangeStreamDocument

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);
}
Also used : ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) Document(org.bson.Document) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) Test(org.junit.jupiter.api.Test)

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