Search in sources :

Example 6 with EntityId

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);
    }
}
Also used : EntityId(io.spine.client.EntityId) Message(com.google.protobuf.Message) EntityIdFilter(io.spine.client.EntityIdFilter) EntityFilters(io.spine.client.EntityFilters) Descriptors(com.google.protobuf.Descriptors) FieldMask(com.google.protobuf.FieldMask) TenantAwareTest(io.spine.server.tenant.TenantAwareTest) Test(org.junit.Test)

Example 7 with EntityId

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;
}
Also used : EntityId(io.spine.client.EntityId) EntityIdFilter(io.spine.client.EntityIdFilter) Any(com.google.protobuf.Any) LinkedList(java.util.LinkedList)

Example 8 with EntityId

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);
}
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 9 with EntityId

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);
}
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)

Example 10 with EntityId

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

Aggregations

EntityId (io.spine.client.EntityId)11 Test (org.junit.Test)8 Any (com.google.protobuf.Any)7 EntityFilters (io.spine.client.EntityFilters)7 EntityIdFilter (io.spine.client.EntityIdFilter)7 Message (com.google.protobuf.Message)5 Query (io.spine.client.Query)3 Target (io.spine.client.Target)3 FieldMask (com.google.protobuf.FieldMask)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 BoolValue (com.google.protobuf.BoolValue)1 Descriptors (com.google.protobuf.Descriptors)1 StringValue (com.google.protobuf.StringValue)1 Identifiers.idToAny (io.spine.base.Identifiers.idToAny)1 Version (io.spine.base.Version)1 AbstractVersionableEntity (io.spine.server.entity.AbstractVersionableEntity)1 EntityRecord (io.spine.server.entity.EntityRecord)1 EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)1 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)1 TestEntity (io.spine.test.client.TestEntity)1