Search in sources :

Example 1 with CommandContext

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

the class ProcessManagerShould method create_router.

@Test
public void create_router() {
    final StringValue commandMessage = Wrapper.forString("create_router");
    final CommandContext commandContext = requestFactory.createCommandContext();
    processManager.setCommandBus(mock(CommandBus.class));
    final CommandRouter router = processManager.newRouterFor(commandMessage, commandContext);
    assertNotNull(router);
    assertEquals(commandMessage, getMessage(router.getSource()));
    assertEquals(commandContext, router.getSource().getContext());
}
Also used : CommandContext(io.spine.base.CommandContext) CommandBus(io.spine.server.commandbus.CommandBus) StringValue(com.google.protobuf.StringValue) TenantAwareTest(io.spine.server.tenant.TenantAwareTest) Test(org.junit.Test)

Example 2 with CommandContext

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

the class CommandFactoryShould method create_command_context.

@Test
public void create_command_context() {
    final TenantId tenantId = newTenantUuid();
    final UserId userId = newUserUuid();
    final ZoneOffset zoneOffset = ZoneOffsets.ofHours(-3);
    final int targetVersion = 100500;
    final CommandContext commandContext = CommandFactory.createContext(tenantId, userId, zoneOffset, targetVersion);
    final ActorContext actorContext = commandContext.getActorContext();
    assertEquals(tenantId, actorContext.getTenantId());
    assertEquals(userId, actorContext.getActor());
    assertEquals(zoneOffset, actorContext.getZoneOffset());
    assertEquals(targetVersion, commandContext.getTargetVersion());
}
Also used : TenantId(io.spine.users.TenantId) CommandContext(io.spine.base.CommandContext) UserId(io.spine.users.UserId) ZoneOffset(io.spine.time.ZoneOffset) ActorContext(io.spine.base.ActorContext) Test(org.junit.Test)

Example 3 with CommandContext

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

the class CommandFactory method create.

/**
     * Creates new {@code Command} with the passed message and target entity version.
     *
     * <p>The command contains a {@code CommandContext} instance with the current time.
     *
     * <p>The message passed is validated according to the constraints set in its Protobuf
     * definition. In case the message isn't valid, an {@linkplain ConstraintViolationThrowable
     * exception} is thrown.
     *
     * @param message       the command message
     * @param targetVersion the ID of the entity for applying commands if {@code null}
     *                      the commands can be applied to any entity
     * @return new command instance
     * @throws ConstraintViolationThrowable if the passed message does not satisfy the constraints
     *                                      set for it in its Protobuf definition
     */
public Command create(Message message, int targetVersion) throws ConstraintViolationThrowable {
    checkNotNull(message);
    checkNotNull(targetVersion);
    checkValid(message);
    final CommandContext context = createContext(targetVersion);
    final Command result = createCommand(message, context);
    return result;
}
Also used : CommandContext(io.spine.base.CommandContext) Command(io.spine.base.Command)

Example 4 with CommandContext

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

the class CommandFactory method createBasedOnContext.

/**
     * Creates new {@code Command} with the passed message, using the existing context.
     *
     * <p>The produced command is created with a {@code CommandContext} instance, copied from
     * the given one, but with the current time set as a context timestamp.
     *
     * @param message the command message
     * @param context the command context to use as a base for the new command
     * @return new command instance
     * @throws ConstraintViolationThrowable if the passed message does not satisfy the constraints
     *                                      set for it in its Protobuf definition
     */
@Internal
public Command createBasedOnContext(Message message, CommandContext context) throws ConstraintViolationThrowable {
    checkNotNull(message);
    checkNotNull(context);
    checkValid(message);
    final CommandContext newContext = contextBasedOn(context);
    final Command result = createCommand(message, newContext);
    return result;
}
Also used : CommandContext(io.spine.base.CommandContext) Command(io.spine.base.Command) Internal(io.spine.annotation.Internal)

Example 5 with CommandContext

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

the class CommandFactory method createContext.

/**
     * Creates a new command context with the current time.
     *
     * @param tenantId      the ID of the tenant or {@code null} for single-tenant applications
     * @param userId        the actor id
     * @param zoneOffset    the offset of the timezone in which the user works
     * @param targetVersion the the ID of the entity for applying commands
     * @return new {@code CommandContext}
     * @see CommandFactory#create(Message)
     */
@VisibleForTesting
static CommandContext createContext(@Nullable TenantId tenantId, UserId userId, ZoneOffset zoneOffset, int targetVersion) {
    checkNotNull(userId);
    checkNotNull(zoneOffset);
    checkNotNull(targetVersion);
    final CommandContext.Builder builder = newContextBuilder(tenantId, userId, zoneOffset);
    final CommandContext result = builder.setTargetVersion(targetVersion).build();
    return result;
}
Also used : CommandContext(io.spine.base.CommandContext) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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