generated from pagopa/template-payments-java-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix base entity with date serializer (#16)
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
...a/it/gov/pagopa/pu/debtpositions/config/json/LocalDateTimeToOffsetDateTimeSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...va/it/gov/pagopa/pu/debtpositions/config/LocalDateTimeToOffsetDateTimeSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |