Search in sources :

Example 71 with ConstraintViolation

use of javax.validation.ConstraintViolation in project camel by apache.

the class CMConfigurationTest method testMaxNumberOfPartsZero.

@Test
public void testMaxNumberOfPartsZero() throws Exception {
    final CMConfiguration configuration = new CMConfiguration();
    configuration.setProductToken(UUID.randomUUID().toString());
    configuration.setDefaultFrom("DefaultFrom");
    configuration.setDefaultMaxNumberOfParts(0);
    configuration.setTestConnectionOnStartup(false);
    final Set<ConstraintViolation<CMConfiguration>> constraintViolations = validator.validate(configuration);
    Assert.isTrue(1 == constraintViolations.size());
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) CMConfiguration(org.apache.camel.component.cm.CMConfiguration) Test(org.junit.Test)

Example 72 with ConstraintViolation

use of javax.validation.ConstraintViolation in project camel by apache.

the class BeanValidatorRouteTest method validateShouldFailWithImpliciteDefaultGroup.

@Test
public void validateShouldFailWithImpliciteDefaultGroup() throws Exception {
    if (isPlatform("aix")) {
        // cannot run on aix
        return;
    }
    final String url = "bean-validator://x";
    final Car car = createCar("BMW", null);
    try {
        template.requestBody(url, car);
        fail("should throw exception");
    } catch (CamelExecutionException e) {
        assertIsInstanceOf(BeanValidationException.class, e.getCause());
        BeanValidationException exception = (BeanValidationException) e.getCause();
        Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations();
        assertEquals(1, constraintViolations.size());
        ConstraintViolation<Object> constraintViolation = constraintViolations.iterator().next();
        assertEquals("licensePlate", constraintViolation.getPropertyPath().toString());
        assertEquals(null, constraintViolation.getInvalidValue());
        assertEquals("may not be null", constraintViolation.getMessage());
    }
    car.setLicensePlate("D-A");
    Exchange exchange = template.request(url, new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody(car);
        }
    });
    assertNotNull(exchange);
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) Exchange(org.apache.camel.Exchange) Set(java.util.Set) Processor(org.apache.camel.Processor) ConstraintViolation(javax.validation.ConstraintViolation) CamelExecutionException(org.apache.camel.CamelExecutionException) Test(org.junit.Test)

Example 73 with ConstraintViolation

use of javax.validation.ConstraintViolation in project dubbo by alibaba.

the class JValidator method validate.

