DATAES-886 - Complete reactive auditing.

Original PR: #496
This commit is contained in:
Peter-Josef Meisch
2020-07-18 19:34:00 +02:00
committed by GitHub
parent bdcecd0950
commit ddee81f82e
9 changed files with 241 additions and 135 deletions

View File

@@ -17,8 +17,9 @@ package org.springframework.data.elasticsearch.config;
import static org.assertj.core.api.Assertions.*;
import reactor.core.publisher.Mono;
import java.time.LocalDateTime;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,8 +31,8 @@ import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.domain.Persistable;
import org.springframework.data.domain.ReactiveAuditorAware;
import org.springframework.data.elasticsearch.core.event.ReactiveBeforeConvertCallback;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
@@ -49,23 +50,23 @@ import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(classes = { ReactiveAuditingIntegrationTest.Config.class })
public class ReactiveAuditingIntegrationTest {
public static AuditorAware<String> auditorProvider() {
return new AuditorAware<String>() {
public static ReactiveAuditorAware<String> auditorProvider() {
return new ReactiveAuditorAware<String>() {
int count = 0;
@Override
public Optional<String> getCurrentAuditor() {
return Optional.of("Auditor " + (++count));
public Mono<String> getCurrentAuditor() {
return Mono.just("Auditor " + (++count));
}
};
}
@Import({ ReactiveElasticsearchRestTemplateConfiguration.class })
@EnableElasticsearchAuditing(auditorAwareRef = "auditorAware")
@EnableReactiveElasticsearchAuditing(auditorAwareRef = "auditorAware")
static class Config {
@Bean
public AuditorAware<String> auditorAware() {
public ReactiveAuditorAware<String> auditorAware() {
return auditorProvider();
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.data.elasticsearch.core.event;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.time.LocalDateTime;
@@ -31,7 +32,7 @@ import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.auditing.IsNewAwareAuditingHandler;
import org.springframework.data.auditing.ReactiveIsNewAwareAuditingHandler;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.mapping.context.PersistentEntities;
@@ -44,14 +45,14 @@ import org.springframework.lang.Nullable;
@ExtendWith(MockitoExtension.class)
class ReactiveAuditingEntityCallbackTests {
IsNewAwareAuditingHandler handler;
ReactiveIsNewAwareAuditingHandler handler;
ReactiveAuditingEntityCallback callback;
@BeforeEach
void setUp() {
SimpleElasticsearchMappingContext context = new SimpleElasticsearchMappingContext();
context.getPersistentEntity(Sample.class);
handler = spy(new IsNewAwareAuditingHandler(PersistentEntities.of(context)));
handler = spy(new ReactiveIsNewAwareAuditingHandler(PersistentEntities.of(context)));
callback = new ReactiveAuditingEntityCallback(() -> handler);
}
@@ -81,13 +82,10 @@ class ReactiveAuditingEntityCallbackTests {
sample1.setId("1");
Sample sample2 = new Sample();
sample2.setId("2");
doReturn(sample2).when(handler).markAudited(any());
doReturn(Mono.just(sample2)).when(handler).markAudited(any());
callback.onBeforeConvert(sample1, IndexCoordinates.of("index")) //
.as(StepVerifier::create) //
.consumeNextWith(it -> { //
assertThat(it).isSameAs(sample2); //
}).verifyComplete();
.as(StepVerifier::create).expectNext(sample2).verifyComplete();
}
static class Sample {

View File

@@ -30,9 +30,9 @@ import org.springframework.util.StringUtils;
/**
* This class manages the connection to an Elasticsearch Cluster, starting a local one if necessary. The information
* about the ClusterConnection is stored botha s a varaible in the instance for direct aaces from JUnit 5 and in a
* static ThreadLocal<ClusterConnectionInfo> acessible with the {@link ClusterConnection#clusterConnectionInfo()} method
* to be integrated in the Spring setup
* about the ClusterConnection is stored both as a variable in the instance for direct access from JUnit 5 and in a
* static ThreadLocal<ClusterConnectionInfo> accessible with the {@link ClusterConnection#clusterConnectionInfo()}
* method to be integrated in the Spring setup
*
* @author Peter-Josef Meisch
*/