use of org.bson.BsonDateTime in project immutables by immutables.
the class JavaTimeTest method localDate.
@Test
public void localDate() throws IOException {
long epoch = System.currentTimeMillis();
LocalDate now = Instant.ofEpochMilli(epoch).atOffset(ZoneOffset.UTC).toLocalDate();
TypeAdapter<LocalDate> adapter = gson.getAdapter(LocalDate.class);
// read
LocalDate 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()).atOffset(ZoneOffset.UTC).toLocalDate()).is(now);
}
use of org.bson.BsonDateTime in project immutables by immutables.
the class TypeConversionTest method dateTime.
@Test
void dateTime() throws IOException {
final long epoch = System.currentTimeMillis();
final BsonDateTime value = new BsonDateTime(epoch);
check(Parsers.parserAt(value).getIntValue()).is((int) epoch);
check(Parsers.parserAt(value).getLongValue()).is(epoch);
check(Parsers.parserAt(value).getDoubleValue()).is((double) epoch);
check(Parsers.parserAt(value).getDecimalValue()).is(BigDecimal.valueOf(epoch));
check(Parsers.parserAt(value).getBigIntegerValue()).is(BigInteger.valueOf(epoch));
check(Parsers.parserAt(value).getText()).is(Long.toString(epoch));
}
use of org.bson.BsonDateTime in project immutables by immutables.
the class TupleCodecProviderTest method localDate.
@Test
void localDate() {
LocalDateHolderCriteria criteria = LocalDateHolderCriteria.localDateHolder;
Query query = Query.of(TypeHolder.LocalDateHolder.class).addProjections(Matchers.toExpression(criteria.value), Matchers.toExpression(criteria.nullable), Matchers.toExpression(criteria.optional));
Path idPath = Visitors.toPath(KeyExtractor.defaultFactory().create(TypeHolder.LocalDateHolder.class).metadata().keys().get(0));
TupleCodecProvider provider = new TupleCodecProvider(query, new MongoPathNaming(idPath, PathNaming.defaultNaming()).toExpression());
Codec<ProjectedTuple> codec = provider.get(ProjectedTuple.class, registry);
LocalDate now = LocalDate.now();
final long millisEpoch = now.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli();
BsonDocument doc = new BsonDocument().append("id", new BsonString("id1")).append("value", new BsonDateTime(millisEpoch)).append("nullable", BsonNull.VALUE).append("optional", BsonNull.VALUE).append("array", new BsonArray()).append("list", new BsonArray());
ProjectedTuple tuple = codec.decode(new BsonDocumentReader(doc), DecoderContext.builder().build());
check(tuple.get(Matchers.toExpression(criteria.value))).is(now);
check(tuple.get(Matchers.toExpression(criteria.nullable))).isNull();
check(tuple.get(Matchers.toExpression(criteria.optional))).is(Optional.empty());
}
use of org.bson.BsonDateTime in project immutables by immutables.
the class JavaTimeTypeTest method localDate.
@Test
void localDate() {
LocalDateHolderRepository repository = new LocalDateHolderRepository(backend);
LocalDate value = LocalDate.now();
ImmutableLocalDateHolder holder = TypeHolder.LocalDateHolder.generator().get().withValue(value).withOptional(value).withNullable(null);
repository.insert(holder);
BsonDocument doc = fetch();
BsonDateTime expected = new BsonDateTime(value.atStartOfDay(ZoneOffset.UTC).toInstant().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 mongo-java-driver by mongodb.
the class IdHoldingBsonWriter method writeDateTime.
@Override
public void writeDateTime(final long value) {
addBsonValue(() -> new BsonDateTime(value), () -> getIdBsonWriter().writeDateTime(value));
super.writeDateTime(value);
}
Aggregations