Search in sources :

Example 11 with AggregationUpdate

use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUnitTests method updateShouldPassOnUnsetCorrectly.

// DATAMONGO-2331
@Test
void updateShouldPassOnUnsetCorrectly() {
    SetOperation setOperation = SetOperation.builder().set("status").toValue("Modified").and().set("comments").toValue(Fields.fields("misc1").and("misc2").asList());
    AggregationUpdate update = AggregationUpdate.update();
    update.set(setOperation);
    update.unset("misc1", "misc2");
    template.updateFirst(new BasicQuery("{}"), update, Wrapper.class).subscribe();
    ArgumentCaptor<List<Document>> captor = ArgumentCaptor.forClass(List.class);
    verify(collection, times(1)).updateOne(any(org.bson.Document.class), captor.capture(), any(UpdateOptions.class));
    assertThat(captor.getValue()).isEqualTo(Arrays.asList(Document.parse("{ $set: { status: \"Modified\", comments: [ \"$misc1\", \"$misc2\" ] } }"), Document.parse("{ $unset: [ \"misc1\", \"misc2\" ] }")));
}
Also used : SetOperation(org.springframework.data.mongodb.core.aggregation.SetOperation) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Test(org.junit.jupiter.api.Test)

Example 12 with AggregationUpdate

use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUpdateTests method versionedAggregateUpdateTouchingVersionProperty.

// DATAMONGO-2331
@Test
public void versionedAggregateUpdateTouchingVersionProperty() {
    Versioned source = new Versioned("id-1", "value-0");
    template.insert(Versioned.class).one(source).then().as(StepVerifier::create).verifyComplete();
    AggregationUpdate update = AggregationUpdate.update().set(SetOperation.builder().set("value").toValue("changed").and().set("version").toValue(10L));
    template.update(Versioned.class).matching(Query.query(Criteria.where("id").is(source.id))).apply(update).first().then().as(StepVerifier::create).verifyComplete();
    Flux.from(collection(Versioned.class).find(new org.bson.Document("_id", source.id)).limit(1)).collectList().as(StepVerifier::create).consumeNextWith(it -> {
        assertThat(it).containsExactly(new org.bson.Document("_id", source.id).append("version", 10L).append("value", "changed").append("_class", "org.springframework.data.mongodb.core.ReactiveMongoTemplateUpdateTests$Versioned"));
    }).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Version(org.springframework.data.annotation.Version) ReplaceWithOperation(org.springframework.data.mongodb.core.aggregation.ReplaceWithOperation) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ArrayList(java.util.ArrayList) Document(org.springframework.data.mongodb.core.mapping.Document) MongoClient(com.mongodb.reactivestreams.client.MongoClient) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions(org.assertj.core.api.Assertions) MongoTestUtils(org.springframework.data.mongodb.test.util.MongoTestUtils) Collection(java.util.Collection) EqualsAndHashCode(lombok.EqualsAndHashCode) Client(org.springframework.data.mongodb.test.util.Client) SetOperation(org.springframework.data.mongodb.core.aggregation.SetOperation) Field(org.springframework.data.mongodb.core.mapping.Field) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) ArithmeticOperators(org.springframework.data.mongodb.core.aggregation.ArithmeticOperators) Id(org.springframework.data.annotation.Id) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) MongoClientExtension(org.springframework.data.mongodb.test.util.MongoClientExtension) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 13 with AggregationUpdate

use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUpdateTests method aggregateUpdateWithReplaceWith.

// DATAMONGO-2331
@Test
public void aggregateUpdateWithReplaceWith() {
    Book one = new Book();
    one.id = 1;
    one.author = new Author("John", "Backus");
    Book two = new Book();
    two.id = 2;
    two.author = new Author("Grace", "Hopper");
    template.insertAll(Arrays.asList(one, two)).then().as(StepVerifier::create).verifyComplete();
    ;
    AggregationUpdate update = AggregationUpdate.update().replaceWith(ReplaceWithOperation.replaceWithValueOf("author"));
    template.update(Book.class).apply(update).all().then().as(StepVerifier::create).verifyComplete();
    all(Book.class).collectList().as(StepVerifier::create).consumeNextWith(it -> {
        assertThat(it).containsExactlyInAnyOrder(org.bson.Document.parse("{\"_id\" : 1, \"first\" : \"John\", \"last\" : \"Backus\"}"), org.bson.Document.parse("{\"_id\" : 2, \"first\" : \"Grace\", \"last\" : \"Hopper\"}"));
    }).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Version(org.springframework.data.annotation.Version) ReplaceWithOperation(org.springframework.data.mongodb.core.aggregation.ReplaceWithOperation) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ArrayList(java.util.ArrayList) Document(org.springframework.data.mongodb.core.mapping.Document) MongoClient(com.mongodb.reactivestreams.client.MongoClient) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions(org.assertj.core.api.Assertions) MongoTestUtils(org.springframework.data.mongodb.test.util.MongoTestUtils) Collection(java.util.Collection) EqualsAndHashCode(lombok.EqualsAndHashCode) Client(org.springframework.data.mongodb.test.util.Client) SetOperation(org.springframework.data.mongodb.core.aggregation.SetOperation) Field(org.springframework.data.mongodb.core.mapping.Field) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) ArithmeticOperators(org.springframework.data.mongodb.core.aggregation.ArithmeticOperators) Id(org.springframework.data.annotation.Id) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) MongoClientExtension(org.springframework.data.mongodb.test.util.MongoClientExtension) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 14 with AggregationUpdate

