Search in sources :

Example 6 with CommandEnvelope

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

the class ProcessManagerRepositoryShould method throw_exception_if_dispatch_unknown_command.

@Test(expected = IllegalArgumentException.class)
public void throw_exception_if_dispatch_unknown_command() {
    final Command unknownCommand = requestFactory.createCommand(Int32Value.getDefaultInstance());
    final CommandEnvelope request = CommandEnvelope.of(unknownCommand);
    repository().dispatchCommand(request);
}
Also used : Command(io.spine.core.Command) CommandEnvelope(io.spine.core.CommandEnvelope) Test(org.junit.Test)

Example 7 with CommandEnvelope

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

the class ProcessManagerShould method testDispatchCommand.

private List<Event> testDispatchCommand(Message commandMsg) {
    final CommandEnvelope envelope = CommandEnvelope.of(requestFactory.command().create(commandMsg));
    final List<Event> events = dispatch(processManager, envelope);
    assertEquals(pack(commandMsg), processManager.getState());
    return events;
}
Also used : CommandEnvelope(io.spine.core.CommandEnvelope) Event(io.spine.core.Event)

Example 8 with CommandEnvelope

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

the class ProcessManagerShould method route_commands.

/**
 * Tests command routing.
 *
 * @see TestProcessManager#handle(PmStartProject, CommandContext)
 */
@Test
public void route_commands() {
    // Add dispatcher for the routed command. Otherwise the command would reject the command.
    final AddTaskDispatcher dispatcher = new AddTaskDispatcher();
    commandBus.register(dispatcher);
    processManager.injectCommandBus(commandBus);
    final List<Event> events = testDispatchCommand(startProject());
    // There's only one event generated.
    assertEquals(1, events.size());
    final Event event = events.get(0);
    // The producer of the event is our Process Manager.
    assertEquals(processManager.getId(), Events.getProducer(event.getContext()));
    final Message message = AnyPacker.unpack(event.getMessage());
    // The event type is CommandRouted.
    assertThat(message, instanceOf(CommandRouted.class));
    final CommandRouted commandRouted = (CommandRouted) message;
    // The source of the command is StartProject.
    assertThat(getMessage(commandRouted.getSource()), instanceOf(PmStartProject.class));
    final List<CommandEnvelope> dispatchedCommands = dispatcher.getCommands();
    assertSize(1, dispatchedCommands);
    final CommandEnvelope dispatchedCommand = dispatcher.getCommands().get(0);
    assertEquals(commandRouted.getProduced(0), dispatchedCommand.getCommand());
}
Also used : AddTaskDispatcher(io.spine.server.procman.given.ProcessManagerTestEnv.AddTaskDispatcher) TypeConverter.toMessage(io.spine.protobuf.TypeConverter.toMessage) Commands.getMessage(io.spine.core.Commands.getMessage) Message(com.google.protobuf.Message) PmStartProject(io.spine.test.procman.command.PmStartProject) Event(io.spine.core.Event) CommandEnvelope(io.spine.core.CommandEnvelope) TenantAwareTest(io.spine.server.tenant.TenantAwareTest) Test(org.junit.Test)

Example 9 with CommandEnvelope

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

the class CommandStoreShould method set_command_status_to_error_when_handler_throws_exception.

@Test
public void set_command_status_to_error_when_handler_throws_exception() {
    ModelTests.clearModel();
    final RuntimeException exception = new IllegalStateException("handler throws");
    final Command command = givenThrowingHandler(exception);
    final CommandEnvelope envelope = CommandEnvelope.of(command);
    commandBus.post(command, observer);
    // Check that the logging was called.
    verify(log).errorHandling(eq(exception), eq(envelope.getMessage()), eq(envelope.getId()));
    final String errorMessage = exception.getMessage();
    assertHasErrorStatusWithMessage(envelope, errorMessage);
}
Also used : Command(io.spine.core.Command) CommandEnvelope(io.spine.core.CommandEnvelope) Test(org.junit.Test)

Example 10 with CommandEnvelope

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

the class CommandStoreShould method set_command_status_to_error_when_dispatcher_throws.

@Test
public void set_command_status_to_error_when_dispatcher_throws() {
    final ThrowingDispatcher dispatcher = new ThrowingDispatcher();
    commandBus.register(dispatcher);
    final Command command = requestFactory.command().create(createProjectMessage());
    commandBus.post(command, observer);
    // Check that the logging was called.
    final CommandEnvelope envelope = CommandEnvelope.of(command);
    verify(log).errorHandling(dispatcher.exception, envelope.getMessage(), envelope.getId());
    // Check that the command status has the correct code,
    // and the error matches the thrown exception.
    assertHasErrorStatusWithMessage(envelope, dispatcher.exception.getMessage());
}
Also used : Command(io.spine.core.Command) CommandEnvelope(io.spine.core.CommandEnvelope) Test(org.junit.Test)

Aggregations

CommandEnvelope (io.spine.core.CommandEnvelope)40 Test (org.junit.Test)25 Command (io.spine.core.Command)11 Message (com.google.protobuf.Message)7 Error (io.spine.base.Error)5 TestActorRequestFactory (io.spine.client.TestActorRequestFactory)4 StringValue (com.google.protobuf.StringValue)3 CommandValidationError (io.spine.core.CommandValidationError)3 Event (io.spine.core.Event)3 BoundedContext (io.spine.server.BoundedContext)3 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)3 CommandId (io.spine.core.CommandId)2 Commands.getMessage (io.spine.core.Commands.getMessage)2 TypeConverter.toMessage (io.spine.protobuf.TypeConverter.toMessage)2 CommandBus (io.spine.server.commandbus.CommandBus)2 HandlerMethodFailedException (io.spine.server.model.HandlerMethodFailedException)2 AggCreateProject (io.spine.test.aggregate.command.AggCreateProject)2 ProjectId (io.spine.test.procman.ProjectId)2 TypeName (io.spine.type.TypeName)2 Set (java.util.Set)2