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