Search in sources :

Example 11 with EntityFilters

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;
}
Also used : EntityFilters(io.spine.client.EntityFilters)

Example 12 with EntityFilters

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;
}
Also used : Entity(io.spine.server.entity.Entity) Target(io.spine.client.Target) Message(com.google.protobuf.Message) ImmutableList(com.google.common.collect.ImmutableList) EntityFilters(io.spine.client.EntityFilters) Any(com.google.protobuf.Any) FieldMask(com.google.protobuf.FieldMask)

Example 13 with EntityFilters

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);
}
Also used : StandTestProjection(io.spine.server.stand.Given.StandTestProjection) EntityFilters(io.spine.client.EntityFilters) ProjectId(io.spine.test.projection.ProjectId) FieldMask(com.google.protobuf.FieldMask)

Example 14 with EntityFilters

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);
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) EntityId(io.spine.client.EntityId) EntityIdFilter(io.spine.client.EntityIdFilter) EntityFilters(io.spine.client.EntityFilters) Identifiers.idToAny(io.spine.base.Identifiers.idToAny) Any(com.google.protobuf.Any) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) Test(org.junit.Test)

Example 15 with EntityFilters

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);
}
Also used : TestEntity(io.spine.test.client.TestEntity) Query(io.spine.client.Query) EntityIdFilter(io.spine.client.EntityIdFilter) Message(com.google.protobuf.Message) EntityFilters(io.spine.client.EntityFilters) Matchers.containsString(org.hamcrest.Matchers.containsString) Any(com.google.protobuf.Any) EntityId(io.spine.client.EntityId) Target(io.spine.client.Target) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Aggregations

EntityFilters (io.spine.client.EntityFilters)18 Test (org.junit.Test)11 Any (com.google.protobuf.Any)7 Message (com.google.protobuf.Message)7 EntityId (io.spine.client.EntityId)7 EntityIdFilter (io.spine.client.EntityIdFilter)6 Target (io.spine.client.Target)6 Query (io.spine.client.Query)5 FieldMask (com.google.protobuf.FieldMask)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 EntityRecord (io.spine.server.entity.EntityRecord)3 Int32Value (com.google.protobuf.Int32Value)2 Version (io.spine.base.Version)2 EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)2 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)2 ProjectId (io.spine.test.projection.ProjectId)2 ImmutableList (com.google.common.collect.ImmutableList)1 BoolValue (com.google.protobuf.BoolValue)1 Descriptors (com.google.protobuf.Descriptors)1 StringValue (com.google.protobuf.StringValue)1