Search in sources :

Example 26 with FieldMask

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

the class RecordStorageShould method read_single_record_with_mask.

// Converter nullability issues
@SuppressWarnings("ConstantConditions")
@Test
public void read_single_record_with_mask() {
    final I id = newId();
    final EntityRecord record = newStorageRecord(id);
    final RecordStorage<I> storage = getStorage();
    storage.write(id, record);
    final Descriptors.Descriptor descriptor = newState(id).getDescriptorForType();
    final FieldMask idMask = FieldMasks.maskOf(descriptor, 1);
    final RecordReadRequest<I> readRequest = new RecordReadRequest<>(id);
    final Optional<EntityRecord> optional = storage.read(readRequest, idMask);
    assertTrue(optional.isPresent());
    final EntityRecord entityRecord = optional.get();
    final Message unpacked = unpack(entityRecord.getState());
    assertFalse(isDefault(unpacked));
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Message(com.google.protobuf.Message) Descriptors(com.google.protobuf.Descriptors) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Example 27 with FieldMask

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

the class StandShould method doCheckReadingCustomersByIdAndFieldMask.

@SuppressWarnings("MethodWithMultipleLoops")
private void doCheckReadingCustomersByIdAndFieldMask(String... paths) {
    final Stand stand = prepareStandWithAggregateRepo(createStandStorage());
    final int querySize = 2;
    final Set<CustomerId> ids = new HashSet<>();
    for (int i = 0; i < querySize; i++) {
        final Customer customer = getSampleCustomer().toBuilder().setId(CustomerId.newBuilder().setNumber(i)).build();
        final Version stateVersion = GivenVersion.withNumber(1);
        stand.update(asEnvelope(customer.getId(), customer, stateVersion));
        ids.add(customer.getId());
    }
    final Query customerQuery = requestFactory.query().byIdsWithMask(Customer.class, ids, paths);
    final FieldMask fieldMask = FieldMask.newBuilder().addAllPaths(Arrays.asList(paths)).build();
    final MemoizeQueryResponseObserver observer = new MemoizeQueryResponseObserver() {

        @Override
        public void onNext(QueryResponse value) {
            super.onNext(value);
            final List<Any> messages = value.getMessagesList();
            Verify.assertSize(ids.size(), messages);
            for (Any message : messages) {
                final Customer customer = unpack(message);
                assertNotEquals(customer, null);
                assertMatches(customer, fieldMask);
            }
        }
    };
    stand.execute(customerQuery, observer);
    verifyObserver(observer);
}
Also used : Query(io.spine.client.Query) Customer(io.spine.test.commandservice.customer.Customer) CustomerId(io.spine.test.commandservice.customer.CustomerId) Any(com.google.protobuf.Any) GivenVersion(io.spine.core.given.GivenVersion) Version(io.spine.core.Version) QueryResponse(io.spine.client.QueryResponse) FieldMask(com.google.protobuf.FieldMask) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) HashSet(java.util.HashSet)

Example 28 with FieldMask

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

the class StandShould method select_entity_singleton_by_id_and_apply_field_masks.

@SuppressWarnings("MethodWithMultipleLoops")
@Test
public void select_entity_singleton_by_id_and_apply_field_masks() {
    final Stand stand = prepareStandWithAggregateRepo(createStandStorage());
    final String customerDescriptor = Customer.getDescriptor().getFullName();
    // clashes with non-related tests.
    @SuppressWarnings("DuplicateStringLiteralInspection") final String[] paths = { customerDescriptor + ".id", customerDescriptor + ".name" };
    final FieldMask fieldMask = FieldMask.newBuilder().addAllPaths(Arrays.asList(paths)).build();
    final List<Customer> customers = new LinkedList<>();
    final int count = 10;
    for (int i = 0; i < count; i++) {
        // Has new ID each time
        final Customer customer = getSampleCustomer();
        customers.add(customer);
        final Version stateVersion = GivenVersion.withNumber(1);
        stand.update(asEnvelope(customer.getId(), customer, stateVersion));
    }
    final Set<CustomerId> ids = Collections.singleton(customers.get(0).getId());
    final Query customerQuery = requestFactory.query().byIdsWithMask(Customer.class, ids, paths);
    final MemoizeQueryResponseObserver observer = new MemoizeQueryResponseObserver();
    stand.execute(customerQuery, observer);
    final List<Any> read = observer.responseHandled.getMessagesList();
    Verify.assertSize(1, read);
    final Customer customer = unpack(read.get(0));
    assertMatches(customer, fieldMask);
    assertTrue(ids.contains(customer.getId()));
    verifyObserver(observer);
}
Also used : Query(io.spine.client.Query) Customer(io.spine.test.commandservice.customer.Customer) CustomerId(io.spine.test.commandservice.customer.CustomerId) Any(com.google.protobuf.Any) LinkedList(java.util.LinkedList) GivenVersion(io.spine.core.given.GivenVersion) Version(io.spine.core.Version) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 29 with FieldMask

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

the class QueryBuilderShould method persist_only_last_field_mask.

@Test
public void persist_only_last_field_mask() {
    final Iterable<String> iterableFields = singleton("TestEntity.firstField");
    final String[] arrayFields = { "TestEntity.secondField" };
    final Query query = factory().query().select(TestEntity.class).withMask(iterableFields).withMask(arrayFields).build();
    assertNotNull(query);
    final FieldMask mask = query.getFieldMask();
    final Collection<String> maskFields = mask.getPathsList();
    assertSize(arrayFields.length, maskFields);
    assertThat(maskFields, contains(arrayFields));
}
Also used : TestEntity(io.spine.test.client.TestEntity) Matchers.containsString(org.hamcrest.Matchers.containsString) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Example 30 with FieldMask

use of com.google.protobuf.FieldMask 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 coexistence.
@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 List<CompositeColumnFilter> aggregatingColumnFilters = entityFilters.getFilterList();
    assertSize(1, aggregatingColumnFilters);
    final Collection<ColumnFilter> columnFilters = aggregatingColumnFilters.get(0).getFilterList();
    assertSize(2, columnFilters);
    final Any actualValue1 = findByName(columnFilters, columnName1).getValue();
    assertNotNull(actualValue1);
    final int actualGenericValue1 = toObject(actualValue1, int.class);
    assertEquals(columnValue1, actualGenericValue1);
    final Any actualValue2 = findByName(columnFilters, columnName2).getValue();
    assertNotNull(actualValue2);
    final Message actualGenericValue2 = toObject(actualValue2, ProjectId.class);
    assertEquals(columnValue2, actualGenericValue2);
}
Also used : TestEntity(io.spine.test.client.TestEntity) Message(com.google.protobuf.Message) Matchers.containsString(org.hamcrest.Matchers.containsString) Any(com.google.protobuf.Any) TypeConverter.toObject(io.spine.protobuf.TypeConverter.toObject) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Aggregations

FieldMask (com.google.protobuf.FieldMask)31 Test (org.junit.Test)22 Any (com.google.protobuf.Any)7 EntityRecord (io.spine.server.entity.EntityRecord)7 Descriptors (com.google.protobuf.Descriptors)5 Message (com.google.protobuf.Message)5 Project (io.spine.test.aggregate.Project)5 LinkedList (java.util.LinkedList)4 EntityFilters (io.spine.client.EntityFilters)3 Project (io.spine.test.storage.Project)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ImmutableList (com.google.common.collect.ImmutableList)2 Query (io.spine.client.Query)2 Target (io.spine.client.Target)2 Version (io.spine.core.Version)2 GivenVersion (io.spine.core.given.GivenVersion)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