Search in sources :

Example 1 with ReactingAggregate

use of io.spine.server.aggregate.given.AggregateRepositoryTestEnv.ReactingAggregate 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)1 ReactingAggregate (io.spine.server.aggregate.given.AggregateRepositoryTestEnv.ReactingAggregate)1 ReactingRepository (io.spine.server.aggregate.given.AggregateRepositoryTestEnv.ReactingRepository)1 RejectionReactingAggregate (io.spine.server.aggregate.given.AggregateRepositoryTestEnv.RejectionReactingAggregate)1 RejectionReactingRepository (io.spine.server.aggregate.given.AggregateRepositoryTestEnv.RejectionReactingRepository)1 TestEventFactory (io.spine.server.command.TestEventFactory)1 ProjectId (io.spine.test.aggregate.ProjectId)1 AggProjectArchived (io.spine.test.aggregate.event.AggProjectArchived)1 Test (org.junit.Test)1