use of io.spine.validate.ConstraintViolation 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.validate.ConstraintViolation 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();
}
use of io.spine.validate.ConstraintViolation in project core-java by SpineEventEngine.
the class Validator method validateTargetId.
private static void validateTargetId(Message message, ImmutableList.Builder<ConstraintViolation> result) {
final Optional targetId = GetTargetIdFromCommand.asOptional(message);
if (targetId.isPresent()) {
final String targetIdString = idToString(targetId.get());
if (targetIdString.equals(EMPTY_ID)) {
final ConstraintViolation violation = newConstraintViolation(COMMAND_TARGET_ENTITY_ID_CANNOT_BE_EMPTY_OR_BLANK);
result.add(violation);
}
}
}
use of io.spine.validate.ConstraintViolation 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;
}
use of io.spine.validate.ConstraintViolation in project core-java by SpineEventEngine.
the class CommandValidatorViolationCheckShould method not_allow_commands_without_IDs.
@Test
public void not_allow_commands_without_IDs() {
final Command cmd = Given.ACommand.createProject();
final Command unidentifiableCommand = cmd.toBuilder().setId(CommandId.getDefaultInstance()).build();
final List<ConstraintViolation> violations = inspect(CommandEnvelope.of(unidentifiableCommand));
assertEquals(1, violations.size());
}
Aggregations