#281 - Add support for auditing.
We now provide auditing support that can be enabled through EnableR2dbcAuditing.
@Configuration
@EnableR2dbcAuditing
class Config {
@Bean
public ReactiveAuditorAware<String> myAuditorProvider() {
return new AuditorAwareImpl();
}
}
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.r2dbc.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import lombok.Data;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
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.ReactiveAuditorAware;
|
||||
import org.springframework.data.mapping.callback.ReactiveEntityCallbacks;
|
||||
import org.springframework.data.r2dbc.mapping.R2dbcMappingContext;
|
||||
import org.springframework.data.r2dbc.mapping.event.BeforeConvertCallback;
|
||||
import org.springframework.data.relational.core.sql.SqlIdentifier;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link EnableR2dbcAuditing}
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class AuditingUnitTests {
|
||||
|
||||
@EnableR2dbcAuditing(auditorAwareRef = "myAuditor")
|
||||
static class AuditingConfiguration {
|
||||
|
||||
@Bean
|
||||
ReactiveAuditorAware<String> myAuditor() {
|
||||
return () -> Mono.just("Walter");
|
||||
}
|
||||
|
||||
@Bean
|
||||
R2dbcMappingContext r2dbcMappingContext() {
|
||||
return new R2dbcMappingContext();
|
||||
}
|
||||
}
|
||||
|
||||
@Test // gh-281
|
||||
public void enablesAuditingAndSetsPropertiesAccordingly() throws Exception {
|
||||
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AuditingConfiguration.class);
|
||||
|
||||
R2dbcMappingContext mappingContext = context.getBean(R2dbcMappingContext.class);
|
||||
mappingContext.getPersistentEntity(Entity.class);
|
||||
|
||||
ReactiveEntityCallbacks callbacks = ReactiveEntityCallbacks.create(context);
|
||||
|
||||
Entity entity = new Entity();
|
||||
entity = callbacks.callback(BeforeConvertCallback.class, entity, SqlIdentifier.unquoted("table")).block();
|
||||
|
||||
assertThat(entity.created).isNotNull();
|
||||
assertThat(entity.modified).isEqualTo(entity.created);
|
||||
assertThat(entity.modifiedBy).isEqualTo("Walter");
|
||||
|
||||
Thread.sleep(10);
|
||||
entity.id = 1L;
|
||||
|
||||
entity = callbacks.callback(BeforeConvertCallback.class, entity, SqlIdentifier.unquoted("table")).block();
|
||||
|
||||
assertThat(entity.created).isNotNull();
|
||||
assertThat(entity.modified).isNotEqualTo(entity.created);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Data
|
||||
class Entity {
|
||||
|
||||
@Id Long id;
|
||||
@CreatedDate LocalDateTime created;
|
||||
@LastModifiedDate LocalDateTime modified;
|
||||
@LastModifiedBy String modifiedBy;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.r2dbc.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link R2dbcAuditingRegistrar}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class R2dbcAuditingRegistrarUnitTests {
|
||||
|
||||
private R2dbcAuditingRegistrar registrar = new R2dbcAuditingRegistrar();
|
||||
|
||||
@Mock AnnotationMetadata metadata;
|
||||
@Mock BeanDefinitionRegistry registry;
|
||||
|
||||
@Test // gh-281
|
||||
public void rejectsNullAnnotationMetadata() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> registrar.registerBeanDefinitions(null, registry));
|
||||
}
|
||||
|
||||
@Test // gh-281
|
||||
public void rejectsNullBeanDefinitionRegistry() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> registrar.registerBeanDefinitions(metadata, null));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user