Search in sources :

Example 1 with CommandContext

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();
}
Also used : Set(java.util.Set) Message(com.google.protobuf.Message) TypeConverter.toMessage(io.spine.protobuf.TypeConverter.toMessage) CommandContext(io.spine.core.CommandContext) CommandEnvelope(io.spine.core.CommandEnvelope) BoundedContext(io.spine.server.BoundedContext) CommandBus(io.spine.server.commandbus.CommandBus) StringValue(com.google.protobuf.StringValue) Test(org.junit.Test)

Example 2 with CommandContext

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));
}
Also used : EventContext(io.spine.core.EventContext) CommandContext(io.spine.core.CommandContext) GivenEvent(io.spine.core.given.GivenEvent) Event(io.spine.core.Event)

Example 3 with CommandContext

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());
}
Also used : CommandContext(io.spine.core.CommandContext) CommandBus(io.spine.server.commandbus.CommandBus) StringValue(com.google.protobuf.StringValue) TenantAwareTest(io.spine.server.tenant.TenantAwareTest) Test(org.junit.Test)

Example 4 with CommandContext

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;
    }
}
Also used : CmdCreateProject(io.spine.test.command.CmdCreateProject) CommandContext(io.spine.core.CommandContext) CommandId(io.spine.core.CommandId)

Example 5 with CommandContext

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);
    }
}
Also used : CommandContext(io.spine.core.CommandContext) Message(com.google.protobuf.Message) Command(io.spine.core.Command) HandlerMethod(io.spine.server.model.HandlerMethod) Method(java.lang.reflect.Method) Exceptions.newIllegalArgumentException(io.spine.util.Exceptions.newIllegalArgumentException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

CommandContext (io.spine.core.CommandContext)24 Test (org.junit.Test)11 ActorContext (io.spine.core.ActorContext)8 Command (io.spine.core.Command)8 StringValue (com.google.protobuf.StringValue)4 TenantId (io.spine.core.TenantId)3 CommandBus (io.spine.server.commandbus.CommandBus)3 Message (com.google.protobuf.Message)2 Event (io.spine.core.Event)2 EventContext (io.spine.core.EventContext)2 RejectionContext (io.spine.core.RejectionContext)2 UserId (io.spine.core.UserId)2 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)2 InvalidProjectName (io.spine.test.reflect.ReflectRejections.InvalidProjectName)2 RjUpdateProjectName (io.spine.test.rejection.command.RjUpdateProjectName)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Duration (com.google.protobuf.Duration)1 Timestamp (com.google.protobuf.Timestamp)1 Internal (io.spine.annotation.Internal)1 CommandFactory (io.spine.client.CommandFactory)1