Search in sources :

Example 16 with ConstraintViolation

use of io.spine.validate.ConstraintViolation in project core-java by SpineEventEngine.

the class CommandValidatorViolationCheckShould 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.ACommand.createProject();
    final List<ConstraintViolation> violations = inspect(CommandEnvelope.of(cmd));
    assertEquals(0, violations.size());
}
Also used : Command(io.spine.core.Command) ConstraintViolation(io.spine.validate.ConstraintViolation) Test(org.junit.Test)

Example 17 with ConstraintViolation

use of io.spine.validate.ConstraintViolation in project core-java by SpineEventEngine.

the class CommandValidatorViolationCheckShould 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(getClass()).createCommand(createProjectMessage(), Time.getCurrentTime());
    final Command commandWithoutContext = command.toBuilder().setContext(CommandContext.getDefaultInstance()).build();
    final List<ConstraintViolation> violations = inspect(CommandEnvelope.of(commandWithoutContext));
    assertEquals(1, violations.size());
}
Also used : Command(io.spine.core.Command) ConstraintViolation(io.spine.validate.ConstraintViolation) Test(org.junit.Test)

Example 18 with ConstraintViolation

use of io.spine.validate.ConstraintViolation in project core-java by SpineEventEngine.

the class RequestValidator method validateMessage.

/**
 * Checks whether the {@code Message} of the given request conforms the constraints
 *
 * @param request the request message to validate.
 * @return an instance of exception,
 *         or {@code Optional.absent()} if the request message is valid.
 */
private Optional<InvalidRequestException> validateMessage(M request) {
    final List<ConstraintViolation> violations = MessageValidator.newInstance().validate(request);
    if (violations.isEmpty()) {
        return Optional.absent();
    }
    final ValidationError validationError = ValidationError.newBuilder().addAllConstraintViolation(violations).build();
    final ProtocolMessageEnum errorCode = getInvalidMessageErrorCode();
    final String typeName = errorCode.getDescriptorForType().getFullName();
    final String errorTextTemplate = getErrorText(request);
    final String errorText = format("%s %s", errorTextTemplate, toText(violations));
    final Error.Builder errorBuilder = Error.newBuilder().setType(typeName).setCode(errorCode.getNumber()).setValidationError(validationError).setMessage(errorText);
    final Error error = errorBuilder.build();
    return Optional.of(onInvalidMessage(formatExceptionMessage(request), request, error));
}
Also used : ProtocolMessageEnum(com.google.protobuf.ProtocolMessageEnum) ConstraintViolation(io.spine.validate.ConstraintViolation) ValidationError(io.spine.validate.ValidationError) Error(io.spine.base.Error) ValidationError(io.spine.validate.ValidationError)

Example 19 with ConstraintViolation

use of io.spine.validate.ConstraintViolation in project core-java by SpineEventEngine.

the class MessageValidatorShould method provide_one_valid_violation_if_time_is_invalid.

@Test
public void provide_one_valid_violation_if_time_is_invalid() {
    final TimeInFutureFieldValue invalidMsg = TimeInFutureFieldValue.newBuilder().setValue(getPast()).build();
    validate(invalidMsg);
    assertEquals(1, violations.size());
    final ConstraintViolation violation = firstViolation();
    assertEquals("Timestamp value must be in the future.", format(firstViolation().getMsgFormat(), firstViolation().getParam(0)));
    assertFieldPathIs(violation, VALUE);
    assertTrue(violation.getViolationList().isEmpty());
}
Also used : ConstraintViolation(io.spine.validate.ConstraintViolation) TimeInFutureFieldValue(io.spine.test.validate.msg.TimeInFutureFieldValue) Test(org.junit.Test)

Example 20 with ConstraintViolation

use of io.spine.validate.ConstraintViolation in project core-java by SpineEventEngine.

the class MessageValidatorShould method provide_custom_invalid_field_message_if_specified.

@Test
public void provide_custom_invalid_field_message_if_specified() {
    validate(EnclosedMessageFieldValueWithCustomInvalidMessage.getDefaultInstance());
    assertSize(1, violations);
    final ConstraintViolation violation = firstViolation();
    assertEquals("Custom error", violation.getMsgFormat());
}
Also used : 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