use of io.spine.client.EntityFilters in project core-java by SpineEventEngine.
the class AggregateQueryProcessor method doFetchWithFilters.
private ImmutableCollection<EntityRecord> doFetchWithFilters(Target target, FieldMask fieldMask) {
final EntityFilters filters = target.getFilters();
final boolean idsAreDefined = !filters.getIdFilter().getIdsList().isEmpty();
if (!idsAreDefined) {
return ImmutableList.of();
}
final EntityIdFilter idFilter = filters.getIdFilter();
final Collection<AggregateStateId> stateIds = Collections2.transform(idFilter.getIdsList(), stateIdTransformer);
final ImmutableCollection<EntityRecord> result = stateIds.size() == 1 ? readOne(stateIds.iterator().next(), fieldMask) : readMany(stateIds, fieldMask);
return result;
}
use of io.spine.client.EntityFilters in project core-java by SpineEventEngine.
the class RecordStorageShould method filter_records_by_columns.
// Complex test case (still tests a single operation)
@SuppressWarnings("OverlyLongMethod")
@Test
public void filter_records_by_columns() {
final Project.Status requiredValue = Project.Status.DONE;
final Int32Value wrappedValue = Int32Value.newBuilder().setValue(requiredValue.getNumber()).build();
final Version versionValue = Version.newBuilder().setNumber(// Value of the counter after one columns
2).build();
final EntityFilters filters = EntityFilters.newBuilder().putColumnFilter("projectStatusValue", AnyPacker.pack(wrappedValue)).putColumnFilter("counterVersion", AnyPacker.pack(versionValue)).build();
final EntityQuery<I> query = EntityQueries.from(filters, TestCounterEntity.class);
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);
// 2 of 3 have required values
matchingEntity.setStatus(requiredValue);
wrongEntity1.setStatus(requiredValue);
wrongEntity2.setStatus(Project.Status.CANCELLED);
// Change internal Entity state
wrongEntity1.getCounter();
// After the mutation above the single matching record is the one under the `idMatching` ID
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();
storage.write(idMatching, recordRight);
storage.write(idWrong1, recordWrong1);
storage.write(idWrong2, recordWrong2);
final Map<I, EntityRecord> readRecords = storage.readAll(query, FieldMask.getDefaultInstance());
Verify.assertSize(1, readRecords);
final I singleId = readRecords.keySet().iterator().next();
assertEquals(idMatching, singleId);
final EntityRecord singleRecord = readRecords.values().iterator().next();
assertEquals(fineRecord, singleRecord);
}
use of io.spine.client.EntityFilters in project core-java by SpineEventEngine.
the class StandShould method entityFilterMatcher.
@SuppressWarnings("OverlyComplexAnonymousInnerClass")
private static ArgumentMatcher<EntityFilters> entityFilterMatcher(final Collection<ProjectId> projectIds) {
// ALL the expected IDs.
return new ArgumentMatcher<EntityFilters>() {
@Override
public boolean matches(EntityFilters argument) {
boolean everyElementPresent = true;
for (EntityId entityId : argument.getIdFilter().getIdsList()) {
final Any idAsAny = entityId.getId();
final Message rawId = AnyPacker.unpack(idAsAny);
if (rawId instanceof ProjectId) {
final ProjectId convertedProjectId = (ProjectId) rawId;
everyElementPresent = everyElementPresent && projectIds.contains(convertedProjectId);
} else {
everyElementPresent = false;
}
}
return everyElementPresent;
}
};
}
use of io.spine.client.EntityFilters in project core-java by SpineEventEngine.
the class RecordBasedRepositoryShould method find_entities_by_query.
@Test
public void find_entities_by_query() {
final I id1 = createId(271);
final I id2 = createId(314);
final Class<E> entityClass = repository.getEntityClass();
final E entity1 = Given.entityOfClass(entityClass).withId(id1).build();
final E entity2 = Given.entityOfClass(entityClass).withId(id2).build();
repository.store(entity1);
repository.store(entity2);
final String fieldPath = "idString";
final StringValue fieldValue = StringValue.newBuilder().setValue(id1.toString()).build();
final EntityFilters filters = EntityFilters.newBuilder().putColumnFilter(fieldPath, AnyPacker.pack(fieldValue)).build();
final Collection<E> found = repository.find(filters, FieldMask.getDefaultInstance());
assertSize(1, found);
assertContains(entity1, found);
assertNotContains(entity2, found);
}
use of io.spine.client.EntityFilters 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);
}
}
Aggregations