Search in sources :

Example 21 with Ack

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

the class AggregateShould method create_single_event_for_a_pair_of_events_with_empty_for_a_rejection_react.

/**
 * Ensures that a {@linkplain io.spine.server.tuple.Pair pair} with an empty second optional
 * value returned from a reaction on a rejection stores a single event.
 *
 * <p>The rejection is fired by the {@link TaskAggregate#handle(AggReassignTask)
 * TaskAggregate.handle(AggReassignTask)}
 * and handled by the {@link TaskAggregate#on(Rejections.AggCannotReassignUnassignedTask)
 * TaskAggregate.on(AggCannotReassignUnassignedTask)}.
 */
@Test
public void create_single_event_for_a_pair_of_events_with_empty_for_a_rejection_react() {
    final BoundedContext boundedContext = newTaskBoundedContext();
    final TenantId tenantId = newTenantId();
    final Command command = command(reassignTask(), tenantId);
    final MemoizingObserver<Ack> observer = memoizingObserver();
    boundedContext.getCommandBus().post(command, observer);
    assertNull(observer.getError());
    final List<Ack> responses = observer.responses();
    assertSize(1, responses);
    final Ack response = responses.get(0);
    final io.spine.core.Status status = response.getStatus();
    final Error emptyError = Error.getDefaultInstance();
    assertEquals(emptyError, status.getError());
    final Rejection emptyRejection = Rejection.getDefaultInstance();
    assertEquals(emptyRejection, status.getRejection());
    final List<Event> events = readAllEvents(boundedContext, tenantId);
    assertSize(1, events);
    final Event reactionEvent = events.get(0);
    final TypeUrl userNotifiedType = TypeUrl.from(AggUserNotified.getDescriptor());
    assertEquals(typeUrlOf(reactionEvent), userNotifiedType);
}
Also used : Ack(io.spine.core.Ack) Error(io.spine.base.Error) TypeUrl(io.spine.type.TypeUrl) AggregateMessageDispatcher.dispatchRejection(io.spine.server.aggregate.AggregateMessageDispatcher.dispatchRejection) Rejection(io.spine.core.Rejection) 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) Event(io.spine.core.Event) BoundedContext(io.spine.server.BoundedContext) AggregateTestEnv.newTaskBoundedContext(io.spine.server.aggregate.given.aggregate.AggregateTestEnv.newTaskBoundedContext) Test(org.junit.Test)

Example 22 with Ack

use of io.spine.core.Ack 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 23 with Ack

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

the class IntegrationBusShould method emit_unsupported_external_message_exception_if_message_type_is_unknown.

@Test
public void emit_unsupported_external_message_exception_if_message_type_is_unknown() {
    final InMemoryTransportFactory transportFactory = InMemoryTransportFactory.newInstance();
    final BoundedContext boundedContext = contextWithTransport(transportFactory);
    final Event event = projectCreated();
    final BoundedContextName boundedContextName = BoundedContext.newName("External context ID");
    final ExternalMessage externalMessage = ExternalMessages.of(event, boundedContextName);
    final MemoizingObserver<Ack> observer = StreamObservers.memoizingObserver();
    boundedContext.getIntegrationBus().post(externalMessage, observer);
    final Error error = observer.firstResponse().getStatus().getError();
    assertFalse(Validate.isDefault(error));
    assertEquals(ExternalMessageValidationError.getDescriptor().getFullName(), error.getType());
    assertTrue(UNSUPPORTED_EXTERNAL_MESSAGE.getNumber() == error.getCode());
}
Also used : Ack(io.spine.core.Ack) InMemoryTransportFactory(io.spine.server.integration.memory.InMemoryTransportFactory) Event(io.spine.core.Event) Error(io.spine.base.Error) BoundedContext(io.spine.server.BoundedContext) BoundedContextName(io.spine.core.BoundedContextName) Test(org.junit.Test)

Example 24 with Ack

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

the class CommandOutputBus method doPost.

@Override
protected Ack doPost(E envelope) {
    final E enrichedEnvelope = enrich(envelope);
    final int dispatchersCalled = callDispatchers(enrichedEnvelope);
    final Any packedId = Identifier.pack(envelope.getId());
    checkState(dispatchersCalled != 0, format("Message %s has no dispatchers.", envelope.getMessage()));
    final Ack result = acknowledge(packedId);
    return result;
}
Also used : Ack(io.spine.core.Ack) Any(com.google.protobuf.Any)

Example 25 with Ack

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

the class AbstractCommandRouter method route.

/**
 * Creates a new command from the passed message and posts it
 * to the {@code CommandBus}.
 *
 * <p>This method waits till the posting of the command is finished.
 *
 * @param message the command message for the new command
 * @return the created and posted {@code Command}
 */
protected Command route(Message message) {
    final Command command = produceCommand(message);
    final SettableFuture<Ack> finishFuture = SettableFuture.create();
    final StreamObserver<Ack> observer = newAckingObserver(finishFuture);
    commandBus.post(command, observer);
    final Ack ack;
    // Wait till the call is completed.
    try {
        ack = finishFuture.get();
    } catch (InterruptedException | ExecutionException e) {
        throw new IllegalStateException(e);
    }
    checkSent(command, ack);
    return command;
}
Also used : Command(io.spine.core.Command) Ack(io.spine.core.Ack) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Ack (io.spine.core.Ack)29 Test (org.junit.Test)18 Command (io.spine.core.Command)15 Error (io.spine.base.Error)11 TenantId (io.spine.core.TenantId)11 CommandBus (io.spine.server.commandbus.CommandBus)7 Any (com.google.protobuf.Any)6 Event (io.spine.core.Event)6 AggregateMessageDispatcher.dispatchCommand (io.spine.server.aggregate.AggregateMessageDispatcher.dispatchCommand)6 AggregateTestEnv.newTenantId (io.spine.server.aggregate.given.aggregate.AggregateTestEnv.newTenantId)6 Rejection (io.spine.core.Rejection)5 BoundedContext (io.spine.server.BoundedContext)5 ProjectId (io.spine.test.aggregate.ProjectId)4 Status (io.spine.core.Status)3 AggregateMessageDispatcher.dispatchRejection (io.spine.server.aggregate.AggregateMessageDispatcher.dispatchRejection)3 IdempotencyGuardTestEnv.newProjectId (io.spine.server.aggregate.given.IdempotencyGuardTestEnv.newProjectId)3 IdempotencyGuardTestEnv.newTenantId (io.spine.server.aggregate.given.IdempotencyGuardTestEnv.newTenantId)3 AggregateTestEnv.newTaskBoundedContext (io.spine.server.aggregate.given.aggregate.AggregateTestEnv.newTaskBoundedContext)3 IgTestAggregate (io.spine.server.aggregate.given.aggregate.IgTestAggregate)3 Rejections.toRejection (io.spine.core.Rejections.toRejection)2