Search in sources :

Example 41 with Any

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;
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Any(com.google.protobuf.Any)

Example 42 with Any

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;
}
Also used : EntityId(io.spine.client.EntityId) Any(com.google.protobuf.Any)

Example 43 with Any

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));
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Message(com.google.protobuf.Message) ProjectId(io.spine.test.entity.ProjectId) Any(com.google.protobuf.Any) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) Test(org.junit.Test)

Example 44 with Any

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));
}
Also used : Serializable(java.io.Serializable) TaskId(io.spine.test.entity.TaskId) EntityColumn(io.spine.server.entity.storage.EntityColumn) ColumnFilter(io.spine.client.ColumnFilter) QueryParameters(io.spine.server.entity.storage.QueryParameters) Any(com.google.protobuf.Any) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) CompositeQueryParameter(io.spine.server.entity.storage.CompositeQueryParameter) EntityRecord(io.spine.server.entity.EntityRecord) Test(org.junit.Test)

Example 45 with Any

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;
}
Also used : Any(com.google.protobuf.Any)

Aggregations

Any (com.google.protobuf.Any)212 Test (org.junit.Test)88 Message (com.google.protobuf.Message)45 TypeConverter.toAny (io.spine.protobuf.TypeConverter.toAny)27 EntityRecord (io.spine.server.entity.EntityRecord)24 TypeUrl (io.spine.type.TypeUrl)15 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)11 Version (io.spine.core.Version)11 Customer (io.spine.test.commandservice.customer.Customer)11 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)10 CdsUpdate (io.grpc.xds.XdsClient.CdsUpdate)9 GivenVersion (io.spine.core.given.GivenVersion)9 EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)8 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)8 CustomerId (io.spine.test.commandservice.customer.CustomerId)8 Query (io.spine.client.Query)7 Project (io.spine.test.storage.Project)7 FieldMask (com.google.protobuf.FieldMask)6 LbEndpoint (io.grpc.xds.Endpoints.LbEndpoint)6 EntityId (io.spine.client.EntityId)6