Skip to content

Commit

Permalink
fix: Fix base entity with date serializer (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benedetta-fabbri authored Jan 16, 2025
2 parents 314c933 + 7466289 commit 4b7a19f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
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);
}
}

0 comments on commit 4b7a19f

Please sign in to comment.