Search in sources :

Example 1 with ValidationService

use of com.alibaba.dubbo.examples.validation.api.ValidationService 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 (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
            }
            // Save Error
            try {
                parameter = new ValidationParameter();
                validationService.save(parameter);
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
            }
            // Delete OK
            validationService.delete(2, "abc");
            // Delete Error
            try {
                validationService.delete(2, "a");
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            // Delete Error
            try {
                validationService.delete(0, "abc");
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            try {
                validationService.delete(2, null);
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            try {
                validationService.delete(0, null);
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                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) RpcException(com.alibaba.dubbo.rpc.RpcException) ConstraintViolationException(javax.validation.ConstraintViolationException) ValidationService(com.alibaba.dubbo.examples.validation.api.ValidationService) Date(java.util.Date) Test(org.junit.Test)

Example 2 with ValidationService

use of com.alibaba.dubbo.examples.validation.api.ValidationService in project dubbo by alibaba.

the class ValidationConsumer method main.

public static void main(String[] args) throws Exception {
    String config = ValidationConsumer.class.getPackage().getName().replace('.', '/') + "/validation-consumer.xml";
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
    context.start();
    ValidationService validationService = (ValidationService) context.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);
    System.out.println("Validation Save OK");
    // Save Error
    try {
        parameter = new ValidationParameter();
        validationService.save(parameter);
        System.err.println("Validation Save ERROR");
    } catch (RpcException e) {
        ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
        Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
        System.out.println(violations);
    }
    // Delete OK
    validationService.delete(2, "abc");
    System.out.println("Validation Delete OK");
    // Delete Error
    try {
        validationService.delete(0, "abc");
        System.err.println("Validation Delete ERROR");
    } catch (RpcException e) {
        ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
        Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
        System.out.println(violations);
    }
}
Also used : Set(java.util.Set) ValidationParameter(com.alibaba.dubbo.examples.validation.api.ValidationParameter) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RpcException(com.alibaba.dubbo.rpc.RpcException) ConstraintViolationException(javax.validation.ConstraintViolationException) ValidationService(com.alibaba.dubbo.examples.validation.api.ValidationService) Date(java.util.Date)

Aggregations

ValidationParameter (com.alibaba.dubbo.examples.validation.api.ValidationParameter)2 ValidationService (com.alibaba.dubbo.examples.validation.api.ValidationService)2 RpcException (com.alibaba.dubbo.rpc.RpcException)2 Date (java.util.Date)2 Set (java.util.Set)2 ConstraintViolationException (javax.validation.ConstraintViolationException)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 Test (org.junit.Test)1