Search in sources :

Example 31 with TypeUrl

use of io.spine.type.TypeUrl in project core-java by SpineEventEngine.

the class TenantRecords method findAndApplyFieldMask.

EntityRecord findAndApplyFieldMask(I givenId, FieldMask fieldMask) {
    EntityRecord matchingResult = null;
    for (I recordId : filtered.keySet()) {
        if (recordId.equals(givenId)) {
            final Optional<EntityRecordWithColumns> record = get(recordId);
            if (!record.isPresent()) {
                continue;
            }
            EntityRecord.Builder matchingRecord = record.get().getRecord().toBuilder();
            final Any state = matchingRecord.getState();
            final TypeUrl typeUrl = TypeUrl.parse(state.getTypeUrl());
            final Message wholeState = unpack(state);
            final Message maskedState = applyMask(fieldMask, wholeState, typeUrl);
            final Any processed = pack(maskedState);
            matchingRecord.setState(processed);
            matchingResult = matchingRecord.build();
        }
    }
    return matchingResult;
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Message(com.google.protobuf.Message) TypeUrl(io.spine.type.TypeUrl) Any(com.google.protobuf.Any) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns)

Example 32 with TypeUrl

use of io.spine.type.TypeUrl in project core-java by SpineEventEngine.

the class DefaultEntityStorageConverterShould method setUp.

@Before
public void setUp() {
    final BoundedContext bc = BoundedContext.newBuilder().build();
    RecordBasedRepository<Long, TestEntity, StringValue> repository = new TestRepository();
    bc.register(repository);
    final TypeUrl stateType = repository.entityClass().getStateType();
    converter = forAllFields(stateType, repository.entityFactory());
}
Also used : BoundedContext(io.spine.server.BoundedContext) TypeUrl(io.spine.type.TypeUrl) StringValue(com.google.protobuf.StringValue) Before(org.junit.Before)

Example 33 with TypeUrl

use of io.spine.type.TypeUrl in project core-java by SpineEventEngine.

the class AggregateShould method create_single_event_for_a_pair_of_events_with_empty_for_an_event_react.

/**
 * Ensures that a {@linkplain io.spine.server.tuple.Pair pair} with an empty second optional
 * value returned from a reaction on an event stores a single event.
 *
 * <p>The first event is produced while handling a command by the
 * {@link TaskAggregate#handle(AggAssignTask) TaskAggregate#handle(AggAssignTask)}.
 * Then as a reaction to this event a single event should be fired as part of the pair by
 * {@link TaskAggregate#on(AggTaskAssigned) TaskAggregate#on(AggTaskAssigned)}.
 */
@Test
public void create_single_event_for_a_pair_of_events_with_empty_for_an_event_react() {
    final BoundedContext boundedContext = newTaskBoundedContext();
    final TenantId tenantId = newTenantId();
    final Command command = command(assignTask(), tenantId);
    final MemoizingObserver<Ack> observer = memoizingObserver();
    boundedContext.getCommandBus().post(command, observer);
    assertNull(observer.getError());
    final List<Ack> responses = observer.responses();
    assertSize(1, responses);
    final Ack response = responses.get(0);
    final io.spine.core.Status status = response.getStatus();
    final Error emptyError = Error.getDefaultInstance();
    assertEquals(emptyError, status.getError());
    final Rejection emptyRejection = Rejection.getDefaultInstance();
    assertEquals(emptyRejection, status.getRejection());
    final List<Event> events = readAllEvents(boundedContext, tenantId);
    assertSize(2, events);
    final Event sourceEvent = events.get(0);
    final TypeUrl taskAssignedType = TypeUrl.from(AggTaskAssigned.getDescriptor());
    assertEquals(typeUrlOf(sourceEvent), taskAssignedType);
    final Event reactionEvent = events.get(1);
    final TypeUrl userNotifiedType = TypeUrl.from(AggUserNotified.getDescriptor());
    assertEquals(typeUrlOf(reactionEvent), userNotifiedType);
}
Also used : Ack(io.spine.core.Ack) Error(io.spine.base.Error) TypeUrl(io.spine.type.TypeUrl) AggregateMessageDispatcher.dispatchRejection(io.spine.server.aggregate.AggregateMessageDispatcher.dispatchRejection) Rejection(io.spine.core.Rejection) AggregateTestEnv.newTenantId(io.spine.server.aggregate.given.aggregate.AggregateTestEnv.newTenantId) TenantId(io.spine.core.TenantId) Command(io.spine.core.Command) AggregateMessageDispatcher.dispatchCommand(io.spine.server.aggregate.AggregateMessageDispatcher.dispatchCommand) Event(io.spine.core.Event) BoundedContext(io.spine.server.BoundedContext) AggregateTestEnv.newTaskBoundedContext(io.spine.server.aggregate.given.aggregate.AggregateTestEnv.newTaskBoundedContext) Test(org.junit.Test)

Example 34 with TypeUrl

use of io.spine.type.TypeUrl in project core-java by SpineEventEngine.

the class AggregateShould method create_single_event_for_a_pair_of_events_with_empty_for_a_rejection_react.