public void validate(String methodName, Class<?>[] parameterTypes, Object[] arguments) throws Exception {
    String methodClassName = clazz.getName() + "_" + toUpperMethoName(methodName);
    Class<?> methodClass = null;
    try {
        methodClass = Class.forName(methodClassName, false, Thread.currentThread().getContextClassLoader());
    } catch (ClassNotFoundException e) {
    }
    Set<ConstraintViolation<?>> violations = new HashSet<ConstraintViolation<?>>();
    Method method = clazz.getMethod(methodName, parameterTypes);
    Object parameterBean = getMethodParameterBean(clazz, method, arguments);
    if (parameterBean != null) {
        if (methodClass != null) {
            violations.addAll(validator.validate(parameterBean, Default.class, clazz, methodClass));
        } else {
            violations.addAll(validator.validate(parameterBean, Default.class, clazz));
        }
    }
    for (Object arg : arguments) {
        validate(violations, arg, clazz, methodClass);
    }
    if (violations.size() > 0) {
        throw new ConstraintViolationException("Failed to validate service: " + clazz.getName() + ", method: " + methodName + ", cause: " + violations, violations);
    }
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Method(java.lang.reflect.Method) Default(javax.validation.groups.Default) HashSet(java.util.HashSet)

Example 74 with ConstraintViolation

use of javax.validation.ConstraintViolation in project spring-petclinic by spring-projects.

the class ValidatorTests method shouldNotValidateWhenFirstNameEmpty.

@Test
public void shouldNotValidateWhenFirstNameEmpty() {
    LocaleContextHolder.setLocale(Locale.ENGLISH);
    Person person = new Person();
    person.setFirstName("");
    person.setLastName("smith");
    Validator validator = createValidator();
    Set<ConstraintViolation<Person>> constraintViolations = validator.validate(person);
    assertThat(constraintViolations.size()).isEqualTo(1);
    ConstraintViolation<Person> violation = constraintViolations.iterator().next();
    assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName");
    assertThat(violation.getMessage()).isEqualTo("may not be empty");
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) Test(org.junit.Test)

Example 75 with ConstraintViolation

use of javax.validation.ConstraintViolation in project ORCID-Source by ORCID.

the class ManagePasswordOptionsValidationFormTest method testPasswordsMatchValidation.

@Test
public void testPasswordsMatchValidation() throws Exception {
    String eightMixedCharactersDigitsAndSymbol = "p4s]sW[rd";
    String incorrectRetype = "p4s]sT]rd";
    ManagePasswordOptionsForm form = new ManagePasswordOptionsForm();
    form.setPassword(eightMixedCharactersDigitsAndSymbol);
    form.setRetypedPassword(incorrectRetype);
    Set<ConstraintViolation<ManagePasswordOptionsForm>> violations = validator.validate(form);
    Map<String, String> allErrorValues = retrieveErrorKeyAndMessage(violations);
    String passwordFormatError = allErrorValues.get("password");
    String retypedPasswordFormatError = allErrorValues.get("retypedPassword");
    allErrorValues = retrieveErrorKeyAndMessage(violations);
    // the passwords themselves should be ok as they conform to the format,
    // problem is they don't match
    assertNull(passwordFormatError);
    assertNull(retypedPasswordFormatError);
    Set<String> fieldLevelErrors = retrieveErrorValuesOnly(violations);
    assertTrue(fieldLevelErrors.contains("The password and confirm password field must match"));
    // now match them
    form.setRetypedPassword("p4s]sW[rd");
    violations = validator.validate(form);
    allErrorValues = retrieveErrorKeyAndMessage(violations);
    passwordFormatError = allErrorValues.get("password");
    retypedPasswordFormatError = allErrorValues.get("retypedPassword");
    allErrorValues = retrieveErrorKeyAndMessage(violations);
    assertNull(passwordFormatError);
    assertNull(retypedPasswordFormatError);
    fieldLevelErrors = retrieveErrorValuesOnly(violations);
    assertFalse(fieldLevelErrors.contains("The password and confirmed password must match"));
}
Also used : ManagePasswordOptionsForm(org.orcid.frontend.web.forms.ManagePasswordOptionsForm) ConstraintViolation(javax.validation.ConstraintViolation) Test(org.junit.Test)

Aggregations

ConstraintViolation (javax.validation.ConstraintViolation)95 Test (org.junit.Test)78 Validator (javax.validation.Validator)19 ConstraintViolationException (javax.validation.ConstraintViolationException)12 SMSMessage (org.apache.camel.component.cm.client.SMSMessage)12 ChangePasswordForm (org.orcid.frontend.web.forms.ChangePasswordForm)11 CmsDocumentBlobSegment (gov.ca.cwds.data.persistence.cms.CmsDocumentBlobSegment)10 ValidatorFactory (javax.validation.ValidatorFactory)9 ManagePasswordOptionsForm (org.orcid.frontend.web.forms.ManagePasswordOptionsForm)8 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 CMConfiguration (org.apache.camel.component.cm.CMConfiguration)6 Set (java.util.Set)5 InitialContext (javax.naming.InitialContext)5 Session (org.hibernate.Session)5 Transaction (org.hibernate.Transaction)5 Method (java.lang.reflect.Method)4 ExecutableValidator (javax.validation.executable.ExecutableValidator)4 CamelExecutionException (org.apache.camel.CamelExecutionException)4 Exchange (org.apache.camel.Exchange)4