Search in sources :

Example 61 with Command

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

the class IdempotencyGuardShould method not_throw_if_another_command_was_handled.

@Test
public void not_throw_if_another_command_was_handled() {
    final TenantId tenantId = newTenantId();
    final ProjectId projectId = newProjectId();
    final Command createCommand = command(createProject(projectId), tenantId);
    final Command startCommand = command(startProject(projectId), tenantId);
    final CommandBus commandBus = boundedContext.getCommandBus();
    final StreamObserver<Ack> noOpObserver = noOpObserver();
    commandBus.post(createCommand, noOpObserver);
    final IgTestAggregate aggregate = repository.loadAggregate(tenantId, projectId);
    final IdempotencyGuard guard = new IdempotencyGuard(aggregate);
    guard.check(of(startCommand));
}
Also used : IdempotencyGuardTestEnv.newTenantId(io.spine.server.aggregate.given.IdempotencyGuardTestEnv.newTenantId) TenantId(io.spine.core.TenantId) Command(io.spine.core.Command) IgTestAggregate(io.spine.server.aggregate.given.aggregate.IgTestAggregate) IdempotencyGuardTestEnv.newProjectId(io.spine.server.aggregate.given.IdempotencyGuardTestEnv.newProjectId) ProjectId(io.spine.test.aggregate.ProjectId) Ack(io.spine.core.Ack) CommandBus(io.spine.server.commandbus.CommandBus) Test(org.junit.Test)

Example 62 with Command

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

the class IdempotencyGuardShould method not_throw_when_the_command_was_handled_but_the_snapshot_was_made.

@Test
public void not_throw_when_the_command_was_handled_but_the_snapshot_was_made() {
    repository.setSnapshotTrigger(1);
    final TenantId tenantId = newTenantId();
    final ProjectId projectId = newProjectId();
    final Command createCommand = command(createProject(projectId), tenantId);
    final CommandBus commandBus = boundedContext.getCommandBus();
    final StreamObserver<Ack> noOpObserver = noOpObserver();
    commandBus.post(createCommand, noOpObserver);
    final IgTestAggregate aggregate = repository.loadAggregate(tenantId, projectId);
    final IdempotencyGuard guard = new IdempotencyGuard(aggregate);
    guard.check(of(createCommand));
}
Also used : IdempotencyGuardTestEnv.newTenantId(io.spine.server.aggregate.given.IdempotencyGuardTestEnv.newTenantId) TenantId(io.spine.core.TenantId) Command(io.spine.core.Command) IgTestAggregate(io.spine.server.aggregate.given.aggregate.IgTestAggregate) IdempotencyGuardTestEnv.newProjectId(io.spine.server.aggregate.given.IdempotencyGuardTestEnv.newProjectId) ProjectId(io.spine.test.aggregate.ProjectId) Ack(io.spine.core.Ack) CommandBus(io.spine.server.commandbus.CommandBus) Test(org.junit.Test)

Example 63 with Command

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

the class AggregateMessageDeliveryTestEnv method createProject.

public static Command createProject() {
    final ProjectId projectId = projectId();
    final Command command = createCommand(AggCreateProject.newBuilder().setName("A project" + projectId).setProjectId(projectId).build());
    return command;
}
Also used : Command(io.spine.core.Command) ProjectId(io.spine.test.aggregate.ProjectId)

Example 64 with Command

use of io.spine.core.Command 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)

Example 65 with Command

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

the class AggregateTestEnv method cannotModifyDeletedEntity.

public static RejectionEnvelope cannotModifyDeletedEntity(Class<? extends Message> commandMessageCls) {
    final CannotModifyDeletedEntity rejectionMsg = CannotModifyDeletedEntity.newBuilder().build();
    final Command command = io.spine.server.commandbus.Given.ACommand.withMessage(Sample.messageOfType(commandMessageCls));
    final Rejection rejection = Rejections.createRejection(rejectionMsg, command);
    return RejectionEnvelope.of(rejection);
}
Also used : Rejection(io.spine.core.Rejection) CannotModifyDeletedEntity(io.spine.server.entity.rejection.StandardRejections.CannotModifyDeletedEntity) Command(io.spine.core.Command)

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