use of io.spine.validate.ValidationError 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.ValidationError in project core-java by SpineEventEngine.
the class StandShould method verifyInvalidTopicException.
private static void verifyInvalidTopicException(Topic invalidTopic, IllegalArgumentException e) {
final Throwable cause = e.getCause();
assertTrue(cause instanceof InvalidTopicException);
final InvalidTopicException topicException = (InvalidTopicException) cause;
assertEquals(invalidTopic, topicException.getRequest());
assertEquals(INVALID_TOPIC.getNumber(), topicException.getError().getCode());
final ValidationError validationError = topicException.getError().getValidationError();
assertTrue(Validate.isNotDefault(validationError));
}
use of io.spine.validate.ValidationError in project core-java by SpineEventEngine.
the class StandShould method verifyInvalidQueryException.
private static void verifyInvalidQueryException(Query invalidQuery, IllegalArgumentException e) {
final Throwable cause = e.getCause();
assertTrue(cause instanceof InvalidQueryException);
final InvalidQueryException queryException = (InvalidQueryException) cause;
assertEquals(invalidQuery, queryException.getRequest());
assertEquals(INVALID_QUERY.getNumber(), queryException.getError().getCode());
final ValidationError validationError = queryException.getError().getValidationError();
assertTrue(Validate.isNotDefault(validationError));
}
use of io.spine.validate.ValidationError in project core-java by SpineEventEngine.
the class StandShould method verifyInvalidSubscriptionException.
private static void verifyInvalidSubscriptionException(Subscription invalidSubscription, IllegalArgumentException e) {
final Throwable cause = e.getCause();
assertTrue(cause instanceof InvalidSubscriptionException);
final InvalidSubscriptionException exception = (InvalidSubscriptionException) cause;
assertEquals(invalidSubscription, exception.getRequest());
assertEquals(SubscriptionValidationError.INVALID_SUBSCRIPTION.getNumber(), exception.getError().getCode());
final ValidationError validationError = exception.getError().getValidationError();
assertTrue(Validate.isNotDefault(validationError));
}
Aggregations