use of com.alibaba.dubbo.config.ReferenceConfig in project dubbo by alibaba.
the class ValidationTest method testProviderValidation.
@Test
public void testProviderValidation() {
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");
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);
// Save Error
try {
parameter = new ValidationParameter();
validationService.save(parameter);
Assert.fail();
} catch (RpcException e) {
Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
}
// Delete OK
validationService.delete(2, "abc");
// Delete Error
try {
validationService.delete(0, "abc");
Assert.fail();
} catch (RpcException e) {
Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
}
try {
validationService.delete(2, null);
Assert.fail();
} catch (RpcException e) {
Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
}
try {
validationService.delete(0, null);
Assert.fail();
} catch (RpcException e) {
Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
}
} finally {
reference.destroy();
}
} finally {
service.unexport();
}
}
use of com.alibaba.dubbo.config.ReferenceConfig 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 (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 {
reference.destroy();
}
} finally {
service.unexport();
}
}
use of com.alibaba.dubbo.config.ReferenceConfig in project dubbo by alibaba.
the class ValidationTest method testGenericValidation.
@Test
public void testGenericValidation() {
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<GenericService> reference = new ReferenceConfig<GenericService>();
reference.setApplication(new ApplicationConfig("validation-consumer"));
reference.setInterface(ValidationService.class.getName());
reference.setUrl("dubbo://127.0.0.1:29582?scope=remote&validation=true");
reference.setGeneric(true);
GenericService validationService = reference.get();
try {
// Save OK
Map<String, Object> parameter = new HashMap<String, Object>();
parameter.put("name", "liangfei");
parameter.put("Email", "liangfei@liang.fei");
parameter.put("Age", 50);
parameter.put("LoginDate", new Date(System.currentTimeMillis() - 1000000));
parameter.put("ExpiryDate", new Date(System.currentTimeMillis() + 1000000));
validationService.$invoke("save", new String[] { ValidationParameter.class.getName() }, new Object[] { parameter });
// Save Error
try {
parameter = new HashMap<String, Object>();
validationService.$invoke("save", new String[] { ValidationParameter.class.getName() }, new Object[] { parameter });
Assert.fail();
} catch (RpcException e) {
Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
}
// Delete OK
validationService.$invoke("delete", new String[] { long.class.getName(), String.class.getName() }, new Object[] { 2, "abc" });
// Delete Error
try {
validationService.$invoke("delete", new String[] { long.class.getName(), String.class.getName() }, new Object[] { 0, "abc" });
Assert.fail();
} catch (RpcException e) {
Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
}
try {
validationService.$invoke("delete", new String[] { long.class.getName(), String.class.getName() }, new Object[] { 2, null });
Assert.fail();
} catch (RpcException e) {
Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
}
try {
validationService.$invoke("delete", new String[] { long.class.getName(), String.class.getName() }, new Object[] { 0, null });
Assert.fail();
} catch (RpcException e) {
Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
}
} finally {
reference.destroy();
}
} finally {
service.unexport();
}
}
use of com.alibaba.dubbo.config.ReferenceConfig in project dubbo by alibaba.
the class ConfigTest method refer.
private DemoService refer(String url) {
ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
reference.setApplication(new ApplicationConfig("consumer"));
reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
reference.setInterface(DemoService.class);
reference.setUrl(url);
return reference.get();
}
use of com.alibaba.dubbo.config.ReferenceConfig in project dubbo by alibaba.
the class ConfigTest method testApiOverrideProperties.
@Test
public void testApiOverrideProperties() throws Exception {
ApplicationConfig application = new ApplicationConfig();
application.setName("api-override-properties");
RegistryConfig registry = new RegistryConfig();
registry.setAddress("N/A");
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(13123);
ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
service.setInterface(DemoService.class);
service.setRef(new DemoServiceImpl());
service.setApplication(application);
service.setRegistry(registry);
service.setProtocol(protocol);
service.export();
try {
URL url = service.toUrls().get(0);
assertEquals("api-override-properties", url.getParameter("application"));
assertEquals("world", url.getParameter("owner"));
assertEquals(13123, url.getPort());
ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
reference.setApplication(new ApplicationConfig("consumer"));
reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
reference.setInterface(DemoService.class);
reference.setUrl("dubbo://127.0.0.1:13123");
reference.get();
try {
url = reference.toUrls().get(0);
assertEquals("2000", url.getParameter("timeout"));
} finally {
reference.destroy();
}
} finally {
service.unexport();
}
}
Aggregations