Search in sources :

Example 71 with Event

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

the class AggregateMessageDeliveryShould method postpone_events_dispatched_to_reactor_method.

@Test
public void postpone_events_dispatched_to_reactor_method() {
    assertNull(ReactingProject.getEventReceived());
    final Event event = projectStarted();
    boundedContext.getEventBus().post(event);
    assertNull(ReactingProject.getEventReceived());
    final EventEnvelope expectedEnvelope = EventEnvelope.of(event);
    final PostponingEventDelivery delivery = repository.getEventEndpointDelivery();
    final Map<ProjectId, EventEnvelope> postponedEvents = delivery.getPostponedEvents();
    assertTrue(postponedEvents.size() == 1 && postponedEvents.containsValue(expectedEnvelope));
    final ProjectId projectId = postponedEvents.keySet().iterator().next();
    delivery.deliverNow(projectId, postponedEvents.get(projectId));
    final AggProjectStarted deliveredEventMsg = ReactingProject.getEventReceived();
    assertNotNull(deliveredEventMsg);
    assertEquals(Events.getMessage(event), deliveredEventMsg);
}
Also used : EventEnvelope(io.spine.core.EventEnvelope) ProjectId(io.spine.test.aggregate.ProjectId) Event(io.spine.core.Event) PostponingEventDelivery(io.spine.server.aggregate.given.AggregateMessageDeliveryTestEnv.PostponingEventDelivery) AggProjectStarted(io.spine.test.aggregate.event.AggProjectStarted) Test(org.junit.Test)

Example 72 with Event

use of io.spine.core.Event 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 73 with Event

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

the class AggregateShould method propagate_RuntimeException_when_play_raises_exception.

@Test
public void propagate_RuntimeException_when_play_raises_exception() {
    ModelTests.clearModel();
    final FaultyAggregate faultyAggregate = new FaultyAggregate(ID, false, true);
    try {
        final Event event = event(projectCreated(ID, getClass().getSimpleName()), 1);
        final AggregateTransaction tx = AggregateTransaction.start(faultyAggregate);
        ((Aggregate) faultyAggregate).play(AggregateStateRecord.newBuilder().addEvent(event).build());
        tx.commit();
        failNotThrows();
    } catch (RuntimeException e) {
        @SuppressWarnings("ThrowableResultOfMethodCallIgnored") final Throwable // because we need it for checking.
        cause = getRootCause(e);
        assertTrue(cause instanceof IllegalStateException);
        assertEquals(FaultyAggregate.BROKEN_APPLIER, cause.getMessage());
    }
}
Also used : Event(io.spine.core.Event) FaultyAggregate(io.spine.server.aggregate.given.aggregate.FaultyAggregate) IntAggregate(io.spine.server.aggregate.given.aggregate.IntAggregate) AmishAggregate(io.spine.server.aggregate.given.aggregate.AmishAggregate) TaskAggregate(io.spine.server.aggregate.given.aggregate.TaskAggregate) TestAggregate(io.spine.server.aggregate.given.aggregate.TestAggregate) UserAggregate(io.spine.server.aggregate.given.aggregate.UserAggregate) FaultyAggregate(io.spine.server.aggregate.given.aggregate.FaultyAggregate) Test(org.junit.Test)

Example 74 with Event

use of io.spine.core.Event 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 75 with Event

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

the class AggregateShould method write_its_version_into_event_context.

@Test
public void write_its_version_into_event_context() {
    dispatchCommand(aggregate, env(createProject));
    // Get the first event since the command handler produces only one event message.
    final Aggregate<?, ?, ?> agg = this.aggregate;
    final List<Event> uncommittedEvents = agg.getUncommittedEvents();
    final Event event = uncommittedEvents.get(0);
    assertEquals(this.aggregate.getVersion(), event.getContext().getVersion());
}
Also used : Event(io.spine.core.Event) Test(org.junit.Test)

Aggregations

Event (io.spine.core.Event)115 Test (org.junit.Test)75 Command (io.spine.core.Command)19 EventContext (io.spine.core.EventContext)12 BoundedContext (io.spine.server.BoundedContext)12 GivenEvent (io.spine.server.event.given.EventBusTestEnv.GivenEvent)12 Version (io.spine.core.Version)10 TestEventFactory (io.spine.server.command.TestEventFactory)10 Message (com.google.protobuf.Message)9 EventEnvelope (io.spine.core.EventEnvelope)9 InMemoryTransportFactory (io.spine.server.integration.memory.InMemoryTransportFactory)9 Timestamp (com.google.protobuf.Timestamp)6 Ack (io.spine.core.Ack)6 TenantId (io.spine.core.TenantId)6 Error (io.spine.base.Error)5 AggregateMessageDispatcher.dispatchCommand (io.spine.server.aggregate.AggregateMessageDispatcher.dispatchCommand)5 AggregateTestEnv.newTenantId (io.spine.server.aggregate.given.aggregate.AggregateTestEnv.newTenantId)5 StringValue (com.google.protobuf.StringValue)4 GivenEvent (io.spine.core.given.GivenEvent)4 Duration (com.google.protobuf.Duration)3