Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix base entity with date serializer #16

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package it.gov.pagopa.pu.debtpositions.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<LocalDateTime> {

@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());
}
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package it.gov.pagopa.pu.debtpositions.model;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import it.gov.pagopa.pu.debtpositions.config.json.LocalDateTimeToOffsetDateTimeSerializer;
import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
Expand All @@ -24,8 +26,10 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package it.gov.pagopa.pu.debtpositions.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import it.gov.pagopa.pu.debtpositions.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);
}
}
Loading