Search in sources :

Example 21 with CommandContext

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

the class StandPostShould method use_delivery_from_builder.

@SuppressWarnings("MagicNumber")
@Test
public void use_delivery_from_builder() {
    final StandUpdateDelivery delivery = spy(new StandUpdateDelivery() {

        @Override
        protected boolean shouldPostponeDelivery(EntityStateEnvelope deliverable, Stand consumer) {
            return false;
        }
    });
    final Stand.Builder builder = Stand.newBuilder().setDelivery(delivery);
    final Stand stand = builder.build();
    Assert.assertNotNull(stand);
    final Object id = Identifier.newUuid();
    final StringValue state = StringValue.getDefaultInstance();
    final VersionableEntity entity = mock(AbstractVersionableEntity.class);
    when(entity.getState()).thenReturn(state);
    when(entity.getId()).thenReturn(id);
    when(entity.getVersion()).thenReturn(newVersion(17, Time.getCurrentTime()));
    final CommandContext context = requestFactory.createCommandContext();
    stand.post(context.getActorContext().getTenantId(), entity);
    final EntityStateEnvelope envelope = EntityStateEnvelope.of(entity, context.getActorContext().getTenantId());
    verify(delivery).deliverNow(eq(envelope), eq(Consumers.idOf(stand)));
}
Also used : EntityStateEnvelope(io.spine.server.entity.EntityStateEnvelope) CommandContext(io.spine.core.CommandContext) StringValue(com.google.protobuf.StringValue) AbstractVersionableEntity(io.spine.server.entity.AbstractVersionableEntity) VersionableEntity(io.spine.server.entity.VersionableEntity) Test(org.junit.Test)

Example 22 with CommandContext

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

the class CommandScheduler method setSchedule.

/**
 * Updates {@linkplain CommandContext.Schedule command schedule}.
 *
 * @param command        a command to update
 * @param delay          a {@linkplain CommandContext.Schedule#getDelay() delay} to set
 * @param schedulingTime the time when the command was scheduled by the {@code CommandScheduler}
 * @return an updated command
 */
static Command setSchedule(Command command, Duration delay, Timestamp schedulingTime) {
    checkNotNull(command);
    checkNotNull(delay);
    checkNotNull(schedulingTime);
    checkValid(schedulingTime);
    final CommandContext context = command.getContext();
    final CommandContext.Schedule scheduleUpdated = context.getSchedule().toBuilder().setDelay(delay).build();
    final CommandContext contextUpdated = context.toBuilder().setSchedule(scheduleUpdated).build();
    final Command.SystemProperties sysProps = command.getSystemProperties().toBuilder().setSchedulingTime(schedulingTime).build();
    final Command result = command.toBuilder().setContext(contextUpdated).setSystemProperties(sysProps).build();
    return result;
}
Also used : CommandContext(io.spine.core.CommandContext) Command(io.spine.core.Command)

Example 23 with CommandContext

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

the class CommandFactory method create.

/**
 * Creates a new {@link Command} with the given message.
 *
 * @param message the command message
 * @return new command instance
 * @throws ValidationException if the passed message does not satisfy the constraints
 *                             set for it in its Protobuf definition
 */
public Command create(Message message) throws ValidationException {
    checkNotNull(message);
    checkValid(message);
    final CommandContext context = createContext();
    final Command result = createCommand(message, context);
    return result;
}
Also used : CommandContext(io.spine.core.CommandContext) Command(io.spine.core.Command)

Example 24 with CommandContext

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

the class GivenCommandContextShould method create_with_actor_and_time.

@Test
public void create_with_actor_and_time() {
    final UserId actorId = newUuid();
    final Timestamp when = add(getCurrentTime(), fromMinutes(42));
    final CommandContext context = GivenCommandContext.withActorAndTime(actorId, when);
    checkValid(context);
    final ActorContext actualActorContext = context.getActorContext();
    assertEquals(actorId, actualActorContext.getActor());
    assertEquals(when, actualActorContext.getTimestamp());
}
Also used : CommandContext(io.spine.core.CommandContext) UserId(io.spine.core.UserId) Timestamp(com.google.protobuf.Timestamp) ActorContext(io.spine.core.ActorContext) Test(org.junit.Test)

Example 25 with CommandContext

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

the class CommandTest method adjustTimestamp.

/**
 * Adjusts a timestamp in the context of the passed command.
 *
 * @return new command instance with the modified timestamp
 */
private 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.core.CommandContext) Command(io.spine.core.Command) ActorContext(io.spine.core.ActorContext)

Aggregations

CommandContext (io.spine.core.CommandContext)25 Test (org.junit.Test)11 ActorContext (io.spine.core.ActorContext)8 Command (io.spine.core.Command)8 StringValue (com.google.protobuf.StringValue)4 Message (com.google.protobuf.Message)3 TenantId (io.spine.core.TenantId)3 CommandBus (io.spine.server.commandbus.CommandBus)3 Event (io.spine.core.Event)2 EventContext (io.spine.core.EventContext)2 RejectionContext (io.spine.core.RejectionContext)2 UserId (io.spine.core.UserId)2 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)2 InvalidProjectName (io.spine.test.reflect.ReflectRejections.InvalidProjectName)2 RjUpdateProjectName (io.spine.test.rejection.command.RjUpdateProjectName)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Duration (com.google.protobuf.Duration)1 Timestamp (com.google.protobuf.Timestamp)1 Internal (io.spine.annotation.Internal)1 CommandFactory (io.spine.client.CommandFactory)1