Search in sources :

Example 6 with EnableIfMongoServerVersion

use of org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateTests method watchesDatabaseCorrectly.

// DATAMONGO-2012
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
@EnableIfReplicaSetAvailable
void watchesDatabaseCorrectly() throws InterruptedException {
    template.dropCollection(Person.class).onErrorResume(it -> Mono.empty()).as(StepVerifier::create).verifyComplete();
    template.dropCollection("personX").onErrorResume(it -> Mono.empty()).as(StepVerifier::create).verifyComplete();
    template.createCollection(Person.class).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    template.createCollection("personX").as(StepVerifier::create).expectNextCount(1).verifyComplete();
    BlockingQueue<ChangeStreamEvent<Person>> documents = new LinkedBlockingQueue<>(100);
    Disposable disposable = template.changeStream(ChangeStreamOptions.empty(), Person.class).doOnNext(documents::add).subscribe();
    // just give it some time to link to the collection.
    Thread.sleep(500);
    Person person1 = new Person("Spring", 38);
    Person person2 = new Person("Data", 37);
    Person person3 = new Person("MongoDB", 39);
    // 
    template.save(person1).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    // 
    template.save(person2).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    // 
    template.save(person3, "personX").as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    // just give it some time to link receive all events
    Thread.sleep(500);
    try {
        assertThat(documents.stream().map(ChangeStreamEvent::getBody).collect(Collectors.toList())).containsExactly(person1, person2, person3);
    } finally {
        disposable.dispose();
    }
}
Also used : Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) VersionedPerson(org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) Test(org.junit.jupiter.api.Test)

Example 7 with EnableIfMongoServerVersion

use of org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateTests method resumesAtTimestampCorrectly.

// DATAMONGO-2012, DATAMONGO-2113
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
@EnableIfReplicaSetAvailable
void resumesAtTimestampCorrectly() throws InterruptedException {
    template.dropCollection(Person.class).onErrorResume(it -> Mono.empty()).as(StepVerifier::create).verifyComplete();
    template.createCollection(Person.class).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    BlockingQueue<ChangeStreamEvent<Person>> documents = new LinkedBlockingQueue<>(100);
    Disposable disposable = template.changeStream("person", ChangeStreamOptions.empty(), Person.class).doOnNext(documents::add).subscribe();
    // just give it some time to link to the collection.
    Thread.sleep(500);
    Person person1 = new Person("Spring", 38);
    Person person2 = new Person("Data", 37);
    Person person3 = new Person("MongoDB", 39);
    // 
    template.save(person1).delayElement(Duration.ofSeconds(1)).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    // 
    template.save(person2).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    // just give it some time to link receive all events
    Thread.sleep(500);
    disposable.dispose();
    // skip first
    documents.take();
    // take 2nd
    Instant resumeAt = documents.take().getTimestamp();
    template.save(person3).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    BlockingQueue<ChangeStreamEvent<Person>> resumeDocuments = new LinkedBlockingQueue<>(100);
    template.changeStream("person", ChangeStreamOptions.builder().resumeAt(resumeAt).build(), Person.class).doOnNext(resumeDocuments::add).subscribe();
    // just give it some time to link receive all events
    Thread.sleep(500);
    try {
        assertThat(resumeDocuments.stream().map(ChangeStreamEvent::getBody).collect(Collectors.toList())).containsExactly(person2, person3);
    } finally {
        disposable.dispose();
    }
}
Also used : Disposable(reactor.core.Disposable) Instant(java.time.Instant) StepVerifier(reactor.test.StepVerifier) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) VersionedPerson(org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) Test(org.junit.jupiter.api.Test)

Example 8 with EnableIfMongoServerVersion

use of org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion in project spring-data-mongodb by spring-projects.

the class SimpleMongoRepositoryVersionedEntityTests method deleteNonExistingInTx.

// DATAMONGO-2195
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
public void deleteNonExistingInTx() {
    assumeThat(ReplicaSet.required().runsAsReplicaSet()).isTrue();
    initTxTemplate().execute(status -> {
        assertThatThrownBy(() -> repository.delete(new VersionedPerson("T-800"))).isInstanceOf(OptimisticLockingFailureException.class);
        return Void.TYPE;
    });
}
Also used : VersionedPerson(org.springframework.data.mongodb.repository.VersionedPerson) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Example 9 with EnableIfMongoServerVersion

