diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java new file mode 100644 index 00000000..25dbe9a6 --- /dev/null +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java @@ -0,0 +1,64 @@ +package it.pagopa.selfcare.user.conf; + +import org.bson.BsonReader; +import org.bson.BsonWriter; +import org.bson.codecs.DecoderContext; +import org.bson.codecs.EncoderContext; +import org.bson.json.JsonReader; +import org.bson.json.JsonWriter; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.StringWriter; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; + +import static org.junit.jupiter.api.Assertions.*; + +class DateCodecTest { + + private final DateCodec dateCodec = new DateCodec(); + + @Test + void decode_withValidDateTime_shouldReturnOffsetDateTime() { + LocalDateTime localDateTime = LocalDateTime.of(2021, 12, 1, 0, 0, 0); + String epoch = String.valueOf(localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli()); + BsonReader reader = new JsonReader("{\"date\": {\"$date\":" + epoch + "}}"); + reader.readStartDocument(); + reader.readName("date"); + + OffsetDateTime result = dateCodec.decode(reader, DecoderContext.builder().build()); + + OffsetDateTime expected = OffsetDateTime.of(2021, 12, 1, 0, 0, 0, 0, ZoneOffset.UTC); + OffsetDateTime desiredOffsetDateTime = expected.withOffsetSameInstant(ZoneOffset.of("+01:00")); + + assertEquals(desiredOffsetDateTime, result); + } + + @Test + void decode_withInvalidType_shouldReturnNull() { + BsonReader reader = new JsonReader("{\"date\": \"invalid\"}"); + reader.readStartDocument(); + reader.readName("date"); + + OffsetDateTime result = dateCodec.decode(reader, DecoderContext.builder().build()); + + assertNull(result); + } + + @Test + void encode_withValidOffsetDateTime_shouldWriteDateTime() { + OffsetDateTime dateTime = OffsetDateTime.of(2021, 12, 1, 0, 0, 0, 0, ZoneOffset.UTC); + StringWriter stringWriter = new StringWriter(); + BsonWriter writer = new JsonWriter(stringWriter); + + Assertions.assertDoesNotThrow(() -> dateCodec.encode(writer, dateTime, EncoderContext.builder().build())); + } + + @Test + void getEncoderClass_shouldReturnOffsetDateTimeClass() { + assertEquals(OffsetDateTime.class, dateCodec.getEncoderClass()); + } +} \ No newline at end of file diff --git a/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserNotificationToSend.java b/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserNotificationToSend.java index 741dc454..cfa5409e 100644 --- a/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserNotificationToSend.java +++ b/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserNotificationToSend.java @@ -6,6 +6,7 @@ import java.time.OffsetDateTime; @Data +@SuppressWarnings("java:S1068") public class UserNotificationToSend { private String id; diff --git a/libs/user-sdk-model/src/main/java/it.pagopa.selfcare.user.model/OnboardedProduct.java b/libs/user-sdk-model/src/main/java/it.pagopa.selfcare.user.model/OnboardedProduct.java index a5915fc4..52c77fd8 100644 --- a/libs/user-sdk-model/src/main/java/it.pagopa.selfcare.user.model/OnboardedProduct.java +++ b/libs/user-sdk-model/src/main/java/it.pagopa.selfcare.user.model/OnboardedProduct.java @@ -12,6 +12,7 @@ @Data +@SuppressWarnings("java:S1068") @FieldNameConstants(asEnum = true) public class OnboardedProduct {