Search in sources :

Example 1 with KAuditableVersionedEntity

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

the class ReactiveMongoTemplateAuditingTests method auditingSetsLastModifiedDateCorrectlyForImmutableVersionedKotlinEntityOnSave.

// DATAMONGO-2346
@Test
void auditingSetsLastModifiedDateCorrectlyForImmutableVersionedKotlinEntityOnSave() {
    KAuditableVersionedEntity entity = new KAuditableVersionedEntity(null, "value", null, null);
    // 
    template.save(entity).delayElement(Duration.ofMillis(500)).flatMap(inserted -> // 
    template.save(inserted.withValue("changed-value")).map(// 
    updated -> Tuples.of(inserted, updated))).flatMap(tuple2 -> template.findOne(Query.query(Criteria.where("id").is(tuple2.getT1().getId())), KAuditableVersionedEntity.class).map(// 
    fetched -> Tuples.of(tuple2.getT1(), tuple2.getT2(), fetched))).as(// 
    StepVerifier::create).consumeNextWith(tuple3 -> {
        assertThat(tuple3.getT2().getModificationDate()).isAfter(tuple3.getT1().getModificationDate());
        assertThat(tuple3.getT3().getModificationDate()).isAfter(tuple3.getT1().getModificationDate());
        assertThat(tuple3.getT3().getModificationDate()).isEqualTo(tuple3.getT2().getModificationDate().truncatedTo(ChronoUnit.MILLIS));
    }).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) AbstractReactiveMongoConfiguration(org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration) Tuples(reactor.util.function.Tuples) Version(org.springframework.data.annotation.Version) Autowired(org.springframework.beans.factory.annotation.Autowired) Document(org.springframework.data.mongodb.core.mapping.Document) MongoClient(com.mongodb.reactivestreams.client.MongoClient) ReactiveAuditorAware(org.springframework.data.domain.ReactiveAuditorAware) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) EnableReactiveMongoAuditing(org.springframework.data.mongodb.config.EnableReactiveMongoAuditing) Duration(java.time.Duration) Assertions(org.assertj.core.api.Assertions) KAuditableVersionedEntity(org.springframework.data.mongodb.core.KAuditableVersionedEntity) MongoTestUtils(org.springframework.data.mongodb.test.util.MongoTestUtils) SpringExtension(org.springframework.test.context.junit.jupiter.SpringExtension) Set(java.util.Set) Mono(reactor.core.publisher.Mono) Instant(java.time.Instant) Client(org.springframework.data.mongodb.test.util.Client) LastModifiedDate(org.springframework.data.annotation.LastModifiedDate) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) LastModifiedBy(org.springframework.data.annotation.LastModifiedBy) ChronoUnit(java.time.temporal.ChronoUnit) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) Bean(org.springframework.context.annotation.Bean) Id(org.springframework.data.annotation.Id) Collections(java.util.Collections) MongoClientExtension(org.springframework.data.mongodb.test.util.MongoClientExtension) KAuditableVersionedEntity(org.springframework.data.mongodb.core.KAuditableVersionedEntity) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 2 with KAuditableVersionedEntity

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

the class MongoTemplateAuditingTests method auditingSetsLastModifiedDateCorrectlyForImmutableVersionedKotlinEntityOnSave.

// DATAMONGO-2346
@Test
public void auditingSetsLastModifiedDateCorrectlyForImmutableVersionedKotlinEntityOnSave() throws InterruptedException {
    template.remove(new Query(), KAuditableVersionedEntity.class);
    KAuditableVersionedEntity entity = new KAuditableVersionedEntity("kId-1", "value", null, null);
    KAuditableVersionedEntity inserted = template.save(entity);
    TimeUnit.MILLISECONDS.sleep(500);
    KAuditableVersionedEntity updated = template.save(inserted.withValue("changed-value"));
    KAuditableVersionedEntity fetched = template.findOne(Query.query(Criteria.where("id").is(entity.getId())), KAuditableVersionedEntity.class);
    assertThat(updated.getModificationDate()).isAfter(inserted.getModificationDate());
    assertThat(fetched.getModificationDate()).isAfter(inserted.getModificationDate());
    assertThat(fetched.getModificationDate()).isEqualTo(updated.getModificationDate().truncatedTo(ChronoUnit.MILLIS));
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) KAuditableVersionedEntity(org.springframework.data.mongodb.core.KAuditableVersionedEntity) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)2 KAuditableVersionedEntity (org.springframework.data.mongodb.core.KAuditableVersionedEntity)2 Query (org.springframework.data.mongodb.core.query.Query)2 MongoClient (com.mongodb.reactivestreams.client.MongoClient)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 ChronoUnit (java.time.temporal.ChronoUnit)1 Collections (java.util.Collections)1 Set (java.util.Set)1 Assertions (org.assertj.core.api.Assertions)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Bean (org.springframework.context.annotation.Bean)1 Configuration (org.springframework.context.annotation.Configuration)1 Id (org.springframework.data.annotation.Id)1 LastModifiedBy (org.springframework.data.annotation.LastModifiedBy)1 LastModifiedDate (org.springframework.data.annotation.LastModifiedDate)1 Version (org.springframework.data.annotation.Version)1 ReactiveAuditorAware (org.springframework.data.domain.ReactiveAuditorAware)1