Search in sources :

Example 16 with CommandBus

use of io.spine.server.commandbus.CommandBus in project core-java by SpineEventEngine.

the class AggregateShould method acknowledge_DuplicateCommandException_when_the_command_was_handled_since_last_snapshot.

@Test
public void acknowledge_DuplicateCommandException_when_the_command_was_handled_since_last_snapshot() {
    final TenantId tenantId = newTenantId();
    final Command createCommand = command(createProject, tenantId);
    final CommandBus commandBus = boundedContext.getCommandBus();
    final StreamObserver<Ack> noOpObserver = noOpObserver();
    final MemoizingObserver<Ack> memoizingObserver = memoizingObserver();
    commandBus.post(createCommand, noOpObserver);
    commandBus.post(createCommand, memoizingObserver);
    final List<Ack> responses = memoizingObserver.responses();
    final Ack ack = responses.get(0);
    assertTrue(ack.getStatus().hasError());
    final String errorType = DuplicateCommandException.class.getCanonicalName();
    assertEquals(errorType, ack.getStatus().getError().getType());
}
Also used : AggregateTestEnv.newTenantId(io.spine.server.aggregate.given.aggregate.AggregateTestEnv.newTenantId) TenantId(io.spine.core.TenantId) Command(io.spine.core.Command) AggregateMessageDispatcher.dispatchCommand(io.spine.server.aggregate.AggregateMessageDispatcher.dispatchCommand) Ack(io.spine.core.Ack) CommandBus(io.spine.server.commandbus.CommandBus) Test(org.junit.Test)

Example 17 with CommandBus

use of io.spine.server.commandbus.CommandBus in project core-java by SpineEventEngine.

the class IdempotencyGuardShould method throw_DuplicateCommandException_when_the_command_was_handled_since_last_snapshot.

@Test(expected = DuplicateCommandException.class)
public void throw_DuplicateCommandException_when_the_command_was_handled_since_last_snapshot() {
    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 18 with CommandBus

use of io.spine.server.commandbus.CommandBus in project core-java by SpineEventEngine.

the class ProcessManager method newRouterFor.

/**
 * Creates a new {@link CommandRouter}.
 *
 * <p>A {@code CommandRouter} allows to create and post one or more commands
 * in response to a command received by the {@code ProcessManager}.
 *
 * <p>A typical usage looks like this:
 *
 * <pre>
 *     {@literal @}Assign
 *     CommandRouted on(MyCommand message, CommandContext context) {
 *         // Create new command messages here.
 *         return newRouterFor(message, context)
 *                  .add(messageOne)
 *                  .add(messageTwo)
 *                  .routeAll();
 *     }
 * </pre>
 *
 * <p>The routed commands are created on behalf of the actor of the original command.
 * That is, the {@code actor} and {@code zoneOffset} fields of created {@code CommandContext}
 * instances will be the same as in the incoming command.
 *
 * @param  commandMessage the source command message
 * @param  commandContext the context of the source command
 * @return new {@code CommandRouter}
 */
protected CommandRouter newRouterFor(Message commandMessage, CommandContext commandContext) {
    checkNotNull(commandMessage);
    checkNotNull(commandContext);
    final CommandBus commandBus = ensureCommandBus();
    final CommandRouter router = new CommandRouter(commandBus, commandMessage, commandContext);
    return router;
}
Also used : CommandBus(io.spine.server.commandbus.CommandBus)

Aggregations

CommandBus (io.spine.server.commandbus.CommandBus)18 Test (org.junit.Test)10 Ack (io.spine.core.Ack)7 Command (io.spine.core.Command)7 TenantId (io.spine.core.TenantId)6 ProjectId (io.spine.test.aggregate.ProjectId)4 AggregateMessageDispatcher.dispatchCommand (io.spine.server.aggregate.AggregateMessageDispatcher.dispatchCommand)3 IdempotencyGuardTestEnv.newProjectId (io.spine.server.aggregate.given.IdempotencyGuardTestEnv.newProjectId)3 IdempotencyGuardTestEnv.newTenantId (io.spine.server.aggregate.given.IdempotencyGuardTestEnv.newTenantId)3 AggregateTestEnv.newTenantId (io.spine.server.aggregate.given.aggregate.AggregateTestEnv.newTenantId)3 IgTestAggregate (io.spine.server.aggregate.given.aggregate.IgTestAggregate)3 StringValue (com.google.protobuf.StringValue)2 CommandEnvelope (io.spine.core.CommandEnvelope)2 Event (io.spine.core.Event)2 BoundedContext (io.spine.server.BoundedContext)2 TestAggregate (io.spine.server.aggregate.given.aggregate.TestAggregate)2 Set (java.util.Set)2 Message (com.google.protobuf.Message)1 StreamObserver (io.grpc.stub.StreamObserver)1 Command (io.spine.base.Command)1