Search in sources :

Example 1 with EnableIfMongoServerVersion

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

the class MongoTemplateUpdateTests method aggregateUpdateWithSet.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void aggregateUpdateWithSet() {
    Score score1 = new Score(1, "Maya", Arrays.asList(10, 5, 10), Arrays.asList(10, 8), 0);
    Score score2 = new Score(2, "Ryan", Arrays.asList(5, 6, 5), Arrays.asList(8, 8), 8);
    template.insertAll(Arrays.asList(score1, score2));
    AggregationUpdate update = AggregationUpdate.update().set(// 
    SetOperation.builder().set("totalHomework").toValueOf(ArithmeticOperators.valueOf("homework").sum()).and().set("totalQuiz").toValueOf(// 
    ArithmeticOperators.valueOf("quiz").sum())).set(// 
    SetOperation.builder().set("totalScore").toValueOf(ArithmeticOperators.valueOf("totalHomework").add("totalQuiz").add("extraCredit")));
    template.update(Score.class).apply(update).all();
    // 
    assertThat(collection(Score.class).find(new org.bson.Document()).into(new ArrayList<>())).containsExactlyInAnyOrder(org.bson.Document.parse("{\"_id\" : 1, \"student\" : \"Maya\", \"homework\" : [ 10, 5, 10 ], \"quiz\" : [ 10, 8 ], \"extraCredit\" : 0, \"totalHomework\" : 25, \"totalQuiz\" : 18, \"totalScore\" : 43,  \"_class\" : \"org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Score\"}"), org.bson.Document.parse("{ \"_id\" : 2, \"student\" : \"Ryan\", \"homework\" : [ 5, 6, 5 ], \"quiz\" : [ 8, 8 ], \"extraCredit\" : 8, \"totalHomework\" : 16, \"totalQuiz\" : 16, \"totalScore\" : 40, \"_class\" : \"org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Score\"}"));
}
Also used : AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) Document(org.springframework.data.mongodb.core.mapping.Document) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Example 2 with EnableIfMongoServerVersion

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

the class MongoTemplateUpdateTests method versionedAggregateUpdateWithSet.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void versionedAggregateUpdateWithSet() {
    Versioned source = template.insert(Versioned.class).one(new Versioned("id-1", "value-0"));
    AggregationUpdate update = AggregationUpdate.update().set("value").toValue("changed");
    template.update(Versioned.class).matching(Query.query(Criteria.where("id").is(source.id))).apply(update).first();
    assertThat(collection(Versioned.class).find(new org.bson.Document("_id", source.id)).limit(1).into(new ArrayList<>())).containsExactly(new org.bson.Document("_id", source.id).append("version", 1L).append("value", "changed").append("_class", "org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Versioned"));
}
Also used : AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) Document(org.springframework.data.mongodb.core.mapping.Document) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Example 3 with EnableIfMongoServerVersion

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

the class MongoTemplateUpdateTests method aggregationUpdateUpsertsCorrectly.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void aggregationUpdateUpsertsCorrectly() {
    AggregationUpdate update = AggregationUpdate.update().set("title").toValue("The Burning White");
    template.update(Book.class).matching(Query.query(Criteria.where("id").is(1))).apply(update).upsert();
    assertThat(all(Book.class)).containsExactly(org.bson.Document.parse("{\"_id\" : 1, \"title\" : \"The Burning White\" }"));
}
Also used : AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Example 4 with EnableIfMongoServerVersion

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

the class MongoTemplateUpdateTests method aggregateUpdateWithUnset.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void aggregateUpdateWithUnset() {
    Book antelopeAntics = new Book();
    antelopeAntics.id = 1;
    antelopeAntics.title = "Antelope Antics";
    antelopeAntics.isbn = "0001122223334";
    antelopeAntics.author = new Author("Auntie", "An");
    antelopeAntics.stock = new ArrayList<>();
    antelopeAntics.stock.add(new Warehouse("A", 5));
    antelopeAntics.stock.add(new Warehouse("B", 15));
    Book beesBabble = new Book();
    beesBabble.id = 2;
    beesBabble.title = "Bees Babble";
    beesBabble.isbn = "999999999333";
    beesBabble.author = new Author("Bee", "Bumble");
    beesBabble.stock = new ArrayList<>();
    beesBabble.stock.add(new Warehouse("A", 2));
    beesBabble.stock.add(new Warehouse("B", 5));
    template.insertAll(Arrays.asList(antelopeAntics, beesBabble));
    AggregationUpdate update = AggregationUpdate.update().unset("isbn", "stock");
    template.update(Book.class).apply(update).all();
    // 
    assertThat(all(Book.class)).containsExactlyInAnyOrder(org.bson.Document.parse("{ \"_id\" : 1, \"title\" : \"Antelope Antics\", \"author\" : { \"last\" : \"An\", \"first\" : \"Auntie\" }, \"_class\" : \"org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Book\" }"), org.bson.Document.parse("{ \"_id\" : 2, \"title\" : \"Bees Babble\", \"author\" : { \"last\" : \"Bumble\", \"first\" : \"Bee\" }, \"_class\" : \"org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Book\" }"));
}
Also used : AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Example 5 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)

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