-
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.
- Loading branch information
1 parent
0f787ba
commit 8b660c1
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.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,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()); | ||
} | ||
} |
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
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