Search in sources :

Example 6 with EntityIdFilter

use of io.spine.client.EntityIdFilter in project core-java by SpineEventEngine.

the class EntityQueriesShould method construct_non_empty_queries.

@Test
public void construct_non_empty_queries() {
    final Message someGenericId = Sample.messageOfType(ProjectId.class);
    final Any someId = AnyPacker.pack(someGenericId);
    final EntityId entityId = EntityId.newBuilder().setId(someId).build();
    final EntityIdFilter idFilter = EntityIdFilter.newBuilder().addIds(entityId).build();
    final Version versionValue = Version.newBuilder().setNumber(1).build();
    final BoolValue archivedValue = BoolValue.newBuilder().setValue(true).build();
    final ColumnFilter versionFilter = ColumnFilters.eq(version.name(), versionValue);
    final ColumnFilter archivedFilter = ColumnFilters.eq(archived.name(), archivedValue);
    final CompositeColumnFilter aggregatingFilter = CompositeColumnFilter.newBuilder().addFilter(versionFilter).addFilter(archivedFilter).setOperator(EITHER).build();
    final EntityFilters filters = EntityFilters.newBuilder().setIdFilter(idFilter).addFilter(aggregatingFilter).build();
    final EntityQuery<?> query = createEntityQuery(filters, AbstractVersionableEntity.class);
    assertNotNull(query);
    final Collection<?> ids = query.getIds();
    assertFalse(ids.isEmpty());
    assertSize(1, ids);
    final Object singleId = ids.iterator().next();
    assertEquals(someGenericId, singleId);
    final List<CompositeQueryParameter> values = newArrayList(query.getParameters());
    assertSize(1, values);
    final CompositeQueryParameter singleParam = values.get(0);
    final Collection<ColumnFilter> columnFilters = singleParam.getFilters().values();
    assertEquals(EITHER, singleParam.getOperator());
    assertContains(versionFilter, columnFilters);
    assertContains(archivedFilter, columnFilters);
}
Also used : CompositeColumnFilter(io.spine.client.CompositeColumnFilter) Message(com.google.protobuf.Message) EntityIdFilter(io.spine.client.EntityIdFilter) EntityFilters(io.spine.client.EntityFilters) BoolValue(com.google.protobuf.BoolValue) ColumnFilter(io.spine.client.ColumnFilter) CompositeColumnFilter(io.spine.client.CompositeColumnFilter) Any(com.google.protobuf.Any) EntityId(io.spine.client.EntityId) Version(io.spine.core.Version) Test(org.junit.Test)

Example 7 with EntityIdFilter

use of io.spine.client.EntityIdFilter 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();
        final I genericId = Identifier.unpack(wrappedMessageId);
        ids.add(genericId);
    }
    return ids;
}
Also used : EntityId(io.spine.client.EntityId) EntityIdFilter(io.spine.client.EntityIdFilter) Any(com.google.protobuf.Any) LinkedList(java.util.LinkedList)

Example 8 with EntityIdFilter

use of io.spine.client.EntityIdFilter in project core-java by SpineEventEngine.

the class RecordStorageShould method filter_archived_or_deleted_records_on_by_ID_bulk_read.

@Test
public void filter_archived_or_deleted_records_on_by_ID_bulk_read() {
    final I activeId = newId();
    final I archivedId = newId();
    final I deletedId = newId();
    final TestCounterEntity<I> activeEntity = new TestCounterEntity<>(activeId);
    final TestCounterEntity<I> archivedEntity = new TestCounterEntity<>(archivedId);
    archivedEntity.archive();
    final TestCounterEntity<I> deletedEntity = new TestCounterEntity<>(deletedId);
    deletedEntity.delete();
    final EntityRecord activeRecord = newStorageRecord(activeId, activeEntity.getState());
    final EntityRecord archivedRecord = newStorageRecord(archivedId, archivedEntity.getState());
    final EntityRecord deletedRecord = newStorageRecord(deletedId, deletedEntity.getState());
    final RecordStorage<I> storage = getStorage();
    storage.write(deletedId, create(deletedRecord, deletedEntity, storage));
    storage.write(activeId, create(activeRecord, activeEntity, storage));
    storage.write(archivedId, create(archivedRecord, archivedEntity, storage));
    final EntityIdFilter idFilter = EntityIdFilter.newBuilder().addIds(toEntityId(activeId)).addIds(toEntityId(archivedId)).addIds(toEntityId(deletedId)).build();
    final EntityFilters filters = EntityFilters.newBuilder().setIdFilter(idFilter).build();
    final EntityQuery<I> query = EntityQueries.<I>from(filters, storage).withLifecycleFlags(storage);
    final Iterator<EntityRecord> read = storage.readAll(query, FieldMask.getDefaultInstance());
    assertSingleRecord(activeRecord, read);
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) EntityIdFilter(io.spine.client.EntityIdFilter) EntityFilters(io.spine.client.EntityFilters) Test(org.junit.Test)

Example 9 with EntityIdFilter

use of io.spine.client.EntityIdFilter 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 RecordStorage<I> storage = getStorage();
    final EntityRecordWithColumns recordRight = create(fineRecord, matchingEntity, storage);
    final EntityRecordWithColumns recordWrong1 = create(notFineRecord1, wrongEntity1, storage);
    final EntityRecordWithColumns recordWrong2 = create(notFineRecord2, wrongEntity2, storage);
    // 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, storage);
    // Perform the query
    final Iterator<EntityRecord> readRecords = storage.readAll(query, FieldMask.getDefaultInstance());
    // Check results
    assertSingleRecord(fineRecord, readRecords);
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) EntityId(io.spine.client.EntityId) EntityIdFilter(io.spine.client.EntityIdFilter) EntityFilters(io.spine.client.EntityFilters) Any(com.google.protobuf.Any) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) Test(org.junit.Test)

Aggregations

EntityIdFilter (io.spine.client.EntityIdFilter)9 EntityFilters (io.spine.client.EntityFilters)7 EntityId (io.spine.client.EntityId)6 Test (org.junit.Test)6 Any (com.google.protobuf.Any)4 EntityRecord (io.spine.server.entity.EntityRecord)4 Message (com.google.protobuf.Message)2 CompositeColumnFilter (io.spine.client.CompositeColumnFilter)2 BoolValue (com.google.protobuf.BoolValue)1 Descriptors (com.google.protobuf.Descriptors)1 FieldMask (com.google.protobuf.FieldMask)1 ColumnFilter (io.spine.client.ColumnFilter)1 Query (io.spine.client.Query)1 Target (io.spine.client.Target)1 Version (io.spine.core.Version)1 EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)1 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)1 LinkedList (java.util.LinkedList)1