use of io.spine.client.EntityFilters in project core-java by SpineEventEngine.
the class SubscriptionRecord method matches.
/**
* Checks whether this record matches the given parameters.
*
* @param type the type to match
* @param id the ID to match
* @param entityState the entity state to match
* @return {@code true} if this record matches all the given parameters,
* {@code false} otherwise.
*/
boolean matches(TypeUrl type, Object id, // entityState will be later used for more advanced filtering
@SuppressWarnings("UnusedParameters") Any entityState) {
final boolean result;
final boolean typeMatches = this.type.equals(type);
if (typeMatches) {
final boolean includeAll = target.getIncludeAll();
final EntityFilters filters = target.getFilters();
result = includeAll || matchByFilters(id, filters);
} else {
result = false;
}
return result;
}
use of io.spine.client.EntityFilters in project core-java by SpineEventEngine.
the class EntityQueryProcessor method process.
@Override
public ImmutableCollection<Any> process(Query query) {
final ImmutableList.Builder<Any> resultBuilder = ImmutableList.builder();
final Target target = query.getTarget();
final FieldMask fieldMask = query.getFieldMask();
final ImmutableCollection<? extends Entity> entities;
if (target.getIncludeAll() && fieldMask.getPathsList().isEmpty()) {
entities = repository.loadAll();
} else {
final EntityFilters filters = target.getFilters();
entities = repository.find(filters, fieldMask);
}
for (Entity entity : entities) {
final Message state = entity.getState();
final Any packedState = AnyPacker.pack(state);
resultBuilder.add(packedState);
}
final ImmutableList<Any> result = resultBuilder.build();
return result;
}
use of io.spine.client.EntityFilters in project core-java by SpineEventEngine.
the class StandShould method setupExpectedFindAllBehaviour.
@SuppressWarnings("ConstantConditions")
private static void setupExpectedFindAllBehaviour(Map<ProjectId, Project> sampleProjects, StandTestProjectionRepository projectionRepository) {
final Set<ProjectId> projectIds = sampleProjects.keySet();
final ImmutableCollection<Given.StandTestProjection> allResults = toProjectionCollection(projectIds);
for (ProjectId projectId : projectIds) {
when(projectionRepository.find(eq(projectId))).thenReturn(Optional.of(new StandTestProjection(projectId)));
}
final Iterable<ProjectId> matchingIds = argThat(projectionIdsIterableMatcher(projectIds));
when(projectionRepository.loadAll(matchingIds, any(FieldMask.class))).thenReturn(allResults);
when(projectionRepository.loadAll()).thenReturn(allResults);
final EntityFilters matchingFilter = argThat(entityFilterMatcher(projectIds));
when(projectionRepository.find(matchingFilter, any(FieldMask.class))).thenReturn(allResults);
}
use of io.spine.client.EntityFilters 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.EntityFilters 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);
}
Aggregations