Add CustomAuditHandler for supporting OffsetDateTime and store in UTC
This commit is contained in:
committed by
Arnold Gálovics
parent
da920b1824
commit
b8f8217148
+71
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you 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
|
||||
*
|
||||
* http://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.apache.fineract.infrastructure.core.auditing;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Optional;
|
||||
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant;
|
||||
import org.apache.fineract.infrastructure.core.service.DateUtils;
|
||||
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CustomDateTimeProviderTest {
|
||||
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
|
||||
ThreadLocalContextUtil.setTenant(new FineractPlatformTenant(1L, "default", "Default", "Asia/Kolkata", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instanceDateProvider() {
|
||||
Optional<TemporalAccessor> dateTimeProvider = CustomDateTimeProvider.INSTANCE.getNow();
|
||||
LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault());
|
||||
assertTrue(dateTimeProvider.isPresent());
|
||||
assertTrue(dateTimeProvider.get() instanceof LocalDateTime);
|
||||
|
||||
assertEquals(now.getYear(), ((LocalDateTime) dateTimeProvider.get()).getYear());
|
||||
assertEquals(now.getMonth(), ((LocalDateTime) dateTimeProvider.get()).getMonth());
|
||||
assertEquals(now.getDayOfMonth(), ((LocalDateTime) dateTimeProvider.get()).getDayOfMonth());
|
||||
assertEquals(now.getHour(), ((LocalDateTime) dateTimeProvider.get()).getHour());
|
||||
assertEquals(now.getMinute(), ((LocalDateTime) dateTimeProvider.get()).getMinute());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tenantDateProvider() {
|
||||
Optional<TemporalAccessor> dateTimeProvider = CustomDateTimeProvider.TENANT.getNow();
|
||||
OffsetDateTime now = OffsetDateTime.now(DateUtils.getDateTimeZoneOfTenant());
|
||||
assertTrue(dateTimeProvider.isPresent());
|
||||
assertTrue(dateTimeProvider.get() instanceof OffsetDateTime);
|
||||
|
||||
assertEquals(now.getYear(), ((OffsetDateTime) dateTimeProvider.get()).getYear());
|
||||
assertEquals(now.getMonth(), ((OffsetDateTime) dateTimeProvider.get()).getMonth());
|
||||
assertEquals(now.getDayOfMonth(), ((OffsetDateTime) dateTimeProvider.get()).getDayOfMonth());
|
||||
assertEquals(now.getHour(), ((OffsetDateTime) dateTimeProvider.get()).getHour());
|
||||
assertEquals(now.getMinute(), ((OffsetDateTime) dateTimeProvider.get()).getMinute());
|
||||
}
|
||||
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you 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
|
||||
*
|
||||
* http://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.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
|
||||
import org.apache.fineract.infrastructure.core.domain.AbstractAuditableCustom;
|
||||
import org.apache.fineract.infrastructure.core.domain.AbstractAuditableWithUTCDateTimeCustom;
|
||||
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant;
|
||||
import org.apache.fineract.infrastructure.core.service.DateUtils;
|
||||
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.mapping.context.PersistentEntities;
|
||||
|
||||
public class CustomAuditingHandlerTest {
|
||||
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
ThreadLocalContextUtil.setTenant(new FineractPlatformTenant(1L, "default", "Default", "Asia/Kolkata", null));
|
||||
ThreadLocalContextUtil
|
||||
.setBusinessDates(new HashMap<>(Map.of(BusinessDateType.BUSINESS_DATE, LocalDate.now(ZoneId.of("Asia/Kolkata")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void markCreated() {
|
||||
Auditor auditor = Mockito.mock(Auditor.class);
|
||||
MappingContext mappingContext = Mockito.mock(MappingContext.class);
|
||||
CustomAuditingHandler testInstance = new CustomAuditingHandler(PersistentEntities.of(mappingContext));
|
||||
AbstractAuditableWithUTCDateTimeCustom targetObject = Mockito.spy(AbstractAuditableWithUTCDateTimeCustom.class);
|
||||
targetObject = testInstance.markCreated(auditor, targetObject);
|
||||
OffsetDateTime now = OffsetDateTime.now(DateUtils.getDateTimeZoneOfTenant());
|
||||
|
||||
assertTrue(targetObject.getCreatedDate().isPresent());
|
||||
assertEquals(now.getYear(), targetObject.getCreatedDate().get().getYear());
|
||||
assertEquals(now.getMonth(), targetObject.getCreatedDate().get().getMonth());
|
||||
assertEquals(now.getDayOfMonth(), targetObject.getCreatedDate().get().getDayOfMonth());
|
||||
assertEquals(now.getHour(), targetObject.getCreatedDate().get().getHour());
|
||||
assertEquals(now.getMinute(), targetObject.getCreatedDate().get().getMinute());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void markModified() {
|
||||
Auditor auditor = Mockito.mock(Auditor.class);
|
||||
MappingContext mappingContext = Mockito.mock(MappingContext.class);
|
||||
CustomAuditingHandler testInstance = new CustomAuditingHandler(PersistentEntities.of(mappingContext));
|
||||
AbstractAuditableWithUTCDateTimeCustom targetObject = Mockito.spy(AbstractAuditableWithUTCDateTimeCustom.class);
|
||||
targetObject = testInstance.markModified(auditor, targetObject);
|
||||
OffsetDateTime now = OffsetDateTime.now(DateUtils.getDateTimeZoneOfTenant());
|
||||
|
||||
assertTrue(targetObject.getLastModifiedDate().isPresent());
|
||||
assertEquals(now.getYear(), targetObject.getLastModifiedDate().get().getYear());
|
||||
assertEquals(now.getMonth(), targetObject.getLastModifiedDate().get().getMonth());
|
||||
assertEquals(now.getDayOfMonth(), targetObject.getLastModifiedDate().get().getDayOfMonth());
|
||||
assertEquals(now.getHour(), targetObject.getLastModifiedDate().get().getHour());
|
||||
assertEquals(now.getMinute(), targetObject.getLastModifiedDate().get().getMinute());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void markModifiedOldDateTimeProvider() {
|
||||
Auditor auditor = Mockito.mock(Auditor.class);
|
||||
MappingContext mappingContext = Mockito.mock(MappingContext.class);
|
||||
CustomAuditingHandler testInstance = new CustomAuditingHandler(PersistentEntities.of(mappingContext));
|
||||
AbstractAuditableCustom targetObject = Mockito.spy(AbstractAuditableCustom.class);
|
||||
targetObject = testInstance.markModified(auditor, targetObject);
|
||||
LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault());
|
||||
|
||||
assertTrue(targetObject.getLastModifiedDate().isPresent());
|
||||
assertEquals(now.getYear(), targetObject.getLastModifiedDate().get().getYear());
|
||||
assertEquals(now.getMonth(), targetObject.getLastModifiedDate().get().getMonth());
|
||||
assertEquals(now.getDayOfMonth(), targetObject.getLastModifiedDate().get().getDayOfMonth());
|
||||
assertEquals(now.getHour(), targetObject.getLastModifiedDate().get().getHour());
|
||||
assertEquals(now.getMinute(), targetObject.getLastModifiedDate().get().getMinute());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void markCreatedOldDateTimeProvider() {
|
||||
Auditor auditor = Mockito.mock(Auditor.class);
|
||||
MappingContext mappingContext = Mockito.mock(MappingContext.class);
|
||||
CustomAuditingHandler testInstance = new CustomAuditingHandler(PersistentEntities.of(mappingContext));
|
||||
AbstractAuditableCustom targetObject = Mockito.spy(AbstractAuditableCustom.class);
|
||||
targetObject = testInstance.markCreated(auditor, targetObject);
|
||||
LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault());
|
||||
|
||||
assertTrue(targetObject.getCreatedDate().isPresent());
|
||||
assertEquals(now.getYear(), targetObject.getCreatedDate().get().getYear());
|
||||
assertEquals(now.getMonth(), targetObject.getCreatedDate().get().getMonth());
|
||||
assertEquals(now.getDayOfMonth(), targetObject.getCreatedDate().get().getDayOfMonth());
|
||||
assertEquals(now.getHour(), targetObject.getCreatedDate().get().getHour());
|
||||
assertEquals(now.getMinute(), targetObject.getCreatedDate().get().getMinute());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user