use of io.spine.client.EntityId in project core-java by SpineEventEngine.
the class RecordBasedRepositoryShould method retrieve_all_records_with_entity_filters_and_field_mask_applied.
@SuppressWarnings("MethodWithMultipleLoops")
@Test
public void retrieve_all_records_with_entity_filters_and_field_mask_applied() {
final int count = 10;
final List<E> entities = createAndStoreEntities(repository, count);
final List<EntityId> ids = Lists.newLinkedList();
// Find some of the records (half of them in this case)
for (int i = 0; i < count / 2; i++) {
final Message entityId = (Message) entities.get(i).getId();
final EntityId id = EntityId.newBuilder().setId(pack(entityId)).build();
ids.add(id);
}
final EntityIdFilter filter = EntityIdFilter.newBuilder().addAllIds(ids).build();
final EntityFilters filters = EntityFilters.newBuilder().setIdFilter(filter).build();
final Descriptors.Descriptor entityDescriptor = entities.get(0).getState().getDescriptorForType();
final FieldMask firstFieldOnly = FieldMasks.maskOf(entityDescriptor, 1);
final Iterable<E> readEntities = find(filters, firstFieldOnly);
assertSize(ids.size(), readEntities);
for (E entity : readEntities) {
assertMatches(entity, firstFieldOnly);
}
}
use of io.spine.client.EntityId in project core-java by SpineEventEngine.
the class EntityQueries method toGenericIdValues.
private static <I> Collection<I> toGenericIdValues(EntityFilters entityFilters) {
final EntityIdFilter idFilter = entityFilters.getIdFilter();
final Collection<I> ids = new LinkedList<>();
for (EntityId entityId : idFilter.getIdsList()) {
final Any wrappedMessageId = entityId.getId();
// Checked at runtime
@SuppressWarnings("unchecked") final I genericId = (I) Identifiers.idFromAny(wrappedMessageId);
ids.add(genericId);
}
return ids;
}
use of io.spine.client.EntityId in project core-java by SpineEventEngine.
the class RecordStorageShould method allow_by_single_id_queries_with_no_columns.
@Test
public void allow_by_single_id_queries_with_no_columns() {
// Create the test data
final I idMatching = newId();
final I idWrong1 = newId();
final I idWrong2 = newId();
final TestCounterEntity<I> matchingEntity = new TestCounterEntity<>(idMatching);
final TestCounterEntity<I> wrongEntity1 = new TestCounterEntity<>(idWrong1);
final TestCounterEntity<I> wrongEntity2 = new TestCounterEntity<>(idWrong2);
final EntityRecord fineRecord = newStorageRecord(idMatching, newState(idMatching));
final EntityRecord notFineRecord1 = newStorageRecord(idWrong1, newState(idWrong1));
final EntityRecord notFineRecord2 = newStorageRecord(idWrong2, newState(idWrong2));
final EntityRecordWithColumns recordRight = create(fineRecord, matchingEntity);
final EntityRecordWithColumns recordWrong1 = create(notFineRecord1, wrongEntity1);
final EntityRecordWithColumns recordWrong2 = create(notFineRecord2, wrongEntity2);
final RecordStorage<I> storage = getStorage();
// Fill the storage
storage.write(idWrong1, recordWrong1);
storage.write(idMatching, recordRight);
storage.write(idWrong2, recordWrong2);
// Prepare the query
final Any matchingIdPacked = TypeConverter.toAny(idMatching);
final EntityId entityId = EntityId.newBuilder().setId(matchingIdPacked).build();
final EntityIdFilter idFilter = EntityIdFilter.newBuilder().addIds(entityId).build();
final EntityFilters filters = EntityFilters.newBuilder().setIdFilter(idFilter).build();
final EntityQuery<I> query = EntityQueries.from(filters, TestCounterEntity.class);
// Perform the query
final Map<I, EntityRecord> readRecords = storage.readAll(query, FieldMask.getDefaultInstance());
// Check results
Verify.assertSize(1, readRecords);
final EntityRecord actualRecord = readRecords.get(idMatching);
assertEquals(fineRecord, actualRecord);
}
use of io.spine.client.EntityId in project core-java by SpineEventEngine.
the class QueryBuilderShould method create_queries_with_all_arguments.
@SuppressWarnings("OverlyLongMethod")
// A big test case covering the query arguments co-living
@Test
public void create_queries_with_all_arguments() {
final Class<? extends Message> testEntityClass = TestEntity.class;
final int id1 = 314;
final int id2 = 271;
final String columnName1 = "column1";
final Object columnValue1 = 42;
final String columnName2 = "column2";
final Object columnValue2 = newMessageId();
final String fieldName = "TestEntity.secondField";
final Query query = factory().query().select(testEntityClass).withMask(fieldName).byId(id1, id2).where(eq(columnName1, columnValue1), eq(columnName2, columnValue2)).build();
assertNotNull(query);
// Check FieldMask
final FieldMask mask = query.getFieldMask();
final Collection<String> fieldNames = mask.getPathsList();
assertSize(1, fieldNames);
assertContains(fieldName, fieldNames);
final Target target = query.getTarget();
assertFalse(target.getIncludeAll());
final EntityFilters entityFilters = target.getFilters();
// Check IDs
final EntityIdFilter idFilter = entityFilters.getIdFilter();
final Collection<EntityId> idValues = idFilter.getIdsList();
final Function<EntityId, Integer> transformer = new EntityIdUnpacker<>(int.class);
final Collection<Integer> intIdValues = transform(idValues, transformer);
assertSize(2, idValues);
assertThat(intIdValues, containsInAnyOrder(id1, id2));
// Check query params
final Map<String, Any> columnFilters = entityFilters.getColumnFilterMap();
assertSize(2, columnFilters);
final Any actualValue1 = columnFilters.get(columnName1);
assertNotNull(actualValue1);
final int actualGenericValue1 = TypeConverter.toObject(actualValue1, int.class);
assertEquals(columnValue1, actualGenericValue1);
final Any actualValue2 = columnFilters.get(columnName2);
assertNotNull(actualValue2);
final Message actualGenericValue2 = TypeConverter.toObject(actualValue2, ProjectId.class);
assertEquals(columnValue2, actualGenericValue2);
}
use of io.spine.client.EntityId in project core-java by SpineEventEngine.
the class QueryBuilderShould method persist_only_last_ids_clause.
@Test
public void persist_only_last_ids_clause() {
final Iterable<?> genericIds = asList(newUuid(), -1, newMessageId());
final Long[] longIds = { 1L, 2L, 3L };
final Message[] messageIds = { newMessageId(), newMessageId(), newMessageId() };
final String[] stringIds = { newUuid(), newUuid(), newUuid() };
final Integer[] intIds = { 4, 5, 6 };
final Query query = factory().query().select(TestEntity.class).byId(genericIds).byId(longIds).byId(stringIds).byId(intIds).byId(messageIds).build();
assertNotNull(query);
final Target target = query.getTarget();
final EntityFilters filters = target.getFilters();
final Collection<EntityId> entityIds = filters.getIdFilter().getIdsList();
assertSize(messageIds.length, entityIds);
final Function<EntityId, ProjectId> transformer = new EntityIdUnpacker<>(ProjectId.class);
final Iterable<? extends Message> actualValues = transform(entityIds, transformer);
assertThat(actualValues, containsInAnyOrder(messageIds));
}
Aggregations