Search in sources :

Example 11 with ChangeStreamDocument

use of com.mongodb.client.model.changestream.ChangeStreamDocument in project mongo-java-driver by mongodb.

the class AbstractChangeStreamsTest method shouldPassAllOutcomes.

@Test
public void shouldPassAllOutcomes() throws InterruptedException {
    BsonDocument result = definition.getDocument("result");
    MongoCursor<ChangeStreamDocument<BsonDocument>> cursor = createCursor(result);
    commandListener.waitForFirstCommandCompletion();
    handleOperations();
    if (cursor != null) {
        try {
            checkStreamValues(result, cursor);
        } finally {
            cursor.close();
        }
    }
    checkExpectations();
}
Also used : BsonDocument(org.bson.BsonDocument) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) JsonTestServerVersionChecker.skipTest(com.mongodb.JsonTestServerVersionChecker.skipTest) Test(org.junit.Test)

Example 12 with ChangeStreamDocument

use of com.mongodb.client.model.changestream.ChangeStreamDocument in project mongo-java-driver by mongodb.

the class DocumentationSamples method testWatch.

@Test
public void testWatch() throws InterruptedException {
    assumeTrue(isDiscoverableReplicaSet() && serverVersionAtLeast(3, 6));
    final MongoCollection<Document> inventory = collection;
    final AtomicBoolean stop = new AtomicBoolean(false);
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (!stop.get()) {
                collection.insertMany(asList(new Document("username", "alice"), new Document()));
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                // ignore
                }
                collection.deleteOne(new Document("username", "alice"));
            }
        }
    });
    thread.start();
    // Start Changestream Example 1
    MongoCursor<ChangeStreamDocument<Document>> cursor = inventory.watch().iterator();
    ChangeStreamDocument<Document> next = cursor.next();
    // End Changestream Example 1
    cursor.close();
    // Start Changestream Example 2
    cursor = inventory.watch().fullDocument(FullDocument.UPDATE_LOOKUP).iterator();
    next = cursor.next();
    // End Changestream Example 2
    cursor.close();
    // Start Changestream Example 3
    BsonDocument resumeToken = next.getResumeToken();
    cursor = inventory.watch().resumeAfter(resumeToken).iterator();
    next = cursor.next();
    // End Changestream Example 3
    cursor.close();
    // Start Changestream Example 4
    List<Bson> pipeline = asList(match(Document.parse("{'fullDocument.username': 'alice'}")), addFields(new Field<String>("newField", "this is an added field!")));
    cursor = inventory.watch(pipeline).iterator();
    next = cursor.next();
    // End Changestream Example 4
    cursor.close();
    stop.set(true);
    thread.join();
}
Also used : ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) Document(org.bson.Document) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) FullDocument(com.mongodb.client.model.changestream.FullDocument) BsonDocument(org.bson.BsonDocument) Bson(org.bson.conversions.Bson) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Field(com.mongodb.client.model.Field) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 13 with ChangeStreamDocument

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

the class ChangeStreamTests method plainAggregationPipelineToFilterMessages.

// DATAMONGO-1803
@Test
void plainAggregationPipelineToFilterMessages() throws InterruptedException {
    CollectingMessageListener<ChangeStreamDocument<Document>, User> messageListener = new CollectingMessageListener<>();
    ChangeStreamRequest<User> request = // 
    ChangeStreamRequest.builder().collection(// 
    "user").publishTo(// 
    messageListener).filter(// 
    new Document("$match", new Document("fullDocument.user_name", "sugarSplashy"))).maxAwaitTime(// 
    Duration.ofMillis(10)).build();
    Subscription subscription = container.register(request, User.class);
    awaitSubscription(subscription);
    template.save(jellyBelly);
    template.save(sugarSplashy);
    template.save(huffyFluffy);
    awaitMessages(messageListener, 1);
    List<User> messageBodies = messageListener.getMessages().stream().map(Message::getBody).collect(Collectors.toList());
    assertThat(messageBodies).hasSize(1).containsExactly(sugarSplashy);
}
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) RepeatFailedTest(org.junitpioneer.jupiter.RepeatFailedTest) Test(org.junit.jupiter.api.Test)

Example 14 with ChangeStreamDocument

use of com.mongodb.client.model.changestream.ChangeStreamDocument 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)

Example 15 with ChangeStreamDocument

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

the class ChangeStreamTests method readsAndConvertsMessageBodyCorrectly.

// DATAMONGO-1803
@Test
void readsAndConvertsMessageBodyCorrectly() 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);
    awaitMessages(messageListener, 1);
    Message<ChangeStreamDocument<Document>, User> message1 = messageListener.getFirstMessage();
    assertThat(message1.getRaw()).isNotNull();
    assertThat(message1.getProperties()).isEqualTo(MessageProperties.builder().collectionName("user").databaseName("change-stream-tests").build());
    assertThat(message1.getBody()).isEqualTo(jellyBelly);
}
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

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