Search in sources :

Example 6 with CommandContext

use of io.spine.base.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 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 7 with CommandContext

use of io.spine.base.CommandContext 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 8 with CommandContext

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

the class ProcessManagerRepository method dispatchCommand.

/**
     * Dispatches the command to a corresponding process manager.
     *
     * <p>If there is no stored process manager with such an ID,
     * a new process manager is created and stored after it handles the passed command.
     *
     * @param envelope a request to dispatch
     * @see CommandHandlingEntity#dispatchCommand(CommandEnvelope)
     */
@Override
public void dispatchCommand(CommandEnvelope envelope) {
    final Message commandMessage = envelope.getMessage();
    final CommandContext context = envelope.getCommandContext();
    final CommandClass commandClass = envelope.getMessageClass();
    checkCommandClass(commandClass);
    final I id = getIdFromCommandMessage.apply(commandMessage, context);
    final P manager = findOrCreate(id);
    final ProcManTransaction<?, ?, ?> tx = beginTransactionFor(manager);
    final List<Event> events = manager.dispatchCommand(envelope);
    store(manager);
    tx.commit();
    postEvents(events);
}
Also used : Message(com.google.protobuf.Message) CommandContext(io.spine.base.CommandContext) Event(io.spine.base.Event) CommandClass(io.spine.type.CommandClass)

Example 9 with CommandContext

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

the class TestEventFactory method newInstance.

public static TestEventFactory newInstance(Any producerId, TestActorRequestFactory requestFactory) {
    checkNotNull(requestFactory);
    final CommandContext commandContext = requestFactory.createCommandContext();
    final CommandId commandId = requestFactory.createCommandId();
    final Builder builder = EventFactory.newBuilder().setProducerId(producerId).setCommandContext(commandContext).setCommandId(commandId);
    final TestEventFactory result = new TestEventFactory(builder);
    return result;
}
Also used : CommandContext(io.spine.base.CommandContext) CommandId(io.spine.base.CommandId)

Example 10 with CommandContext

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

the class TimeTests method adjustTimestamp.

/**
     * Adjusts a timestamp in the context of the passed command.
     *
     * @return new command instance with the modified timestamp
     */
public static Command adjustTimestamp(Command command, Timestamp timestamp) {
    final CommandContext context = command.getContext();
    final ActorContext.Builder withTime = context.getActorContext().toBuilder().setTimestamp(timestamp);
    final Command.Builder commandBuilder = command.toBuilder().setContext(context.toBuilder().setActorContext(withTime));
    return commandBuilder.build();
}
Also used : CommandContext(io.spine.base.CommandContext) Command(io.spine.base.Command) ActorContext(io.spine.base.ActorContext)

Aggregations

CommandContext (io.spine.base.CommandContext)17 Command (io.spine.base.Command)6 Test (org.junit.Test)4 Message (com.google.protobuf.Message)3 StringValue (com.google.protobuf.StringValue)3 CommandId (io.spine.base.CommandId)3 ActorContext (io.spine.base.ActorContext)2 CommandBus (io.spine.server.commandbus.CommandBus)2 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 Internal (io.spine.annotation.Internal)1 Event (io.spine.base.Event)1 CommandFactory (io.spine.client.CommandFactory)1 AbstractVersionableEntity (io.spine.server.entity.AbstractVersionableEntity)1 EntityStateEnvelope (io.spine.server.entity.EntityStateEnvelope)1 VersionableEntity (io.spine.server.entity.VersionableEntity)1 CreateProject (io.spine.test.command.CreateProject)1 TestCommandContextFactory.createCommandContext (io.spine.testdata.TestCommandContextFactory.createCommandContext)1 ZoneOffset (io.spine.time.ZoneOffset)1