diff --git a/src/main/java/it/gov/pagopa/pu/organization/config/json/LocalDateTimeToOffsetDateTimeSerializer.java b/src/main/java/it/gov/pagopa/pu/organization/config/json/LocalDateTimeToOffsetDateTimeSerializer.java new file mode 100644 index 0000000..88cfad6 --- /dev/null +++ b/src/main/java/it/gov/pagopa/pu/organization/config/json/LocalDateTimeToOffsetDateTimeSerializer.java @@ -0,0 +1,24 @@ +package it.gov.pagopa.pu.organization.config.json; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import org.springframework.context.annotation.Configuration; + +import java.io.IOException; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneId; + +@Configuration +public class LocalDateTimeToOffsetDateTimeSerializer extends JsonSerializer { + + @Override + public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + if (value != null) { + OffsetDateTime offsetDateTime = value.atZone(ZoneId.systemDefault()).toOffsetDateTime(); + gen.writeString(offsetDateTime.toString()); + } + } +} + diff --git a/src/main/java/it/gov/pagopa/pu/organization/model/BaseEntity.java b/src/main/java/it/gov/pagopa/pu/organization/model/BaseEntity.java index 5322da7..1211fa4 100644 --- a/src/main/java/it/gov/pagopa/pu/organization/model/BaseEntity.java +++ b/src/main/java/it/gov/pagopa/pu/organization/model/BaseEntity.java @@ -1,5 +1,7 @@ package it.gov.pagopa.pu.organization.model; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import it.gov.pagopa.pu.organization.config.json.LocalDateTimeToOffsetDateTimeSerializer; import jakarta.persistence.Column; import jakarta.persistence.EntityListeners; import jakarta.persistence.MappedSuperclass; @@ -24,9 +26,14 @@ public abstract class BaseEntity implements Serializable { @Column(updatable = false) @CreatedDate + @JsonSerialize(using = LocalDateTimeToOffsetDateTimeSerializer.class) private LocalDateTime creationDate; @LastModifiedDate + @JsonSerialize(using = LocalDateTimeToOffsetDateTimeSerializer.class) private LocalDateTime updateDate; @LastModifiedBy private String updateOperatorExternalId; + } + + diff --git a/src/test/java/it/gov/pagopa/pu/organization/config/LocalDateTimeToOffsetDateTimeSerializerTest.java b/src/test/java/it/gov/pagopa/pu/organization/config/LocalDateTimeToOffsetDateTimeSerializerTest.java new file mode 100644 index 0000000..6e0c644 --- /dev/null +++ b/src/test/java/it/gov/pagopa/pu/organization/config/LocalDateTimeToOffsetDateTimeSerializerTest.java @@ -0,0 +1,52 @@ +package it.gov.pagopa.pu.organization.config; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import it.gov.pagopa.pu.organization.config.json.LocalDateTimeToOffsetDateTimeSerializer; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.IOException; +import java.time.LocalDateTime; +import java.util.TimeZone; + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; + +@ExtendWith(MockitoExtension.class) +class LocalDateTimeToOffsetDateTimeSerializerTest { + + @Mock + private JsonGenerator jsonGenerator; + + @Mock + private SerializerProvider serializerProvider; + + private LocalDateTimeToOffsetDateTimeSerializer dateTimeSerializer; + + @BeforeEach + public void setUp() { + dateTimeSerializer = new LocalDateTimeToOffsetDateTimeSerializer(); + } + + @Test + void testDateSerializer() throws IOException { + LocalDateTime localDateTime = LocalDateTime.of(2025, 1, 16, 9, 15, 20); + + TimeZone.setDefault(TimeZone.getTimeZone("Europe/Rome")); + + dateTimeSerializer.serialize(localDateTime, jsonGenerator, serializerProvider); + + verify(jsonGenerator).writeString("2025-01-16T09:15:20+01:00"); + } + + @Test + void testNullDateSerializer() throws IOException { + dateTimeSerializer.serialize(null, jsonGenerator, serializerProvider); + + verifyNoInteractions(jsonGenerator); + } +}