Search in sources :

Example 6 with EntityFilters

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

Example 7 with EntityFilters

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);
}
Also used : EntityFilters(io.spine.client.EntityFilters) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) EntityRecord(io.spine.server.entity.EntityRecord) Project(io.spine.test.storage.Project) Version(io.spine.base.Version) Int32Value(com.google.protobuf.Int32Value) Test(org.junit.Test)

Example 8 with EntityFilters

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;
        }
    };
}
Also used : EntityId(io.spine.client.EntityId) Message(com.google.protobuf.Message) ArgumentMatcher(org.mockito.ArgumentMatcher) EntityFilters(io.spine.client.EntityFilters) ProjectId(io.spine.test.projection.ProjectId) Any(com.google.protobuf.Any)

Example 9 with EntityFilters

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

Example 10 with EntityFilters

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

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