Search in sources :

Example 41 with Event

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

the class TransactionShould method advance_version_from_event.

@Test
public void advance_version_from_event() {
    final E entity = createEntity();
    final Transaction<I, E, S, B> tx = createTx(entity);
    assertEquals(entity.getVersion(), tx.getVersion());
    final Event event = createEvent(createEventMessage());
    final EventEnvelope envelope = EventEnvelope.of(event);
    tx.apply(envelope);
    assertEquals(event.getContext().getVersion(), tx.getVersion());
    tx.commit();
    assertEquals(event.getContext().getVersion(), entity.getVersion());
}
Also used : EventEnvelope(io.spine.core.EventEnvelope) Event(io.spine.core.Event) Test(org.junit.Test)

Example 42 with Event

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

the class TransactionShould method propagate_changes_to_entity_upon_commit.

@Test
public void propagate_changes_to_entity_upon_commit() {
    final E entity = createEntity();
    final Transaction<I, E, S, B> tx = createTx(entity);
    final Event event = createEvent(createEventMessage());
    applyEvent(tx, event);
    final S stateBeforeCommit = entity.getState();
    final Version versionBeforeCommit = entity.getVersion();
    tx.commit();
    final S modifiedState = entity.getState();
    final Version modifiedVersion = entity.getVersion();
    assertNotEquals(stateBeforeCommit, modifiedState);
    assertNotEquals(versionBeforeCommit, modifiedVersion);
}
Also used : Versions.newVersion(io.spine.core.Versions.newVersion) Version(io.spine.core.Version) Event(io.spine.core.Event) Test(org.junit.Test)

Example 43 with Event

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

the class GivenEvent method withDisabledEnrichmentOf.

public static Event withDisabledEnrichmentOf(Message message) {
    final Event event = withMessage(message);
    final Event.Builder builder = event.toBuilder().setContext(event.getContext().toBuilder().setEnrichment(Enrichment.newBuilder().setDoNotEnrich(true)));
    return builder.build();
}
Also used : Event(io.spine.core.Event)

Example 44 with Event

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

the class AggregateRepositoryShould method route_events_to_aggregates.

@Test
public void route_events_to_aggregates() {
    final ProjectAggregate parent = givenStoredAggregate();
    final ProjectAggregate child = givenStoredAggregate();
    assertTrue(repository.find(parent.getId()).isPresent());
    assertTrue(repository.find(child.getId()).isPresent());
    final TestEventFactory factory = TestEventFactory.newInstance(getClass());
    final AggProjectArchived msg = AggProjectArchived.newBuilder().setProjectId(parent.getId()).addChildProjectId(child.getId()).build();
    final Event event = factory.createEvent(msg);
    boundedContext.getEventBus().post(event);
    // Check that the child aggregate was archived.
    final Optional<ProjectAggregate> childAfterArchive = repository.find(child.getId());
    assertTrue(childAfterArchive.isPresent());
    assertTrue(childAfterArchive.get().isArchived());
    // The parent should not be archived since the dispatch route uses only
    // child aggregates from the `ProjectArchived` event.
    final Optional<ProjectAggregate> parentAfterArchive = repository.find(parent.getId());
    assertTrue(parentAfterArchive.isPresent());
    assertFalse(parentAfterArchive.get().isArchived());
}
Also used : ProjectAggregate(io.spine.server.aggregate.given.AggregateRepositoryTestEnv.ProjectAggregate) TestEventFactory(io.spine.server.command.TestEventFactory) Event(io.spine.core.Event) AggProjectArchived(io.spine.test.aggregate.event.AggProjectArchived) Test(org.junit.Test)

Example 45 with Event

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

the class AggregateRepositoryShould method allow_aggregates_react_on_events.

@Test
public void allow_aggregates_react_on_events() {
    final ReactingRepository repository = new ReactingRepository();
    boundedContext.register(repository);
    final ProjectId parentId = givenAggregateId("parent");
    final ProjectId childId = givenAggregateId("child");
    /**
     * Create event factory for which producer ID would be the `parentId`.
     * Custom routing set by {@linkplain ReactingRepository()} would use
     * child IDs from the event.
     */
    final TestEventFactory factory = TestEventFactory.newInstance(Identifier.pack(parentId), getClass());
    final AggProjectArchived msg = AggProjectArchived.newBuilder().setProjectId(parentId).addChildProjectId(childId).build();
    final Event event = factory.createEvent(msg);
    // Posting this event should archive the aggregate.
    boundedContext.getEventBus().post(event);
    // Check that the aggregate marked itself as `archived`, and therefore became invisible
    // to regular queries.
    final Optional<ReactingAggregate> optional = repository.find(childId);
    // The aggregate was created because of dispatching.
    assertTrue(optional.isPresent());
    // The proper method was called, which we check by the state the aggregate got.
    assertEquals(ReactingAggregate.PROJECT_ARCHIVED, optional.get().getState().getValue());
}
Also used : ReactingRepository(io.spine.server.aggregate.given.AggregateRepositoryTestEnv.ReactingRepository) RejectionReactingRepository(io.spine.server.aggregate.given.AggregateRepositoryTestEnv.RejectionReactingRepository) ReactingAggregate(io.spine.server.aggregate.given.AggregateRepositoryTestEnv.ReactingAggregate) RejectionReactingAggregate(io.spine.server.aggregate.given.AggregateRepositoryTestEnv.RejectionReactingAggregate) TestEventFactory(io.spine.server.command.TestEventFactory) ProjectId(io.spine.test.aggregate.ProjectId) Event(io.spine.core.Event) AggProjectArchived(io.spine.test.aggregate.event.AggProjectArchived) 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