Search in sources :

Example 21 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project wildfly by wildfly.

the class JPABeanValidationTestCase method testFailingBeanValidationNullAddress.

/**
     * Test that a bean validation error is thrown
     *
     * @throws Exception
     */
@Test
public void testFailingBeanValidationNullAddress() throws Exception {
    SFSB1 sfsb1 = lookup("SFSB1", SFSB1.class);
    try {
        sfsb1.createEmployee("name", null, 2);
        fail("should of thrown validation error for null address in Employee entity");
    } catch (Throwable throwable) {
        ConstraintViolationException constraintViolationException = null;
        // find the ConstraintViolationException
        while (throwable != null && !(throwable instanceof ConstraintViolationException)) {
            throwable = throwable.getCause();
        }
        // should be null or instanceof ConstraintViolationException
        constraintViolationException = (ConstraintViolationException) throwable;
        assertTrue("expected ConstraintViolationException but got " + constraintViolationException, constraintViolationException instanceof ConstraintViolationException);
    }
}
Also used : ConstraintViolationException(javax.validation.ConstraintViolationException) SFSB1(org.jboss.as.test.integration.jpa.beanvalidation.SFSB1) Test(org.junit.Test)

Example 22 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project spring-framework by spring-projects.

the class MethodValidationInterceptor method invoke.

