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