/**
 * Ensures that a {@linkplain io.spine.server.tuple.Pair pair} with an empty second optional
 * value returned from a reaction on a rejection stores a single event.
 *
 * <p>The rejection is fired by the {@link TaskAggregate#handle(AggReassignTask)
 * TaskAggregate.handle(AggReassignTask)}
 * and handled by the {@link TaskAggregate#on(Rejections.AggCannotReassignUnassignedTask)
 * TaskAggregate.on(AggCannotReassignUnassignedTask)}.
 */
@Test
public void create_single_event_for_a_pair_of_events_with_empty_for_a_rejection_react() {
    final BoundedContext boundedContext = newTaskBoundedContext();
    final TenantId tenantId = newTenantId();
    final Command command = command(reassignTask(), tenantId);
    final MemoizingObserver<Ack> observer = memoizingObserver();
    boundedContext.getCommandBus().post(command, observer);
    assertNull(observer.getError());
    final List<Ack> responses = observer.responses();
    assertSize(1, responses);
    final Ack response = responses.get(0);
    final io.spine.core.Status status = response.getStatus();
    final Error emptyError = Error.getDefaultInstance();
    assertEquals(emptyError, status.getError());
    final Rejection emptyRejection = Rejection.getDefaultInstance();
    assertEquals(emptyRejection, status.getRejection());
    final List<Event> events = readAllEvents(boundedContext, tenantId);
    assertSize(1, events);
    final Event reactionEvent = events.get(0);
    final TypeUrl userNotifiedType = TypeUrl.from(AggUserNotified.getDescriptor());
    assertEquals(typeUrlOf(reactionEvent), userNotifiedType);
}
Also used : Ack(io.spine.core.Ack) Error(io.spine.base.Error) TypeUrl(io.spine.type.TypeUrl) AggregateMessageDispatcher.dispatchRejection(io.spine.server.aggregate.AggregateMessageDispatcher.dispatchRejection) Rejection(io.spine.core.Rejection) AggregateTestEnv.newTenantId(io.spine.server.aggregate.given.aggregate.AggregateTestEnv.newTenantId) TenantId(io.spine.core.TenantId) Command(io.spine.core.Command) AggregateMessageDispatcher.dispatchCommand(io.spine.server.aggregate.AggregateMessageDispatcher.dispatchCommand) Event(io.spine.core.Event) BoundedContext(io.spine.server.BoundedContext) AggregateTestEnv.newTaskBoundedContext(io.spine.server.aggregate.given.aggregate.AggregateTestEnv.newTaskBoundedContext) Test(org.junit.Test)

Example 35 with TypeUrl

use of io.spine.type.TypeUrl in project core-java by SpineEventEngine.

the class Field method getFieldClass.

/**
 * Returns the class of the Protobuf message field.
 *
 * @param field the field descriptor
 * @return the class of the field
 * @throws IllegalArgumentException if the field type is unknown
 */
// as each branch is a fairly simple.
@SuppressWarnings("OverlyComplexMethod")
public static Class<?> getFieldClass(FieldDescriptor field) {
    checkNotNull(field);
    final FieldDescriptor.JavaType javaType = field.getJavaType();
    final TypeUrl typeUrl;
    switch(javaType) {
        case INT:
            return Integer.class;
        case LONG:
            return Long.class;
        case FLOAT:
            return Float.class;
        case DOUBLE:
            return Double.class;
        case BOOLEAN:
            return Boolean.class;
        case STRING:
            return String.class;
        case BYTE_STRING:
            return ByteString.class;
        case ENUM:
            typeUrl = TypeUrl.from(field.getEnumType());
            final Class<? extends Message> enumClass = typeUrl.getJavaClass();
            return enumClass;
        case MESSAGE:
            typeUrl = TypeUrl.from(field.getMessageType());
            final Class<? extends Message> msgClass = typeUrl.getJavaClass();
            return msgClass;
        default:
            throw newIllegalArgumentException("Unknown field type discovered: %s", field.getFullName());
    }
}
Also used : ByteString(com.google.protobuf.ByteString) TypeUrl(io.spine.type.TypeUrl) ByteString(com.google.protobuf.ByteString) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

TypeUrl (io.spine.type.TypeUrl)46 Test (org.junit.Test)15 Any (com.google.protobuf.Any)14 EntityRecord (io.spine.server.entity.EntityRecord)8 Message (com.google.protobuf.Message)7 BoundedContext (io.spine.server.BoundedContext)5 EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)3 ClassName (io.spine.type.ClassName)3 Descriptors (com.google.protobuf.Descriptors)2 StringValue (com.google.protobuf.StringValue)2 Error (io.spine.base.Error)2 Query (io.spine.client.Query)2 Ack (io.spine.core.Ack)2 Command (io.spine.core.Command)2 Event (io.spine.core.Event)2 Rejection (io.spine.core.Rejection)2 TenantId (io.spine.core.TenantId)2 Version (io.spine.core.Version)2 TypeConverter.toAny (io.spine.protobuf.TypeConverter.toAny)2 AggregateMessageDispatcher.dispatchCommand (io.spine.server.aggregate.AggregateMessageDispatcher.dispatchCommand)2