@Override
@SuppressWarnings("unchecked")
public Object invoke(MethodInvocation invocation) throws Throwable {
    Class<?>[] groups = determineValidationGroups(invocation);
    // Standard Bean Validation 1.1 API
    ExecutableValidator execVal = this.validator.forExecutables();
    Method methodToValidate = invocation.getMethod();
    Set<ConstraintViolation<Object>> result;
    try {
        result = execVal.validateParameters(invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
    } catch (IllegalArgumentException ex) {
        // Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011
        // Let's try to find the bridged method on the implementation class...
        methodToValidate = BridgeMethodResolver.findBridgedMethod(ClassUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass()));
        result = execVal.validateParameters(invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
    }
    if (!result.isEmpty()) {
        throw new ConstraintViolationException(result);
    }
    Object returnValue = invocation.proceed();
    result = execVal.validateReturnValue(invocation.getThis(), methodToValidate, returnValue, groups);
    if (!result.isEmpty()) {
        throw new ConstraintViolationException(result);
    }
    return returnValue;
}
Also used : ExecutableValidator(javax.validation.executable.ExecutableValidator) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Method(java.lang.reflect.Method)

Example 23 with ConstraintViolationException

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

the class ValidationTest method testValidation.

@Test
public void testValidation() {
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ValidationTest.class.getPackage().getName().replace('.', '/') + "/validation-provider.xml");
    providerContext.start();
    try {
        ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(ValidationTest.class.getPackage().getName().replace('.', '/') + "/validation-consumer.xml");
        consumerContext.start();
        try {
            ValidationService validationService = (ValidationService) consumerContext.getBean("validationService");
            // Save OK
            ValidationParameter parameter = new ValidationParameter();
            parameter.setName("liangfei");
            parameter.setEmail("liangfei@liang.fei");
            parameter.setAge(50);
            parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
            parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
            validationService.save(parameter);
            try {
                parameter = new ValidationParameter();
                parameter.setName("l");
                parameter.setEmail("liangfei@liang.fei");
                parameter.setAge(50);
                parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
                parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
                validationService.save(parameter);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
            }
            // Save Error
            try {
                parameter = new ValidationParameter();
                validationService.save(parameter);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
            }
            // Delete OK
            validationService.delete(2, "abc");
            // Delete Error
            try {
                validationService.delete(2, "a");
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            // Delete Error
            try {
                validationService.delete(0, "abc");
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            try {
                validationService.delete(2, null);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            try {
                validationService.delete(0, null);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(2, violations.size());
            }
        } finally {
            consumerContext.stop();
            consumerContext.close();
        }
    } finally {
        providerContext.stop();
        providerContext.close();
    }
}
Also used : Set(java.util.Set) ValidationParameter(com.alibaba.dubbo.examples.validation.api.ValidationParameter) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ConstraintViolationException(javax.validation.ConstraintViolationException) ValidationService(com.alibaba.dubbo.examples.validation.api.ValidationService) Date(java.util.Date) Test(org.junit.Test)

Example 24 with ConstraintViolationException

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

the class ValidationTest method testValidation.

@Test
public void testValidation() {
    ServiceConfig<ValidationService> service = new ServiceConfig<ValidationService>();
    service.setApplication(new ApplicationConfig("validation-provider"));
    service.setRegistry(new RegistryConfig("N/A"));
    service.setProtocol(new ProtocolConfig("dubbo", 29582));
    service.setInterface(ValidationService.class.getName());
    service.setRef(new ValidationServiceImpl());
    service.setValidation(String.valueOf(true));
    service.export();
    try {
        ReferenceConfig<ValidationService> reference = new ReferenceConfig<ValidationService>();
        reference.setApplication(new ApplicationConfig("validation-consumer"));
        reference.setInterface(ValidationService.class);
        reference.setUrl("dubbo://127.0.0.1:29582?scope=remote&validation=true");
        ValidationService validationService = reference.get();
        try {
            // Save OK
            ValidationParameter parameter = new ValidationParameter();
            parameter.setName("liangfei");
            parameter.setEmail("liangfei@liang.fei");
            parameter.setAge(50);
            parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
            parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
            validationService.save(parameter);
            try {
                parameter = new ValidationParameter();
                parameter.setName("l");
                parameter.setEmail("liangfei@liang.fei");
                parameter.setAge(50);
                parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
                parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
                validationService.save(parameter);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
            }
            // verify save group, save error
            try {
                parameter = new ValidationParameter();
                parameter.setName("liangfei");
                parameter.setAge(50);
                parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
                parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
                validationService.save(parameter);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
            }
            // and Update
            try {
                parameter = new ValidationParameter();
                parameter.setName("liangfei");
                parameter.setAge(50);
                parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
                parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
                validationService.relatedQuery(parameter);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertEquals(violations.size(), 2);
            }
            // Save Error
            try {
                parameter = new ValidationParameter();
                validationService.save(parameter);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertTrue(violations.size() == 3);
                Assert.assertNotNull(violations);
            }
            // Delete OK
            validationService.delete(2, "abc");
            // Delete Error
            try {
                validationService.delete(2, "a");
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            // Delete Error
            try {
                validationService.delete(0, "abc");
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            try {
                validationService.delete(2, null);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            try {
                validationService.delete(0, null);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(2, violations.size());
            }
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) Set(java.util.Set) Date(java.util.Date) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) ConstraintViolationException(javax.validation.ConstraintViolationException) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig) Test(org.junit.Test)

Example 25 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project spring-data-mongodb by spring-projects.

the class ValidatingMongoEventListener method onBeforeSave.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener#onBeforeSave(org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent)
	 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void onBeforeSave(BeforeSaveEvent<Object> event) {
    LOG.debug("Validating object: {}", event.getSource());
    Set violations = validator.validate(event.getSource());
    if (!violations.isEmpty()) {
        LOG.info("During object: {} validation violations found: {}", event.getSource(), violations);
        throw new ConstraintViolationException(violations);
    }
}
Also used : Set(java.util.Set) ConstraintViolationException(javax.validation.ConstraintViolationException)

Aggregations

ConstraintViolationException (javax.validation.ConstraintViolationException)87 Test (org.junit.Test)67 LocalMode (com.datatorrent.api.LocalMode)35 Configuration (org.apache.hadoop.conf.Configuration)34 File (java.io.File)16 ConstraintViolation (javax.validation.ConstraintViolation)15 Session (org.hibernate.Session)11 Transaction (org.hibernate.Transaction)11 LogicalPlan (com.datatorrent.stram.plan.logical.LogicalPlan)9 Map (java.util.Map)9 BigDecimal (java.math.BigDecimal)7 HashMap (java.util.HashMap)7 Set (java.util.Set)6 StramLocalCluster (com.datatorrent.stram.StramLocalCluster)5 ConfigApiTest (com.sun.enterprise.configapi.tests.ConfigApiTest)5 HashSet (java.util.HashSet)5 ValidationException (javax.validation.ValidationException)5 Validator (javax.validation.Validator)5 Response (javax.ws.rs.core.Response)5 Path (org.apache.hadoop.fs.Path)5