Search in sources :

Example 91 with Command

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

the class CommandServiceShould method return_error_status_if_command_is_unsupported.

@Test
public void return_error_status_if_command_is_unsupported() {
    final TestActorRequestFactory factory = TestActorRequestFactory.newInstance(getClass());
    final Command unsupportedCmd = factory.createCommand(StringValue.getDefaultInstance());
    service.post(unsupportedCmd, responseObserver);
    assertTrue(responseObserver.isCompleted());
    final Ack result = responseObserver.firstResponse();
    assertNotNull(result);
    assertTrue(isNotDefault(result));
    final Status status = result.getStatus();
    assertEquals(ERROR, status.getStatusCase());
    final Error error = status.getError();
    assertEquals(CommandValidationError.getDescriptor().getFullName(), error.getType());
}
Also used : TestActorRequestFactory(io.spine.client.TestActorRequestFactory) Status(io.spine.core.Status) Command(io.spine.core.Command) Ack(io.spine.core.Ack) Error(io.spine.base.Error) CommandValidationError(io.spine.core.CommandValidationError) Test(org.junit.Test)

Example 92 with Command

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

the class AggregateShould method propagate_RuntimeException_when_handler_throws.

@Test
public void propagate_RuntimeException_when_handler_throws() {
    ModelTests.clearModel();
    final FaultyAggregate faultyAggregate = new FaultyAggregate(ID, true, false);
    final Command command = Given.ACommand.createProject();
    try {
        dispatchCommand(faultyAggregate, env(command.getMessage()));
        failNotThrows();
    } catch (RuntimeException e) {
        // We need it for checking.
        @SuppressWarnings("ThrowableResultOfMethodCallIgnored") final Throwable cause = getRootCause(e);
        assertTrue(cause instanceof IllegalStateException);
        assertEquals(FaultyAggregate.BROKEN_HANDLER, cause.getMessage());
    }
}
Also used : Command(io.spine.core.Command) AggregateMessageDispatcher.dispatchCommand(io.spine.server.aggregate.AggregateMessageDispatcher.dispatchCommand) FaultyAggregate(io.spine.server.aggregate.given.aggregate.FaultyAggregate) Test(org.junit.Test)

Example 93 with Command

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

the class AggregateShould method propagate_RuntimeException_when_applier_throws.

@Test
public void propagate_RuntimeException_when_applier_throws() {
    ModelTests.clearModel();
    final FaultyAggregate faultyAggregate = new FaultyAggregate(ID, false, true);
    final Command command = Given.ACommand.createProject();
    try {
        dispatchCommand(faultyAggregate, env(command.getMessage()));
        failNotThrows();
    } catch (RuntimeException e) {
        @SuppressWarnings("ThrowableResultOfMethodCallIgnored") final Throwable // because we need it for checking.
        cause = getRootCause(e);
        assertTrue(cause instanceof IllegalStateException);
        assertEquals(FaultyAggregate.BROKEN_APPLIER, cause.getMessage());
    }
}
Also used : Command(io.spine.core.Command) AggregateMessageDispatcher.dispatchCommand(io.spine.server.aggregate.AggregateMessageDispatcher.dispatchCommand) FaultyAggregate(io.spine.server.aggregate.given.aggregate.FaultyAggregate) Test(org.junit.Test)

Example 94 with Command

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

the class AggregateShould method create_single_event_for_a_pair_of_events_with_empty_for_an_event_react.

/**
 * Ensures that a {@linkplain io.spine.server.tuple.Pair pair} with an empty second optional
 * value returned from a reaction on an event stores a single event.
 *
 * <p>The first event is produced while handling a command by the
 * {@link TaskAggregate#handle(AggAssignTask) TaskAggregate#handle(AggAssignTask)}.
 * Then as a reaction to this event a single event should be fired as part of the pair by
 * {@link TaskAggregate#on(AggTaskAssigned) TaskAggregate#on(AggTaskAssigned)}.
 */
@Test
public void create_single_event_for_a_pair_of_events_with_empty_for_an_event_react() {
    final BoundedContext boundedContext = newTaskBoundedContext();
    final TenantId tenantId = newTenantId();
    final Command command = command(assignTask(), 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(2, events);
    final Event sourceEvent = events.get(0);
    final TypeUrl taskAssignedType = TypeUrl.from(AggTaskAssigned.getDescriptor());
    assertEquals(typeUrlOf(sourceEvent), taskAssignedType);
    final Event reactionEvent = events.get(1);
    final TypeUrl userNotifiedType = TypeUrl.from(AggUserNotified.getDescriptor());
    assertEquals(typeUrlOf(reactionEvent), userNotifiedType);
}
Also used : Ack(io.spine.core.Ack) Error(io.spine.base.Error) TypeUrl(io.spine.type.TypeUrl) 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 95 with Command

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

the class AggregateShould method acknowledge_DuplicateCommandException_when_the_command_was_handled_since_last_snapshot.

@Test
public void acknowledge_DuplicateCommandException_when_the_command_was_handled_since_last_snapshot() {
    final TenantId tenantId = newTenantId();
    final Command createCommand = command(createProject, tenantId);
    final CommandBus commandBus = boundedContext.getCommandBus();
    final StreamObserver<Ack> noOpObserver = noOpObserver();
    final MemoizingObserver<Ack> memoizingObserver = memoizingObserver();
    commandBus.post(createCommand, noOpObserver);
    commandBus.post(createCommand, memoizingObserver);
    final List<Ack> responses = memoizingObserver.responses();
    final Ack ack = responses.get(0);
    assertTrue(ack.getStatus().hasError());
    final String errorType = DuplicateCommandException.class.getCanonicalName();
    assertEquals(errorType, ack.getStatus().getError().getType());
}
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) Ack(io.spine.core.Ack) CommandBus(io.spine.server.commandbus.CommandBus) Test(org.junit.Test)

Aggregations

Command (io.spine.core.Command)137 Test (org.junit.Test)87 Event (io.spine.core.Event)19 TenantId (io.spine.core.TenantId)17 Ack (io.spine.core.Ack)15 Message (com.google.protobuf.Message)14 Rejection (io.spine.core.Rejection)13 CommandEnvelope (io.spine.core.CommandEnvelope)11 Timestamp (com.google.protobuf.Timestamp)9 Error (io.spine.base.Error)9 CommandContext (io.spine.core.CommandContext)9 AggregateMessageDispatcher.dispatchCommand (io.spine.server.aggregate.AggregateMessageDispatcher.dispatchCommand)8 CommandRecord (io.spine.server.commandbus.CommandRecord)8 ProjectId (io.spine.test.aggregate.ProjectId)8 CommandBus (io.spine.server.commandbus.CommandBus)7 GivenEvent (io.spine.server.event.given.EventBusTestEnv.GivenEvent)7 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)7 Any (com.google.protobuf.Any)6 CommandId (io.spine.core.CommandId)6 AggregateTestEnv.newTenantId (io.spine.server.aggregate.given.aggregate.AggregateTestEnv.newTenantId)6