Search in sources :

Example 6 with Command

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

the class CommandServiceShould method return_error_if_command_is_unsupported.

@Test
public void return_error_if_command_is_unsupported() {
    final TestActorRequestFactory factory = TestActorRequestFactory.newInstance(getClass());
    final Command unsupportedCmd = factory.createCommand(StringValue.getDefaultInstance());
    service.post(unsupportedCmd, responseObserver);
    final Throwable exception = responseObserver.getThrowable().getCause();
    assertEquals(UnsupportedCommandException.class, exception.getClass());
}
Also used : TestActorRequestFactory(io.spine.test.TestActorRequestFactory) Command(io.spine.base.Command) Test(org.junit.Test)

Example 7 with Command

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

the class ValidatorShould method validate_command_and_return_violations_if_context_is_NOT_valid.

@Test
public void validate_command_and_return_violations_if_context_is_NOT_valid() {
    final Command command = TestActorRequestFactory.newInstance(ValidatorShould.class).createCommand(createProjectMessage(), Time.getCurrentTime());
    final Command commandWithoutContext = command.toBuilder().setContext(CommandContext.getDefaultInstance()).build();
    final List<ConstraintViolation> violations = validator.validate(CommandEnvelope.of(commandWithoutContext));
    assertEquals(1, violations.size());
}
Also used : Command(io.spine.base.Command) ConstraintViolation(io.spine.validate.ConstraintViolation) Test(org.junit.Test)

Example 8 with Command

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

the class ValidatorShould method validate_command_and_return_nothing_if_it_is_valid.

@Test
public void validate_command_and_return_nothing_if_it_is_valid() {
    final Command cmd = Given.Command.createProject();
    final List<ConstraintViolation> violations = validator.validate(CommandEnvelope.of(cmd));
    assertEquals(0, violations.size());
}
Also used : Command(io.spine.base.Command) ConstraintViolation(io.spine.validate.ConstraintViolation) Test(org.junit.Test)

Example 9 with Command

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

the class AggregateRepository method dispatch.

/**
     * Dispatches the passed command to an aggregate.
     *
     * <p>The aggregate ID is obtained from the passed command.
     *
     * <p>The repository loads the aggregate by this ID, or creates a new aggregate
     * if there is no aggregate with such ID.
     *
     * @param envelope the envelope of the command to dispatch
     */
@Override
public void dispatch(final CommandEnvelope envelope) {
    final Command command = envelope.getCommand();
    final CommandOperation op = new CommandOperation(command) {

        @Override
        public void run() {
            final AggregateCommandEndpoint<I, A> commandEndpoint = createFor(AggregateRepository.this, envelope);
            commandEndpoint.execute();
            final Optional<A> processedAggregate = commandEndpoint.getAggregate();
            if (!processedAggregate.isPresent()) {
                throw new IllegalStateException("No aggregate loaded for command: " + command);
            }
            final A aggregate = processedAggregate.get();
            final List<Event> events = aggregate.getUncommittedEvents();
            store(aggregate);
            stand.post(aggregate, command.getContext());
            postEvents(events);
        }
    };
    op.execute();
}
Also used : Command(io.spine.base.Command) GetTargetIdFromCommand(io.spine.server.entity.idfunc.GetTargetIdFromCommand) CommandOperation(io.spine.server.tenant.CommandOperation) Event(io.spine.base.Event)

Example 10 with Command

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

the class ValidationFilter method isCommandValid.

private boolean isCommandValid(CommandEnvelope envelope, StreamObserver<Response> responseObserver) {
    final Command command = envelope.getCommand();
    final List<ConstraintViolation> violations = Validator.getInstance().validate(envelope);
    if (!violations.isEmpty()) {
        final CommandException invalidCommand = InvalidCommandException.onConstraintViolations(command, violations);
        commandBus.commandStore().storeWithError(command, invalidCommand);
        responseObserver.onError(invalidArgumentWithCause(invalidCommand, invalidCommand.getError()));
        return false;
    }
    return true;
}
Also used : Command(io.spine.base.Command) ConstraintViolation(io.spine.validate.ConstraintViolation)

Aggregations

Command (io.spine.base.Command)24 Test (org.junit.Test)14 StringValue (com.google.protobuf.StringValue)6 Message (com.google.protobuf.Message)4 ConstraintViolation (io.spine.validate.ConstraintViolation)4 Timestamp (com.google.protobuf.Timestamp)3 TestActorRequestFactory (io.spine.test.TestActorRequestFactory)3 TenantId (io.spine.users.TenantId)3 Event (io.spine.base.Event)2 Response (io.spine.base.Response)2 CommandOperation (io.spine.server.tenant.CommandOperation)2 EqualsTester (com.google.common.testing.EqualsTester)1 Any (com.google.protobuf.Any)1 StreamObserver (io.grpc.stub.StreamObserver)1 ActorContext (io.spine.base.ActorContext)1 CommandContext (io.spine.base.CommandContext)1 CommandId (io.spine.base.CommandId)1 Commands.getMessage (io.spine.base.Commands.getMessage)1 Failure (io.spine.base.Failure)1 StringChange (io.spine.change.StringChange)1