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());
}
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());
}
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());
}
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();
}
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;
}
Aggregations