use of org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion in project spring-data-mongodb by spring-projects.

the class ReactiveChangeStreamOperationSupportTests method changeStreamEventsShouldBeConvertedCorrectly.

// DATAMONGO-1803
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
public void changeStreamEventsShouldBeConvertedCorrectly() throws InterruptedException {
    BlockingQueue<ChangeStreamEvent<Person>> documents = new LinkedBlockingQueue<>(100);
    Disposable disposable = // 
    template.changeStream(Person.class).listen().doOnNext(documents::add).subscribe();
    // just give it some time to link to the collection.
    Thread.sleep(500);
    Person person1 = new Person("Spring", 38);
    Person person2 = new Person("Data", 39);
    Person person3 = new Person("MongoDB", 37);
    Flux.merge(template.insert(person1).delayElement(Duration.ofMillis(2)), template.insert(person2).delayElement(Duration.ofMillis(2)), // 
    template.insert(person3).delayElement(Duration.ofMillis(2))).as(// 
    StepVerifier::create).expectNextCount(// 
    3).verifyComplete();
    // just give it some time to link receive all events
    Thread.sleep(500);
    try {
        assertThat(documents.stream().map(ChangeStreamEvent::getBody).collect(Collectors.toList())).containsOnly(person1, person2, person3);
    } finally {
        disposable.dispose();
    }
}
Also used : Disposable(reactor.core.Disposable) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) StepVerifier(reactor.test.StepVerifier) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Example 10 with EnableIfMongoServerVersion

use of org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateTests method resumesAtBsonTimestampCorrectly.

// DATAMONGO-2115
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
@EnableIfReplicaSetAvailable
void resumesAtBsonTimestampCorrectly() throws InterruptedException {
    template.createCollection(Person.class).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    BlockingQueue<ChangeStreamEvent<Person>> documents = new LinkedBlockingQueue<>(100);
    Disposable disposable = template.changeStream("person", ChangeStreamOptions.empty(), Person.class).doOnNext(documents::add).subscribe();
    // just give it some time to link to the collection.
    Thread.sleep(500);
    Person person1 = new Person("Spring", 38);
    Person person2 = new Person("Data", 37);
    Person person3 = new Person("MongoDB", 39);
    // 
    template.save(person1).delayElement(Duration.ofSeconds(1)).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    // 
    template.save(person2).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    // skip first
    documents.take();
    // take 2nd
    BsonTimestamp resumeAt = documents.take().getBsonTimestamp();
    disposable.dispose();
    template.save(person3).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    template.changeStream("person", ChangeStreamOptions.builder().resumeAt(resumeAt).build(), Person.class).map(// 
    ChangeStreamEvent::getBody).buffer(// 
    2).as(// 
    StepVerifier::create).consumeNextWith(actual -> {
        assertThat(actual).containsExactly(person2, person3);
    }).thenCancel().verify();
}
Also used : Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) VersionedPerson(org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson) BsonTimestamp(org.bson.BsonTimestamp) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)17 EnableIfMongoServerVersion (org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion)17 AggregationUpdate (org.springframework.data.mongodb.core.aggregation.AggregationUpdate)8 EnableIfReplicaSetAvailable (org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable)6 StepVerifier (reactor.test.StepVerifier)5 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)4 Disposable (reactor.core.Disposable)4 VersionedPerson (org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson)3 Document (org.springframework.data.mongodb.core.mapping.Document)3 MongoTransactionManager (org.springframework.data.mongodb.MongoTransactionManager)2 Person (org.springframework.data.mongodb.repository.Person)2 VersionedPerson (org.springframework.data.mongodb.repository.VersionedPerson)2 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)2 ClientSessionOptions (com.mongodb.ClientSessionOptions)1 ClientSession (com.mongodb.reactivestreams.client.ClientSession)1 MongoClient (com.mongodb.reactivestreams.client.MongoClient)1 Instant (java.time.Instant)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Supplier (java.util.function.Supplier)1 Assertions (org.assertj.core.api.Assertions)1