Search in sources :

Example 11 with FieldMask

use of com.google.protobuf.FieldMask in project core-java by SpineEventEngine.

the class StandStorageShould method read_all_records_of_given_type_with_field_mask.

@Test
public void read_all_records_of_given_type_with_field_mask() {
    final FieldMask mask = FieldMasks.maskOf(Project.getDescriptor(), 1, 2);
    checkByTypeRead(mask);
}
Also used : FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Example 12 with FieldMask

use of com.google.protobuf.FieldMask in project core-java by SpineEventEngine.

the class RecordStorageShould method read_multiple_records_with_field_mask.

@SuppressWarnings({ "MethodWithMultipleLoops", "ConstantConditions" })
// Converter nullability issues
@Test
public void read_multiple_records_with_field_mask() {
    final RecordStorage<I> storage = getStorage();
    final int count = 10;
    final List<I> ids = new LinkedList<>();
    Descriptors.Descriptor typeDescriptor = null;
    for (int i = 0; i < count; i++) {
        final I id = newId();
        final Message state = newState(id);
        final EntityRecord record = newStorageRecord(state);
        storage.write(id, record);
        ids.add(id);
        if (typeDescriptor == null) {
            typeDescriptor = state.getDescriptorForType();
        }
    }
    final int bulkCount = count / 2;
    final FieldMask fieldMask = FieldMasks.maskOf(typeDescriptor, 2);
    final Iterable<EntityRecord> readRecords = storage.readMultiple(ids.subList(0, bulkCount), fieldMask);
    final List<EntityRecord> readList = newLinkedList(readRecords);
    Verify.assertSize(bulkCount, readList);
    for (EntityRecord record : readRecords) {
        final Message state = unpack(record.getState());
        assertMatchesMask(state, fieldMask);
    }
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Message(com.google.protobuf.Message) Descriptors(com.google.protobuf.Descriptors) LinkedList(java.util.LinkedList) Lists.newLinkedList(com.google.common.collect.Lists.newLinkedList) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Example 13 with FieldMask

use of com.google.protobuf.FieldMask in project core-java by SpineEventEngine.

the class RecordStorageShould method retrieve_empty_map_if_storage_is_empty.

@Test
public void retrieve_empty_map_if_storage_is_empty() {
    final FieldMask nonEmptyFieldMask = FieldMask.newBuilder().addPaths("invalid-path").build();
    final RecordStorage storage = getStorage();
    final Map empty = storage.readAll(nonEmptyFieldMask);
    assertNotNull(empty);
    Verify.assertEmpty(empty);
}
Also used : Map(java.util.Map) HashMap(java.util.HashMap) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Example 14 with FieldMask

use of com.google.protobuf.FieldMask 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 15 with FieldMask

use of com.google.protobuf.FieldMask in project core-java by SpineEventEngine.

the class FieldMasksShould method apply_only_non_empty_mask_to_collection.

@SuppressWarnings({ "ObjectEquality", "MethodWithMultipleLoops" })
@Test
public void apply_only_non_empty_mask_to_collection() {
    final FieldMask emptyMask = Given.fieldMask();
    final Collection<Project> original = new LinkedList<>();
    final int count = 5;
    for (int i = 0; i < count; i++) {
        final Project project = Given.newProject(format("test-data--%s", i));
        original.add(project);
    }
    final Collection<Project> processed = FieldMasks.applyMask(emptyMask, original, Given.TYPE);
    assertSize(original.size(), processed);
    // The argument is not returned
    assertFalse(original == processed);
    // A copy of the argument is returned (Collection type may differ)
    final Iterator<Project> processedProjects = processed.iterator();
    for (Project anOriginal : original) {
        assertTrue(processedProjects.next().equals(anOriginal));
    }
}
Also used : Project(io.spine.test.aggregate.Project) FieldMask(com.google.protobuf.FieldMask) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

FieldMask (com.google.protobuf.FieldMask)29 Test (org.junit.Test)20 Any (com.google.protobuf.Any)7 EntityRecord (io.spine.server.entity.EntityRecord)6 Descriptors (com.google.protobuf.Descriptors)5 Message (com.google.protobuf.Message)5 Query (io.spine.client.Query)5 Project (io.spine.test.aggregate.Project)5 LinkedList (java.util.LinkedList)4 EntityFilters (io.spine.client.EntityFilters)3 Target (io.spine.client.Target)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ImmutableList (com.google.common.collect.ImmutableList)2 Version (io.spine.base.Version)2 EntityId (io.spine.client.EntityId)2 EntityIdFilter (io.spine.client.EntityIdFilter)2 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)2 TestEntity (io.spine.test.client.TestEntity)2 Customer (io.spine.test.commandservice.customer.Customer)2 CustomerId (io.spine.test.commandservice.customer.CustomerId)2