use of org.immutables.criteria.typemodel.StringHolderRepository in project immutables by immutables.
the class IdAttributeTest method idAttribute.
/**
* Test that {@code _id} attribute is persisted instead of {@code id}
*/
@Test
void idAttribute() {
StringHolderRepository repository = new StringHolderRepository(resource.backend());
ImmutableStringHolder holder = TypeHolder.StringHolder.generator().get().withId("id1");
repository.insertAll(Arrays.asList(holder, holder.withId("id2")));
MongoCollection<BsonDocument> collection = resource.collection(TypeHolder.StringHolder.class).withDocumentClass(BsonDocument.class);
List<BsonDocument> docs = Flowable.fromPublisher(collection.find()).toList().blockingGet();
Checkers.check(docs).hasSize(2);
// has _id attribute
Checkers.check(docs.stream().map(BsonDocument::keySet).flatMap(Collection::stream).collect(Collectors.toSet())).has("_id");
// does not have 'id' attribute only '_id' (with underscore which is mongo specific) in collected documents
Checkers.check(docs.stream().map(BsonDocument::keySet).flatMap(Collection::stream).collect(Collectors.toSet())).not().has("id");
Checkers.check(docs.stream().map(d -> d.get("_id").asString().getValue()).collect(Collectors.toList())).hasContentInAnyOrder("id1", "id2");
// using repository
Checkers.check(repository.findAll().fetch().stream().map(TypeHolder.StringHolder::id).collect(Collectors.toList())).hasContentInAnyOrder("id1", "id2");
}
Aggregations