EntityQueryProcessor implements QueryProcessor { @Override public ImmutableCollection<EntityStateWithVersion> process(Query query) { Iterator<EntityRecord> entities = query.all() ? loadAll(query.responseFormat()) : loadByQuery(query); ImmutableList<EntityStateWithVersion> result = stream(entities) .map(EntityQueryProcessor::toEntityState) .collect(toImmutableList()); return result; } EntityQueryProcessor(
            RecordBasedRepository<?, ? extends Entity, ? extends EntityState> repository); @Override ImmutableCollection<EntityStateWithVersion> process(Query query);  }
@Test @DisplayName("read all entities") void readAll() { Query query = queries.all(Menu.class); ImmutableCollection<EntityStateWithVersion> records = processor.process(query); assertThat(records).hasSize(MENU_COUNT); }
@Test @DisplayName("read one by ID") void readById() { DishAdded event = addDish(); MenuId id = event.getId(); Query query = queries.byIds(Menu.class, ImmutableSet.of(id)); ImmutableCollection<EntityStateWithVersion> records = processor.process(query); assertThat(records).hasSize(1); EntityStateWithVersion record = records.asList().get(0); Menu menu = state(record); assertThat(menu.getId()).isEqualTo(id); assertThat(menu.getDishList()).containsExactly(event.getDish()); }
@Test @DisplayName("read all entities and sort") void readAllAndSort() { int limit = MENU_COUNT - 4; Query query = queries.select(Menu.class) .orderBy(UUID_COLUMN, DESCENDING) .limit(limit) .build(); ImmutableCollection<EntityStateWithVersion> records = processor.process(query); IterableSubject assertRecords = assertThat(records); assertRecords.hasSize(limit); Comparator<EntityStateWithVersion> uuidOrder = comparing( (EntityStateWithVersion record) -> state(record).getId().getUuid() ); assertRecords.isInOrder(uuidOrder.reversed()); }
@Test @DisplayName("fail if the query does not specify filters and `include_all` is not set") void failOnInvalidQuery() { Target target = Target .newBuilder() .setType(TypeName.of(Mirror.class).value()) .buildPartial(); Query query = Query .newBuilder() .setId(QueryId.newBuilder().setValue(newUuid())) .setContext(factory.newActorContext()) .setTarget(target) .buildPartial(); assertThrows(ValidationException.class, () -> processor.process(query)); }