use of io.spine.core.CommandContext in project core-java by SpineEventEngine.
the class CommandRouterOnErrorShould method throw_IllegalStateException_when_caught_error_when_posting.
@Test(expected = IllegalStateException.class)
public void throw_IllegalStateException_when_caught_error_when_posting() {
final BoundedContext boundedContext = BoundedContext.newBuilder().build();
final CommandBus commandBus = boundedContext.getCommandBus();
// Register dispatcher for `StringValue` message type.
// Fails each time of dispatch().
commandBus.register(new CommandDispatcher<Message>() {
@Override
public Set<CommandClass> getMessageClasses() {
return CommandClass.setOf(StringValue.class);
}
@Override
public Message dispatch(CommandEnvelope envelope) {
throw new IllegalStateException("I am faulty!");
}
@Override
public void onError(CommandEnvelope envelope, RuntimeException exception) {
// Do nothing.
}
});
final StringValue sourceMessage = toMessage(getClass().getSimpleName());
final CommandContext sourceContext = getRequestFactory().createCommandContext();
final CommandRouter router = createRouter(commandBus, sourceMessage, sourceContext);
router.addAll(getMessages());
router.routeAll();
}
use of io.spine.core.CommandContext in project core-java by SpineEventEngine.
the class ProcessManagerRepositoryShould method testDispatchEvent.
private void testDispatchEvent(Message eventMessage) {
final CommandContext commandContext = requestFactory.createCommandContext();
// EventContext should have CommandContext with appropriate TenantId to avoid usage
// of different storages during command and event dispatching.
final EventContext eventContextWithTenantId = GivenEvent.context().toBuilder().setCommandContext(commandContext).build();
final Event event = GivenEvent.withMessage(eventMessage).toBuilder().setContext(eventContextWithTenantId).build();
repository().dispatch(EventEnvelope.of(event));
assertTrue(TestProcessManager.processed(eventMessage));
}
use of io.spine.core.CommandContext in project core-java by SpineEventEngine.
the class ProcessManagerShould method create_router.
@Test
public void create_router() {
final StringValue commandMessage = toMessage("create_router");
final CommandContext commandContext = requestFactory.createCommandContext();
processManager.injectCommandBus(mock(CommandBus.class));
final CommandRouter router = processManager.newRouterFor(commandMessage, commandContext);
assertNotNull(router);
assertEquals(commandMessage, getMessage(router.getSource()));
assertEquals(commandContext, router.getSource().getContext());
}
use of io.spine.core.CommandContext in project core-java by SpineEventEngine.
the class CommandTestUtil method checkRecord.
static void checkRecord(CommandRecord record, Command cmd, CommandStatus statusExpected) {
final CommandContext context = cmd.getContext();
final CommandId commandId = cmd.getId();
final CmdCreateProject message = unpack(cmd.getMessage());
assertEquals(cmd.getMessage(), record.getCommand().getMessage());
assertTrue(record.getTimestamp().getSeconds() > 0);
assertEquals(message.getClass().getSimpleName(), record.getCommandType());
assertEquals(commandId, record.getCommandId());
assertEquals(statusExpected, record.getStatus().getCode());
assertEquals(context, record.getCommand().getContext());
switch(statusExpected) {
case RECEIVED:
case OK:
case SCHEDULED:
assertTrue(isDefault(record.getStatus().getError()));
assertTrue(isDefault(record.getStatus().getRejection()));
break;
case ERROR:
assertTrue(isNotDefault(record.getStatus().getError()));
break;
case REJECTED:
assertTrue(isNotDefault(record.getStatus().getRejection()));
break;
case UNDEFINED:
case UNRECOGNIZED:
break;
}
}
use of io.spine.core.CommandContext in project core-java by SpineEventEngine.
the class RejectionHandlerMethod method doInvoke.
/**
* Invokes the wrapped handler method to handle {@code rejectionMessage},
* {@code commandMessage} with the passed {@code context} of the {@code Command}.
*
* <p>Unlike the {@linkplain #invoke(Object, Message, Message) overloaded alternative method},
* this one may return some value.
*
* @param target the target object on which call the method
* @param rejectionMsg the rejection message to handle
* @param context the context of the rejection
* @return the result of the invocation
*/
@SuppressWarnings("OverlyLongMethod")
Object doInvoke(Object target, Message rejectionMsg, RejectionContext context) {
checkNotNull(target);
checkNotNull(rejectionMsg);
checkNotNull(context);
final Command command = context.getCommand();
final CommandContext commandContext = command.getContext();
try {
final Object output;
final Method method = getMethod();
Message commandMessage;
switch(kind) {
case REJECTION_MESSAGE_AWARE:
output = method.invoke(target, rejectionMsg);
break;
case REJECTION_CONTEXT_AWARE:
output = method.invoke(target, rejectionMsg, context);
break;
case COMMAND_CONTEXT_AWARE:
output = method.invoke(target, rejectionMsg, commandContext);
break;
case COMMAND_MESSAGE_AWARE:
commandMessage = Commands.getMessage(command);
output = method.invoke(target, rejectionMsg, commandMessage);
break;
case COMMAND_AWARE:
commandMessage = Commands.getMessage(command);
output = method.invoke(target, rejectionMsg, commandMessage, commandContext);
break;
default:
throw unsupported("Unsupported method kind encountered %s", kind.name());
}
return output;
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
throw whyFailed(target, rejectionMsg, context, e);
}
}
Aggregations