Search in sources :

Example 1 with ConstraintViolation

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

the class EventBus method validateMessage.

@Override
protected boolean validateMessage(Message event, StreamObserver<Response> responseObserver) {
    final EventClass eventClass = EventClass.of(event);
    if (isUnsupportedEvent(eventClass)) {
        final UnsupportedEventException unsupportedEvent = new UnsupportedEventException(event);
        responseObserver.onError(invalidArgumentWithCause(unsupportedEvent, unsupportedEvent.getError()));
        return false;
    }
    final List<ConstraintViolation> violations = eventValidator.validate(event);
    if (!violations.isEmpty()) {
        final InvalidEventException invalidEvent = InvalidEventException.onConstraintViolations(event, violations);
        responseObserver.onError(invalidArgumentWithCause(invalidEvent, invalidEvent.getError()));
        return false;
    }
    return true;
}
Also used : EventClass(io.spine.type.EventClass) ConstraintViolation(io.spine.validate.ConstraintViolation)

Example 2 with ConstraintViolation

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

the class MessageValidatorShould method provide_one_valid_violation_if_number_is_greater_than_max.

@Test
public void provide_one_valid_violation_if_number_is_greater_than_max() {
    final MaxNumberFieldValue msg = MaxNumberFieldValue.newBuilder().setValue(GREATER_THAN_MAX).build();
    validate(msg);
    assertEquals(1, violations.size());
    final ConstraintViolation violation = firstViolation();
    assertEquals(GREATER_MAX_MSG, format(violation.getMsgFormat(), violation.getParam(0)));
    assertFieldPathIs(violation, VALUE);
    assertTrue(violation.getViolationList().isEmpty());
}
Also used : MaxNumberFieldValue(io.spine.test.validate.msg.MaxNumberFieldValue) ConstraintViolation(io.spine.validate.ConstraintViolation) Test(org.junit.Test)

Example 3 with ConstraintViolation

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

the class MessageValidatorShould method provide_valid_violations_if_enclosed_message_field_is_not_valid.

@Test
public void provide_valid_violations_if_enclosed_message_field_is_not_valid() {
    final RequiredStringFieldValue enclosedMsg = RequiredStringFieldValue.getDefaultInstance();
    final EnclosedMessageFieldValue msg = EnclosedMessageFieldValue.newBuilder().setOuterMsgField(enclosedMsg).build();
    validate(msg);
    assertEquals(1, violations.size());
    final ConstraintViolation violation = firstViolation();
    assertEquals("Message must have valid properties.", violation.getMsgFormat());
    assertFieldPathIs(violation, OUTER_MSG_FIELD);
    final List<ConstraintViolation> innerViolations = violation.getViolationList();
    assertEquals(1, innerViolations.size());
    final ConstraintViolation innerViolation = innerViolations.get(0);
    assertEquals(NO_VALUE_MSG, innerViolation.getMsgFormat());
    assertFieldPathIs(innerViolation, OUTER_MSG_FIELD, VALUE);
    assertTrue(innerViolation.getViolationList().isEmpty());
}
Also used : ConstraintViolation(io.spine.validate.ConstraintViolation) EnclosedMessageFieldValue(io.spine.test.validate.msg.EnclosedMessageFieldValue) CustomMessageRequiredStringFieldValue(io.spine.test.validate.msg.CustomMessageRequiredStringFieldValue) RequiredStringFieldValue(io.spine.test.validate.msg.RequiredStringFieldValue) Test(org.junit.Test)

Example 4 with ConstraintViolation

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

the class MessageValidatorShould method assertIsValid.

private void assertIsValid(boolean isValid) {
    if (isValid) {
        assertTrue(violations.isEmpty());
    } else {
        assertFalse(violations.isEmpty());
        for (ConstraintViolation violation : violations) {
            final String format = violation.getMsgFormat();
            assertTrue(!format.isEmpty());
            final boolean noParams = violation.getParamList().isEmpty();
            if (format.contains("%s")) {
                assertFalse(noParams);
            } else {
                assertTrue(noParams);
            }
            assertFalse(violation.getFieldPath().getFieldNameList().isEmpty());
        }
    }
}
Also used : ConstraintViolation(io.spine.validate.ConstraintViolation) ByteString(com.google.protobuf.ByteString)

Example 5 with ConstraintViolation

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

the class MessageValidatorShould method provide_one_valid_violation_if_string_does_not_match_to_regex_pattern.

@Test
public void provide_one_valid_violation_if_string_does_not_match_to_regex_pattern() {
    final PatternStringFieldValue msg = PatternStringFieldValue.newBuilder().setEmail("invalid.email").build();
    validate(msg);
    assertEquals(1, violations.size());
    final ConstraintViolation violation = firstViolation();
    assertEquals("String must match the regular expression '%s'.", violation.getMsgFormat());
    assertEquals("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$", firstViolation().getParam(0));
    assertFieldPathIs(violation, "email");
    assertTrue(violation.getViolationList().isEmpty());
}
Also used : PatternStringFieldValue(io.spine.test.validate.msg.PatternStringFieldValue) ConstraintViolation(io.spine.validate.ConstraintViolation) Test(org.junit.Test)

Aggregations

ConstraintViolation (io.spine.validate.ConstraintViolation)26 Test (org.junit.Test)18 Command (io.spine.core.Command)5 Command (io.spine.base.Command)4 Any (com.google.protobuf.Any)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 MessageInvalid (io.spine.core.MessageInvalid)1 EnclosedMessageFieldValue (io.spine.test.validate.msg.EnclosedMessageFieldValue)1 MaxNumberFieldValue (io.spine.test.validate.msg.MaxNumberFieldValue)1 MinNumberFieldValue (io.spine.test.validate.msg.MinNumberFieldValue)1