use of io.spine.server.command.TestEventFactory 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());
}
use of io.spine.server.command.TestEventFactory in project core-java by SpineEventEngine.
the class AggregateRepositoryShould method log_error_when_event_reaction_fails.
@Test
public void log_error_when_event_reaction_fails() {
final FailingAggregateRepository repository = new FailingAggregateRepository();
boundedContext.register(repository);
final TestEventFactory factory = TestEventFactory.newInstance(getClass());
// Passing negative float value should cause an exception.
final EventEnvelope envelope = EventEnvelope.of(factory.createEvent(FloatValue.newBuilder().setValue(-412.0f).build()));
boundedContext.getEventBus().post(envelope.getOuterObject());
assertTrue(repository.isErrorLogged());
final RuntimeException lastException = repository.getLastException();
assertTrue(lastException instanceof HandlerMethodFailedException);
final HandlerMethodFailedException methodFailedException = (HandlerMethodFailedException) lastException;
assertEquals(envelope.getMessage(), methodFailedException.getDispatchedMessage());
assertEquals(envelope.getEventContext(), methodFailedException.getMessageContext());
final MessageEnvelope lastErrorEnvelope = repository.getLastErrorEnvelope();
assertNotNull(lastErrorEnvelope);
assertTrue(lastErrorEnvelope instanceof EventEnvelope);
assertEquals(envelope.getMessage(), lastErrorEnvelope.getMessage());
}
use of io.spine.server.command.TestEventFactory 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());
}
use of io.spine.server.command.TestEventFactory in project core-java by SpineEventEngine.
the class StorageRecords method sequenceFor.
/**
* Returns several records sorted by timestamp ascending.
*
* @param start the timestamp of first record.
*/
public static List<AggregateEventRecord> sequenceFor(ProjectId id, Timestamp start) {
final Duration delta = seconds(10);
final Timestamp timestamp2 = add(start, delta);
final Timestamp timestamp3 = add(timestamp2, delta);
final TestEventFactory eventFactory = newInstance(Given.class);
final Event e1 = eventFactory.createEvent(projectCreated(id, Given.projectName(id)), null, start);
final AggregateEventRecord record1 = StorageRecord.create(start, e1);
final Event e2 = eventFactory.createEvent(taskAdded(id), null, timestamp2);
final AggregateEventRecord record2 = StorageRecord.create(timestamp2, e2);
final Event e3 = eventFactory.createEvent(Given.EventMessage.projectStarted(id), null, timestamp3);
final AggregateEventRecord record3 = StorageRecord.create(timestamp3, e3);
return newArrayList(record1, record2, record3);
}
use of io.spine.server.command.TestEventFactory in project core-java by SpineEventEngine.
the class DelegatingEventDispatcherShould method expose_external_dispatcher_that_delegates_onError.
@Test
public void expose_external_dispatcher_that_delegates_onError() {
final ExternalMessageDispatcher<String> extMessageDispatcher = delegatingDispatcher.getExternalDispatcher();
final TestEventFactory factory = TestEventFactory.newInstance(getClass());
final StringValue eventMsg = newUuidValue();
final Event event = factory.createEvent(eventMsg);
final ExternalMessage externalMessage = ExternalMessages.of(event, BoundedContext.newName(getClass().getName()));
final ExternalMessageEnvelope externalMessageEnvelope = ExternalMessageEnvelope.of(externalMessage, eventMsg);
final RuntimeException exception = new RuntimeException("test external dispatcher delegating onError");
extMessageDispatcher.onError(externalMessageEnvelope, exception);
assertTrue(delegate.onErrorCalled());
}
Aggregations