DATACMNS-1231 - Introduce dedicated Auditor value object.

Original Pull Request: #458
This commit is contained in:
Christoph Strobl
2020-07-16 12:21:09 +02:00
parent d3af4a79e4
commit d4e94109df
8 changed files with 245 additions and 50 deletions

View File

@@ -36,6 +36,7 @@ import org.springframework.data.mapping.context.SampleMappingContext;
* Unit test for {@code AuditingHandler}.
*
* @author Oliver Gierke
* @author Christoph Strobl
* @since 1.5
*/
@SuppressWarnings("unchecked")
@@ -177,6 +178,27 @@ class AuditingHandlerUnitTests {
assertThat(result.modified).isNotNull();
}
@Test // DATACMNS-1231
void getAuditorGetsAuditorNoneWhenNoAuditorAwareNotPresent() {
assertThat(handler.getAuditor()).isEqualTo(Auditor.none());
}
@Test // DATACMNS-1231
void getAuditorGetsAuditorWhenPresent() {
handler.setAuditorAware(auditorAware);
assertThat(handler.getAuditor()).isEqualTo(Auditor.of(user));
}
@Test // DATACMNS-1231
void getAuditorShouldReturnNoneIfAuditorAwareDoesNotHoldObject() {
when(auditorAware.getCurrentAuditor()).thenReturn(Optional.empty());
handler.setAuditorAware(auditorAware);
assertThat(handler.getAuditor()).isEqualTo(Auditor.none());
}
static abstract class AbstractModel {
@CreatedDate Instant created;

View File

@@ -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.auditing;
import static org.assertj.core.api.Assertions.*;
import java.util.Optional;
import org.junit.jupiter.api.Test;
/**
* @author Christoph Strobl
*/
class AuditorUnitTests {
@Test // DATACMNS-1231
void auditorOfEmptyOptionalIsNone() {
assertThat(Auditor.ofOptional(Optional.empty())).isEqualTo(Auditor.none());
}
@Test // DATACMNS-1231
void auditorOfOptionalIsValue() {
assertThat(Auditor.ofOptional(Optional.of("batman"))).isEqualTo(Auditor.of("batman"));
}
@Test // DATACMNS-1231
void auditorOfMustNotWrapOtherAuditor() {
Auditor<String> source = Auditor.of("batman");
assertThat(Auditor.of(source)).isSameAs(source);
}
@Test // DATACMNS-1231
void auditorOfNullIsNone() {
assertThat(Auditor.of(null)).isEqualTo(Auditor.none());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2020 the original author or authors.
* 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.
@@ -26,7 +26,6 @@ import java.time.Instant;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
@@ -62,7 +61,7 @@ class ReactiveAuditingHandlerUnitTests {
when(auditorAware.getCurrentAuditor()).thenReturn(Mono.just(user));
}
@Test
@Test // DATACMNS-1231
void markCreatedShouldSetDatesIfAuditorNotSet() {
Immutable immutable = new Immutable(null, null, null, null);
@@ -79,7 +78,7 @@ class ReactiveAuditingHandlerUnitTests {
assertThat(immutable.getCreatedDate()).isNull();
}
@Test
@Test // DATACMNS-1231
void markModifiedSetsModifiedFields() {
AuditedUser audited = new AuditedUser();