use of org.bson.BsonDateTime in project immutables by immutables.
the class JavaTimeTypeTest method localDateTime.
@Test
void localDateTime() {
LocalDateTimeHolderRepository repository = new LocalDateTimeHolderRepository(backend);
LocalDateTime value = LocalDateTime.now();
ImmutableLocalDateTimeHolder holder = TypeHolder.LocalDateTimeHolder.generator().get().withValue(value).withOptional(value).withNullable(null);
repository.insert(holder);
BsonDocument doc = fetch();
BsonDateTime expected = new BsonDateTime(value.toInstant(ZoneOffset.UTC).toEpochMilli());
check(doc.get("value")).is(expected);
check(doc.get("optional")).is(expected);
if (doc.containsKey("nullable")) {
check(doc.get("nullable")).is(BsonNull.VALUE);
}
}
use of org.bson.BsonDateTime in project immutables by immutables.
the class JavaTimeTest method javaUtilDate.
@Test
public void javaUtilDate() throws IOException {
Date now = new Date();
long epoch = now.getTime();
TypeAdapter<Date> adapter = GsonCodecs.typeAdapterFromCodec(new DateCodec());
// read
Date date = adapter.read(Jsons.readerAt(new BsonDateTime(epoch)));
check(date).is(now);
// write
BsonValue bson = writeAndReadBson(now);
check(bson.getBsonType()).is(BsonType.DATE_TIME);
check(new Date(bson.asDateTime().getValue())).is(now);
}
use of org.bson.BsonDateTime in project immutables by immutables.
the class JavaTimeTest method localDateTime.
@Test
public void localDateTime() throws IOException {
LocalDateTime now = LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS);
long epoch = now.toInstant(ZoneOffset.UTC).toEpochMilli();
TypeAdapter<LocalDateTime> adapter = gson.getAdapter(LocalDateTime.class);
// read
LocalDateTime date = adapter.read(Jsons.readerAt(new BsonDateTime(epoch)));
LocalDateTime valueRead = Instant.ofEpochMilli(epoch).atOffset(ZoneOffset.UTC).toLocalDateTime();
check(date).is(now);
// write
BsonValue bson = writeAndReadBson(valueRead);
check(bson.getBsonType()).is(BsonType.DATE_TIME);
check(Instant.ofEpochMilli(bson.asDateTime().getValue()).atOffset(ZoneOffset.UTC).toLocalDateTime()).is(valueRead);
}
use of org.bson.BsonDateTime in project immutables by immutables.
the class TypeConversionTest method dateTime.
@Test
public void dateTime() throws IOException {
final long epoch = System.currentTimeMillis();
BsonDateTime value = new BsonDateTime(epoch);
check(Jsons.readerAt(value).peek()).is(JsonToken.NUMBER);
check(Jsons.readerAt(value).nextInt()).is((int) epoch);
check(Jsons.readerAt(value).nextLong()).is(epoch);
check(Jsons.readerAt(value).nextDouble()).is((double) epoch);
}
use of org.bson.BsonDateTime in project immutables by immutables.
the class JavaTimeTest method instant.
@Test
public void instant() throws IOException {
Instant now = Instant.now().truncatedTo(ChronoUnit.MILLIS);
long epoch = now.toEpochMilli();
TypeAdapter<Instant> adapter = gson.getAdapter(Instant.class);
// read
Instant date = adapter.read(Jsons.readerAt(new BsonDateTime(epoch)));
check(date).is(now);
// write
BsonValue bson = writeAndReadBson(now);
check(bson.getBsonType()).is(BsonType.DATE_TIME);
check(Instant.ofEpochMilli(bson.asDateTime().getValue())).is(now);
}
Aggregations