use of com.google.protobuf.Any in project core-java by SpineEventEngine.
the class RecordStorageShould method newStorageRecord.
private static EntityRecord newStorageRecord(Message state) {
final Any wrappedState = pack(state);
final EntityRecord record = EntityRecord.newBuilder().setState(wrappedState).setVersion(GivenVersion.withNumber(0)).build();
return record;
}
use of com.google.protobuf.Any in project core-java by SpineEventEngine.
the class RecordStorageShould method toEntityId.
private EntityId toEntityId(I id) {
final Any packed = Identifier.pack(id);
final EntityId entityId = EntityId.newBuilder().setId(packed).build();
return entityId;
}
use of com.google.protobuf.Any in project core-java by SpineEventEngine.
the class EntityQueryMatcherShould method match_ids.
@Test
public void match_ids() {
final Message genericId = Sample.messageOfType(ProjectId.class);
final Collection<Object> idFilter = Collections.<Object>singleton(genericId);
final Any entityId = AnyPacker.pack(genericId);
final EntityQuery<?> query = createQuery(idFilter, defaultQueryParameters());
final EntityQueryMatcher<?> matcher = new EntityQueryMatcher<>(query);
final EntityRecord matching = EntityRecord.newBuilder().setEntityId(entityId).build();
final Any otherEntityId = AnyPacker.pack(Sample.messageOfType(ProjectId.class));
final EntityRecord nonMatching = EntityRecord.newBuilder().setEntityId(otherEntityId).build();
final EntityRecordWithColumns matchingRecord = of(matching);
final EntityRecordWithColumns nonMatchingRecord = of(nonMatching);
assertTrue(matcher.apply(matchingRecord));
assertFalse(matcher.apply(nonMatchingRecord));
}
use of com.google.protobuf.Any in project core-java by SpineEventEngine.
the class EntityQueryMatcherShould method match_columns.
@SuppressWarnings({ // Mocks <-> reflection issues
"unchecked", // Test data is constant
"ConstantConditions" })
@Test
public void match_columns() {
final String targetName = "feature";
final EntityColumn target = mock(EntityColumn.class);
when(target.isNullable()).thenReturn(true);
when(target.getStoredName()).thenReturn(targetName);
when(target.getType()).thenReturn(Boolean.class);
final Serializable acceptedValue = true;
final Collection<Object> ids = Collections.emptyList();
final Multimap<EntityColumn, ColumnFilter> filters = of(target, eq(targetName, acceptedValue));
final CompositeQueryParameter parameter = createParams(filters, ALL);
final QueryParameters params = QueryParameters.newBuilder().add(parameter).build();
final EntityQuery<?> query = createQuery(ids, params);
final Any matchingId = AnyPacker.pack(Sample.messageOfType(TaskId.class));
final Any nonMatchingId = AnyPacker.pack(Sample.messageOfType(TaskId.class));
final EntityQueryMatcher<?> matcher = new EntityQueryMatcher<>(query);
final EntityRecord matching = EntityRecord.newBuilder().setEntityId(matchingId).build();
final EntityRecord nonMatching = EntityRecord.newBuilder().setEntityId(nonMatchingId).build();
final EntityColumn.MemoizedValue storedValue = mock(EntityColumn.MemoizedValue.class);
when(storedValue.getSourceColumn()).thenReturn(target);
when(storedValue.getValue()).thenReturn(acceptedValue);
final Map<String, EntityColumn.MemoizedValue> matchingColumns = ImmutableMap.of(targetName, storedValue);
final EntityRecordWithColumns nonMatchingRecord = of(nonMatching);
final EntityRecordWithColumns matchingRecord = createRecord(matching, matchingColumns);
assertTrue(matcher.apply(matchingRecord));
assertFalse(matcher.apply(nonMatchingRecord));
}
use of com.google.protobuf.Any in project core-java by SpineEventEngine.
the class Sample method messageOfType.
/**
* Generates a new stub {@link Message} with all the fields set to {@link Random random} values.
*
* <p> All the fields are guaranteed to be not {@code null} and not default.
* Number and {@code boolean} fields
* may or may not have their default values ({@code 0} and {@code false}).
*
* <p>If the required type is {@link Any}, an instance of an empty {@link Any} wrapped into
* another {@link Any} is returned. See {@link AnyPacker}.
*
* @param clazz Java class of the required stub message
* @param <M> type of the required message
* @return new instance of the given {@link Message} type with random fields
* @see #builderForType(Class)
*/
public static <M extends Message> M messageOfType(Class<M> clazz) {
checkClass(clazz);
if (Any.class.equals(clazz)) {
final Any any = Any.getDefaultInstance();
//
@SuppressWarnings("unchecked") final M result = (M) AnyPacker.pack(any);
return result;
}
final M.Builder builder = builderForType(clazz);
// Checked cast
@SuppressWarnings("unchecked") final M result = (M) builder.build();
return result;
}
Aggregations