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);
}
}
use of io.spine.core.CommandContext in project core-java by SpineEventEngine.
the class GivenCommandContext method withActorAndTime.
/**
* Creates a new {@link CommandContext} instance based upon given actor and creation date.
*
* @return a new {@code CommandContext} instance
*/
public static CommandContext withActorAndTime(UserId actor, Timestamp when) {
final TenantId tenantId = GivenTenantId.newUuid();
final ActorContext.Builder actorContext = ActorContext.newBuilder().setActor(actor).setTimestamp(when).setZoneOffset(UTC).setTenantId(tenantId);
final CommandContext.Builder builder = CommandContext.newBuilder().setActorContext(actorContext);
return builder.build();
}
Aggregations