Search in sources :

Example 11 with EnableIfMongoServerVersion

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

the class MongoTemplateUpdateTests method aggregateUpdateWithSetToValue.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void aggregateUpdateWithSetToValue() {
    Book one = new Book();
    one.id = 1;
    one.author = new Author("John", "Backus");
    template.insertAll(Arrays.asList(one));
    AggregationUpdate update = AggregationUpdate.update().set("author").toValue(new Author("Ada", "Lovelace"));
    template.update(Book.class).matching(Query.query(Criteria.where("id").is(one.id))).apply(update).all();
    assertThat(all(Book.class)).containsExactlyInAnyOrder(org.bson.Document.parse("{\"_id\" : 1, \"author\" : {\"first\" : \"Ada\", \"last\" : \"Lovelace\"}, \"_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 12 with EnableIfMongoServerVersion

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

the class MongoTemplateUpdateTests method versionedAggregateUpdateTouchingVersionProperty.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void versionedAggregateUpdateTouchingVersionProperty() {
    Versioned source = template.insert(Versioned.class).one(new Versioned("id-1", "value-0"));
    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();
    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", 10L).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 13 with EnableIfMongoServerVersion

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

the class MongoTemplateUpdateTests method aggregateUpdateWithReplaceWith.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
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));
    AggregationUpdate update = AggregationUpdate.update().replaceWith(ReplaceWithOperation.replaceWithValueOf("author"));
    template.update(Book.class).apply(update).all();
    assertThat(all(Book.class)).containsExactlyInAnyOrder(org.bson.Document.parse("{\"_id\" : 1, \"first\" : \"John\", \"last\" : \"Backus\"}"), org.bson.Document.parse("{\"_id\" : 2, \"first\" : \"Grace\", \"last\" : \"Hopper\"}"));
}
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 14 with EnableIfMongoServerVersion

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

the class MongoTemplateUpdateTests method aggregateUpdateWithReplaceWithNewObject.

// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void aggregateUpdateWithReplaceWithNewObject() {
    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));
    AggregationUpdate update = AggregationUpdate.update().replaceWith(new Author("Ada", "Lovelace"));
    template.update(Book.class).matching(Query.query(Criteria.where("id").is(one.id))).apply(update).all();
    assertThat(all(Book.class)).containsExactlyInAnyOrder(org.bson.Document.parse("{\"_id\" : 1, \"first\" : \"Ada\", \"last\" : \"Lovelace\", \"_class\" : \"org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Author\"}"), org.bson.Document.parse("{\"_id\" : 2, \"author\" : {\"first\" : \"Grace\", \"last\" : \"Hopper\"}, \"_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 15 with EnableIfMongoServerVersion

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

the class SimpleMongoRepositoryTests method existsShouldBePossibleInTransaction.

// DATAMONGO-2130
@Test
@EnableIfReplicaSetAvailable
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
void existsShouldBePossibleInTransaction() {
    MongoTransactionManager txmgr = new MongoTransactionManager(template.getMongoDbFactory());
    TransactionTemplate tt = new TransactionTemplate(txmgr);
    tt.afterPropertiesSet();
    boolean exists = tt.execute(status -> {
        Person sample = new Person();
        sample.setLastname("Matthews");
        repository.save(sample);
        return repository.existsById(sample.getId());
    });
    assertThat(exists).isTrue();
}
Also used : MongoTransactionManager(org.springframework.data.mongodb.MongoTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Person(org.springframework.data.mongodb.repository.Person) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) 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