Search in sources :

Example 1 with StringHolderRepository

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");
}
Also used : Arrays(java.util.Arrays) Checkers(org.immutables.check.Checkers) TypeHolder(org.immutables.criteria.typemodel.TypeHolder) Collection(java.util.Collection) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ImmutableStringHolder(org.immutables.criteria.typemodel.ImmutableStringHolder) Collectors(java.util.stream.Collectors) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test) StringHolderRepository(org.immutables.criteria.typemodel.StringHolderRepository) List(java.util.List) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Flowable(io.reactivex.Flowable) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) TypeHolder(org.immutables.criteria.typemodel.TypeHolder) StringHolderRepository(org.immutables.criteria.typemodel.StringHolderRepository) ImmutableStringHolder(org.immutables.criteria.typemodel.ImmutableStringHolder) ImmutableStringHolder(org.immutables.criteria.typemodel.ImmutableStringHolder) BsonDocument(org.bson.BsonDocument) Collection(java.util.Collection) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Test(org.junit.jupiter.api.Test)

Aggregations

MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)1 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)1 Flowable (io.reactivex.Flowable)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 BsonDocument (org.bson.BsonDocument)1 Checkers (org.immutables.check.Checkers)1 ImmutableStringHolder (org.immutables.criteria.typemodel.ImmutableStringHolder)1 StringHolderRepository (org.immutables.criteria.typemodel.StringHolderRepository)1 TypeHolder (org.immutables.criteria.typemodel.TypeHolder)1 Test (org.junit.jupiter.api.Test)1 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)1