Search in sources :

Example 1 with ValidationError

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));
}
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 2 with ValidationError

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));
}
Also used : SubscriptionValidationError(io.spine.client.SubscriptionValidationError) ValidationError(io.spine.validate.ValidationError)

Example 3 with 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));
}
Also used : SubscriptionValidationError(io.spine.client.SubscriptionValidationError) ValidationError(io.spine.validate.ValidationError)

Example 4 with 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));
}
Also used : SubscriptionValidationError(io.spine.client.SubscriptionValidationError) ValidationError(io.spine.validate.ValidationError)

Aggregations

ValidationError (io.spine.validate.ValidationError)4 SubscriptionValidationError (io.spine.client.SubscriptionValidationError)3 ProtocolMessageEnum (com.google.protobuf.ProtocolMessageEnum)1 Error (io.spine.base.Error)1 ConstraintViolation (io.spine.validate.ConstraintViolation)1