use of com.alibaba.dubbo.rpc.RpcException 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.rpc.RpcException in project dubbo by alibaba.
the class MockClusterInvokerTest method testMockInvokerFromOverride_Invoke_check_ListPojo_error.
@Test
public void testMockInvokerFromOverride_Invoke_check_ListPojo_error() {
URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName()).addParameter("getUsers.mock", "force:return [{id:x, name:\"hi1\"}]").addParameter("invoke_return_error", "true");
Invoker<IHelloService> cluster = getClusterInvoker(url);
// Configured with mock
RpcInvocation invocation = new RpcInvocation();
invocation.setMethodName("getUsers");
try {
cluster.invoke(invocation);
} catch (RpcException e) {
}
}
use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.
the class MockClusterInvokerTest method testMockInvokerFromOverride_Invoke_force_throw.
@Test
public void testMockInvokerFromOverride_Invoke_force_throw() {
URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName()).addParameter("getBoolean2.mock", "force:throw ").addParameter("invoke_return_error", "true");
Invoker<IHelloService> cluster = getClusterInvoker(url);
// Configured with mock
RpcInvocation invocation = new RpcInvocation();
invocation.setMethodName("getBoolean2");
try {
cluster.invoke(invocation);
Assert.fail();
} catch (RpcException e) {
Assert.assertFalse("not custem exception", e.isBiz());
}
}
use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.
the class MockClusterInvokerTest method testMockInvokerFromOverride_Invoke_mock_false.
@Test
public void testMockInvokerFromOverride_Invoke_mock_false() {
URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName()).addParameter("mock", "false").addParameter("invoke_return_error", "true");
Invoker<IHelloService> cluster = getClusterInvoker(url);
// Configured with mock
RpcInvocation invocation = new RpcInvocation();
invocation.setMethodName("getBoolean2");
try {
cluster.invoke(invocation);
Assert.fail();
} catch (RpcException e) {
Assert.assertTrue(e.isTimeout());
}
}
use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.
the class MockClusterInvokerTest method getClusterInvoker.
@SuppressWarnings({ "unchecked", "rawtypes" })
private Invoker<IHelloService> getClusterInvoker(URL url) {
// As `javassist` have a strict restriction of argument types, request will fail if Invocation do not contains complete parameter type information
final URL durl = url.addParameter("proxy", "jdk");
invokers.clear();
ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getExtension("jdk");
Invoker<IHelloService> invoker1 = proxy.getInvoker(new HelloService(), IHelloService.class, durl);
invokers.add(invoker1);
Directory<IHelloService> dic = new StaticDirectory<IHelloService>(durl, invokers, null);
AbstractClusterInvoker<IHelloService> cluster = new AbstractClusterInvoker(dic) {
@Override
protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
if (durl.getParameter("invoke_return_error", false)) {
throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "test rpc exception");
} else {
return ((Invoker<?>) invokers.get(0)).invoke(invocation);
}
}
};
return new MockClusterInvoker<IHelloService>(dic, cluster);
}
Aggregations