Search in sources :

Example 6 with ConstraintViolation

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

the class SMSMessageTest method testDynamicFromFieldZeroLength.

@Test
public void testDynamicFromFieldZeroLength() throws Exception {
    String zeroLengthDynamicFrom = "";
    final SMSMessage m = new SMSMessage("idAsString", "Hello World", validNumber, zeroLengthDynamicFrom);
    final Set<ConstraintViolation<SMSMessage>> constraintViolations = validator.validate(m);
    Assert.isTrue(1 == constraintViolations.size());
}
Also used : SMSMessage(org.apache.camel.component.cm.client.SMSMessage) ConstraintViolation(javax.validation.ConstraintViolation) Test(org.junit.Test)

Example 7 with ConstraintViolation

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

the class BeanValidatorRouteTest method validateShouldFailWithOrderedChecksGroup.

@Test
public void validateShouldFailWithOrderedChecksGroup() throws Exception {
    if (isPlatform("aix")) {
        // cannot run on aix
        return;
    }
    final String url = "bean-validator://x?group=org.apache.camel.component.bean.validator.OrderedChecks";
    final Car car = createCar(null, "D-A");
    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("manufacturer", constraintViolation.getPropertyPath().toString());
        assertEquals(null, constraintViolation.getInvalidValue());
        assertEquals("may not be null", constraintViolation.getMessage());
    }
    car.setManufacturer("BMW");
    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("D-A", constraintViolation.getInvalidValue());
        assertEquals("size must be between 5 and 14", constraintViolation.getMessage());
    }
    car.setLicensePlate("DD-AB-123");
    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 8 with ConstraintViolation

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

the class BeanValidatorRouteTest method validateShouldFailWithExpliciteDefaultGroup.

@Test
public void validateShouldFailWithExpliciteDefaultGroup() throws Exception {
    if (isPlatform("aix")) {
        // cannot run on aix
        return;
    }
    final String url = "bean-validator://x?group=javax.validation.groups.Default";
    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 9 with ConstraintViolation

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

the class BeanValidatorRouteTest method validateShouldFailWithOptionalChecksGroup.

@Test
public void validateShouldFailWithOptionalChecksGroup() throws Exception {
    if (isPlatform("aix")) {
        // cannot run on aix
        return;
    }
    final String url = "bean-validator://x?group=org.apache.camel.component.bean.validator.OptionalChecks";
    final Car car = createCar("BMW", "D-A");
    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("D-A", constraintViolation.getInvalidValue());
        assertEquals("size must be between 5 and 14", constraintViolation.getMessage());
    }
    car.setLicensePlate("DD-AB-123");
    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 10 with ConstraintViolation

use of javax.validation.ConstraintViolation in project dropwizard by dropwizard.

the class ParamValidatorUnwrapperTest method failsWithInvalidIntParam.

@Test
public void failsWithInvalidIntParam() {
    final Example example = new Example();
    example.inter = new IntParam("2");
    final Set<ConstraintViolation<Example>> validate = validator.validate(example);
    assertThat(validate).hasSize(1);
}
Also used : IntParam(io.dropwizard.jersey.params.IntParam) 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