Search in sources :

Example 46 with Event

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

the class AggregateShould method traverse_the_history_iterating_through_newest_events_first.

@Test
public void traverse_the_history_iterating_through_newest_events_first() {
    final TenantId tenantId = newTenantId();
    final Command createCommand = command(createProject, tenantId);
    final Command startCommand = command(startProject, tenantId);
    final Command addTaskCommand = command(addTask, tenantId);
    final Command addTaskCommand2 = command(addTask, tenantId);
    final CommandBus commandBus = boundedContext.getCommandBus();
    final StreamObserver<Ack> noOpObserver = noOpObserver();
    commandBus.post(createCommand, noOpObserver);
    commandBus.post(addTaskCommand, noOpObserver);
    commandBus.post(newArrayList(addTaskCommand2, startCommand), noOpObserver);
    final TestAggregate aggregate = repository.loadAggregate(tenantId, ID);
    final Iterator<Event> history = aggregate.historyBackward();
    assertEquals(startCommand.getId(), getRootCommandId(history.next()));
    assertEquals(addTaskCommand2.getId(), getRootCommandId(history.next()));
    assertEquals(addTaskCommand.getId(), getRootCommandId(history.next()));
    assertEquals(createCommand.getId(), getRootCommandId(history.next()));
    assertFalse(history.hasNext());
}
Also used : 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) TestAggregate(io.spine.server.aggregate.given.aggregate.TestAggregate) Ack(io.spine.core.Ack) Event(io.spine.core.Event) CommandBus(io.spine.server.commandbus.CommandBus) Test(org.junit.Test)

Example 47 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_command_dispatch.

/**
 * Ensures that a {@linkplain io.spine.server.tuple.Pair pair} with an empty second
 * optional value returned from a command handler stores a single event.
 *
 * <p>The command handler that should return a pair is
 * {@link TaskAggregate#handle(AggAssignTask)
 * TaskAggregate#handle(AggAssignTask)}.
 */
@Test
public void create_single_event_for_a_pair_of_events_with_empty_for_a_command_dispatch() {
    final BoundedContext boundedContext = newTaskBoundedContext();
    final TenantId tenantId = newTenantId();
    final Command command = command(createTask(), 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);
}
Also used : Ack(io.spine.core.Ack) Error(io.spine.base.Error) 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 48 with Event

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

the class AggregateShould method traverse_the_history_up_to_the_latest_snapshot.

@Test
public void traverse_the_history_up_to_the_latest_snapshot() {
    repository.setSnapshotTrigger(3);
    final TenantId tenantId = newTenantId();
    final Command createCommand = command(createProject, tenantId);
    final Command startCommand = command(startProject, tenantId);
    final Command addTaskCommand = command(addTask, tenantId);
    final Command addTaskCommand2 = command(addTask, tenantId);
    final CommandBus commandBus = boundedContext.getCommandBus();
    final StreamObserver<Ack> noOpObserver = noOpObserver();
    commandBus.post(createCommand, noOpObserver);
    commandBus.post(startCommand, noOpObserver);
    commandBus.post(newArrayList(addTaskCommand, addTaskCommand2), noOpObserver);
    final TestAggregate aggregate = repository.loadAggregate(tenantId, ID);
    final Iterator<Event> history = aggregate.historyBackward();
    assertEquals(addTaskCommand2.getId(), getRootCommandId(history.next()));
    assertFalse(history.hasNext());
}
Also used : 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) TestAggregate(io.spine.server.aggregate.given.aggregate.TestAggregate) Ack(io.spine.core.Ack) Event(io.spine.core.Event) CommandBus(io.spine.server.commandbus.CommandBus) Test(org.junit.Test)

Example 49 with Event

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

the class AggregateStorageShould method writeAndReadEventTest.

// OK as we write right before we get.
@SuppressWarnings("OptionalGetWithoutIsPresent")
private <I> void writeAndReadEventTest(I id, AggregateStorage<I> storage) {
    final Event expectedEvent = eventFactory.createEvent(Time.getCurrentTime());
    storage.writeEvent(id, expectedEvent);
    final AggregateReadRequest<I> readRequest = new AggregateReadRequest<>(id, MAX_VALUE);
    final AggregateStateRecord events = storage.read(readRequest).get();
    assertEquals(1, events.getEventCount());
    final Event actualEvent = events.getEvent(0);
    assertEquals(expectedEvent, actualEvent);
    close(storage);
}
Also used : Event(io.spine.core.Event)

Example 50 with Event

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

the class AggregateStorageShould method not_store_enrichment_for_EventContext.

@Test
public void not_store_enrichment_for_EventContext() {
    final EventContext enrichedContext = EventContext.newBuilder().setEnrichment(withOneAttribute()).build();
    final Event event = Event.newBuilder().setId(newEventId()).setContext(enrichedContext).setMessage(Any.getDefaultInstance()).build();
    storage.writeEvent(id, event);
    final EventContext loadedContext = storage.read(newReadRequest(id)).get().getEvent(0).getContext();
    assertTrue(isDefault(loadedContext.getEnrichment()));
}
Also used : EventContext(io.spine.core.EventContext) 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