Search in sources :

Example 11 with ConstraintViolation

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());
}
Also used : Command(io.spine.base.Command) ConstraintViolation(io.spine.validate.ConstraintViolation) Test(org.junit.Test)

Example 12 with ConstraintViolation

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();
}
Also used : Message(com.google.protobuf.Message) CommandContext(io.spine.base.CommandContext) ImmutableList(com.google.common.collect.ImmutableList) ConstraintViolation(io.spine.validate.ConstraintViolation) CommandId(io.spine.base.CommandId)

Example 13 with ConstraintViolation

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);
        }
    }
}
Also used : Optional(com.google.common.base.Optional) ConstraintViolation(io.spine.validate.ConstraintViolation) Identifiers.idToString(io.spine.base.Identifiers.idToString)

Example 14 with ConstraintViolation

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;
}
Also used : Command(io.spine.base.Command) ConstraintViolation(io.spine.validate.ConstraintViolation)

Example 15 with ConstraintViolation

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());
}
Also used : Command(io.spine.core.Command) ConstraintViolation(io.spine.validate.ConstraintViolation) Test(org.junit.Test)

Aggregations

ConstraintViolation (io.spine.validate.ConstraintViolation)27 Test (org.junit.Test)18 Command (io.spine.core.Command)5 Command (io.spine.base.Command)4 Any (com.google.protobuf.Any)2 MessageInvalid (io.spine.core.MessageInvalid)2 CustomMessageRequiredStringFieldValue (io.spine.test.validate.msg.CustomMessageRequiredStringFieldValue)2 RequiredStringFieldValue (io.spine.test.validate.msg.RequiredStringFieldValue)2 Optional (com.google.common.base.Optional)1 ImmutableList (com.google.common.collect.ImmutableList)1 ByteString (com.google.protobuf.ByteString)1 Message (com.google.protobuf.Message)1 ProtocolMessageEnum (com.google.protobuf.ProtocolMessageEnum)1 CommandContext (io.spine.base.CommandContext)1 CommandId (io.spine.base.CommandId)1 Error (io.spine.base.Error)1 Identifiers.idToString (io.spine.base.Identifiers.idToString)1 Event (io.spine.core.Event)1 EnclosedMessageFieldValue (io.spine.test.validate.msg.EnclosedMessageFieldValue)1 MaxNumberFieldValue (io.spine.test.validate.msg.MaxNumberFieldValue)1