use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUpdateTests method versionedAggregateUpdateWithSet.

// DATAMONGO-2331
@Test
public void versionedAggregateUpdateWithSet() {
    Versioned source = new Versioned("id-1", "value-0");
    template.insert(Versioned.class).one(source).then().as(StepVerifier::create).verifyComplete();
    AggregationUpdate update = AggregationUpdate.update().set("value").toValue("changed");
    template.update(Versioned.class).matching(Query.query(Criteria.where("id").is(source.id))).apply(update).first().then().as(StepVerifier::create).verifyComplete();
    Flux.from(collection(Versioned.class).find(new org.bson.Document("_id", source.id)).limit(1)).collectList().as(StepVerifier::create).consumeNextWith(it -> {
        assertThat(it).containsExactly(new org.bson.Document("_id", source.id).append("version", 1L).append("value", "changed").append("_class", "org.springframework.data.mongodb.core.ReactiveMongoTemplateUpdateTests$Versioned"));
    }).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Version(org.springframework.data.annotation.Version) ReplaceWithOperation(org.springframework.data.mongodb.core.aggregation.ReplaceWithOperation) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ArrayList(java.util.ArrayList) Document(org.springframework.data.mongodb.core.mapping.Document) MongoClient(com.mongodb.reactivestreams.client.MongoClient) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions(org.assertj.core.api.Assertions) MongoTestUtils(org.springframework.data.mongodb.test.util.MongoTestUtils) Collection(java.util.Collection) EqualsAndHashCode(lombok.EqualsAndHashCode) Client(org.springframework.data.mongodb.test.util.Client) SetOperation(org.springframework.data.mongodb.core.aggregation.SetOperation) Field(org.springframework.data.mongodb.core.mapping.Field) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) ArithmeticOperators(org.springframework.data.mongodb.core.aggregation.ArithmeticOperators) Id(org.springframework.data.annotation.Id) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) MongoClientExtension(org.springframework.data.mongodb.test.util.MongoClientExtension) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 15 with AggregationUpdate

use of org.springframework.data.mongodb.core.aggregation.AggregationUpdate in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUpdateTests method aggregateUpdateWithUnset.

// DATAMONGO-2331
@Test
public 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)).then().as(StepVerifier::create).verifyComplete();
    AggregationUpdate update = AggregationUpdate.update().unset("isbn", "stock");
    template.update(Book.class).apply(update).all().then().as(StepVerifier::create).verifyComplete();
    all(Book.class).collectList().as(StepVerifier::create).consumeNextWith(it -> {
        // 
        assertThat(it).containsExactlyInAnyOrder(org.bson.Document.parse("{ \"_id\" : 1, \"title\" : \"Antelope Antics\", \"author\" : { \"last\" : \"An\", \"first\" : \"Auntie\" }, \"_class\" : \"org.springframework.data.mongodb.core.ReactiveMongoTemplateUpdateTests$Book\" }"), org.bson.Document.parse("{ \"_id\" : 2, \"title\" : \"Bees Babble\", \"author\" : { \"last\" : \"Bumble\", \"first\" : \"Bee\" }, \"_class\" : \"org.springframework.data.mongodb.core.ReactiveMongoTemplateUpdateTests$Book\" }"));
    }).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Version(org.springframework.data.annotation.Version) ReplaceWithOperation(org.springframework.data.mongodb.core.aggregation.ReplaceWithOperation) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ArrayList(java.util.ArrayList) Document(org.springframework.data.mongodb.core.mapping.Document) MongoClient(com.mongodb.reactivestreams.client.MongoClient) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions(org.assertj.core.api.Assertions) MongoTestUtils(org.springframework.data.mongodb.test.util.MongoTestUtils) Collection(java.util.Collection) EqualsAndHashCode(lombok.EqualsAndHashCode) Client(org.springframework.data.mongodb.test.util.Client) SetOperation(org.springframework.data.mongodb.core.aggregation.SetOperation) Field(org.springframework.data.mongodb.core.mapping.Field) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) ArithmeticOperators(org.springframework.data.mongodb.core.aggregation.ArithmeticOperators) Id(org.springframework.data.annotation.Id) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) MongoClientExtension(org.springframework.data.mongodb.test.util.MongoClientExtension) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)19 AggregationUpdate (org.springframework.data.mongodb.core.aggregation.AggregationUpdate)19 EnableIfMongoServerVersion (org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion)14 ArrayList (java.util.ArrayList)11 List (java.util.List)11 Document (org.springframework.data.mongodb.core.mapping.Document)9 SetOperation (org.springframework.data.mongodb.core.aggregation.SetOperation)7 MongoClient (com.mongodb.reactivestreams.client.MongoClient)6 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)6 Arrays (java.util.Arrays)6 Collection (java.util.Collection)6 EqualsAndHashCode (lombok.EqualsAndHashCode)6 Assertions (org.assertj.core.api.Assertions)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)6 Id (org.springframework.data.annotation.Id)6 Version (org.springframework.data.annotation.Version)6 ArithmeticOperators (org.springframework.data.mongodb.core.aggregation.ArithmeticOperators)6 ReplaceWithOperation (org.springframework.data.mongodb.core.aggregation.ReplaceWithOperation)6 Field (org.springframework.data.mongodb.core.mapping.Field)6