Search in sources :

Example 1 with CommandId

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

the class CommandStoreShould method set_command_status_to_failure_when_handler_throws_failure.

@Test
public void set_command_status_to_failure_when_handler_throws_failure() {
    final TestFailure failure = new TestFailure();
    final Command command = givenThrowingHandler(failure);
    final CommandId commandId = command.getId();
    final Message commandMessage = getMessage(command);
    commandBus.post(command, responseObserver);
    // Check that the logging was called.
    verify(log).failureHandling(eq(failure), eq(commandMessage), eq(commandId));
    // Check that the status has the correct code,
    // and the failure matches the thrown failure.
    final TenantId tenantId = command.getContext().getActorContext().getTenantId();
    final ProcessingStatus status = getStatus(commandId, tenantId);
    assertEquals(CommandStatus.FAILURE, status.getCode());
    assertEquals(failure.toFailure(command).getMessage(), status.getFailure().getMessage());
}
Also used : TenantId(io.spine.users.TenantId) Commands.getMessage(io.spine.base.Commands.getMessage) CommandMessage.createProjectMessage(io.spine.server.commandbus.Given.CommandMessage.createProjectMessage) Message(com.google.protobuf.Message) Command(io.spine.base.Command) CommandId(io.spine.base.CommandId) Test(org.junit.Test)

Example 2 with CommandId

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

the class StorageShould method store_command_with_error.

// We get right after we store.
@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void store_command_with_error() {
    final Command command = Given.Command.createProject();
    final CommandId commandId = command.getId();
    final Error error = newError();
    storage.store(command, error);
    final CommandRecord record = read(commandId).get();
    checkRecord(record, command, ERROR);
    assertEquals(error, record.getStatus().getError());
}
Also used : Command(io.spine.base.Command) Error(io.spine.base.Error) CommandId(io.spine.base.CommandId) CommandRecord(io.spine.server.commandbus.CommandRecord) TenantAwareTest(io.spine.server.tenant.TenantAwareTest) Test(org.junit.Test)

Example 3 with CommandId

use of io.spine.base.CommandId 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 CreateProject 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().getFailure()));
            break;
        case ERROR:
            assertTrue(isNotDefault(record.getStatus().getError()));
            break;
        case FAILURE:
            assertTrue(isNotDefault(record.getStatus().getFailure()));
            break;
        case UNDEFINED:
        case UNRECOGNIZED:
            break;
    }
}
Also used : CommandContext(io.spine.base.CommandContext) CommandId(io.spine.base.CommandId) CreateProject(io.spine.test.command.CreateProject)

Example 4 with CommandId

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

the class Validator method validate.

/**
     * Validates a command checking that its required fields are valid and
     * validates a command message according to Spine custom protobuf options.
     *
     * @param envelope a command to validate
     * @return constraint violations found
     */
List<ConstraintViolation> validate(CommandEnvelope envelope) {
    final ImmutableList.Builder<ConstraintViolation> result = ImmutableList.builder();
    final Message message = envelope.getMessage();
    final CommandContext context = envelope.getCommandContext();
    final CommandId id = envelope.getCommandId();
    validateCommandId(id, result);
    validateMessage(message, result);
    validateContext(context, result);
    validateTargetId(message, result);
    return result.build();
}
Also used : Message(com.google.protobuf.Message) CommandContext(io.spine.base.CommandContext) ImmutableList(com.google.common.collect.ImmutableList) ConstraintViolation(io.spine.validate.ConstraintViolation) CommandId(io.spine.base.CommandId)

Example 5 with CommandId

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

the class CommandStore method getStatus.

/**
     * Obtains the processing status for the command with the passed ID.
     */
public ProcessingStatus getStatus(CommandId commandId) {
    final Func<CommandId, ProcessingStatus> func = new Func<CommandId, ProcessingStatus>(this) {

        @Override
        public ProcessingStatus apply(@Nullable CommandId input) {
            checkNotNull(input);
            final ProcessingStatus status = storage.getStatus(input);
            return status;
        }
    };
    return func.execute(commandId);
}
Also used : CommandId(io.spine.base.CommandId) ProcessingStatus(io.spine.server.commandbus.ProcessingStatus) Nullable(javax.annotation.Nullable)

Aggregations

CommandId (io.spine.base.CommandId)20 Test (org.junit.Test)7 Command (io.spine.base.Command)6 Message (com.google.protobuf.Message)5 Error (io.spine.base.Error)4 CommandRecord (io.spine.server.commandbus.CommandRecord)4 CommandContext (io.spine.base.CommandContext)3 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)3 TenantId (io.spine.users.TenantId)3 Commands.getMessage (io.spine.base.Commands.getMessage)2 CommandEnvelope (io.spine.envelope.CommandEnvelope)2 CommandMessage.createProjectMessage (io.spine.server.commandbus.Given.CommandMessage.createProjectMessage)2 AbstractEntity (io.spine.server.entity.AbstractEntity)2 Nullable (javax.annotation.Nullable)2 ImmutableList (com.google.common.collect.ImmutableList)1 Duration (com.google.protobuf.Duration)1 Timestamp (com.google.protobuf.Timestamp)1 CommandStatus (io.spine.base.CommandStatus)1 FailureThrowable (io.spine.base.FailureThrowable)1 Identifiers.idToString (io.spine.base.Identifiers.idToString)1