Search in sources :

Example 6 with TestEventFactory

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());
}
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 7 with TestEventFactory

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());
}
Also used : EventEnvelope(io.spine.core.EventEnvelope) FailingAggregateRepository(io.spine.server.aggregate.given.AggregateRepositoryTestEnv.FailingAggregateRepository) TestEventFactory(io.spine.server.command.TestEventFactory) HandlerMethodFailedException(io.spine.server.model.HandlerMethodFailedException) MessageEnvelope(io.spine.core.MessageEnvelope) Test(org.junit.Test)

Example 8 with TestEventFactory

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());
}
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)

Example 9 with TestEventFactory

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);
}
Also used : AggregateEventRecord(io.spine.server.aggregate.AggregateEventRecord) TestEventFactory(io.spine.server.command.TestEventFactory) Event(io.spine.core.Event) Duration(com.google.protobuf.Duration) Timestamp(com.google.protobuf.Timestamp)

Example 10 with TestEventFactory

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());
}
Also used : TestEventFactory(io.spine.server.command.TestEventFactory) Event(io.spine.core.Event) ExternalMessageEnvelope(io.spine.server.integration.ExternalMessageEnvelope) ExternalMessage(io.spine.server.integration.ExternalMessage) StringValue(com.google.protobuf.StringValue) Test(org.junit.Test)

Aggregations

TestEventFactory (io.spine.server.command.TestEventFactory)16 Event (io.spine.core.Event)10 Test (org.junit.Test)8 EventEnvelope (io.spine.core.EventEnvelope)4 StringValue (com.google.protobuf.StringValue)3 ProjectId (io.spine.test.aggregate.ProjectId)2 AggProjectArchived (io.spine.test.aggregate.event.AggProjectArchived)2 ProjectId (io.spine.test.integration.ProjectId)2 NullPointerTester (com.google.common.testing.NullPointerTester)1 Duration (com.google.protobuf.Duration)1 FloatValue (com.google.protobuf.FloatValue)1 Int32Value (com.google.protobuf.Int32Value)1 Timestamp (com.google.protobuf.Timestamp)1 TestActorRequestFactory (io.spine.client.TestActorRequestFactory)1 Command (io.spine.core.Command)1 MessageEnvelope (io.spine.core.MessageEnvelope)1 Version (io.spine.core.Version)1 GivenEvent (io.spine.core.given.GivenEvent)1 AggregateEventRecord (io.spine.server.aggregate.AggregateEventRecord)1 FailingAggregateRepository (io.spine.server.aggregate.given.AggregateRepositoryTestEnv.FailingAggregateRepository)1