PageRenderTime 29ms CodeModel.GetById 28ms RepoModel.GetById 7ms app.codeStats 0ms

/java/src/test/java/com/googlecode/protobuf/socketrpc/RpcForwarderTest.java

http://protobuf-socket-rpc.googlecode.com/
Java | 372 lines | 236 code | 49 blank | 87 comment | 0 complexity | be66cf6f40dc23af892c15c0a05f5813 MD5 | raw file
  1. // Copyright (c) 2009 Shardul Deo
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package com.googlecode.protobuf.socketrpc;
  21. import junit.framework.TestCase;
  22. import com.google.protobuf.ByteString;
  23. import com.googlecode.protobuf.socketrpc.RpcForwarder.Callback;
  24. import com.googlecode.protobuf.socketrpc.RpcForwarder.RpcException;
  25. import com.googlecode.protobuf.socketrpc.SocketRpcProtos.ErrorReason;
  26. import com.googlecode.protobuf.socketrpc.TestProtos.Request;
  27. import com.googlecode.protobuf.socketrpc.TestProtos.Response;
  28. import com.googlecode.protobuf.socketrpc.TestProtos.TestService;
  29. /**
  30. * Unit tests for {@link RpcForwarder}.
  31. *
  32. * @author Shardul Deo
  33. */
  34. public class RpcForwarderTest extends TestCase {
  35. private static final Request REQUEST;
  36. private static final SocketRpcProtos.Request RPC_REQUEST;
  37. static {
  38. REQUEST = Request.newBuilder().setStrData("Request Data").build();
  39. RPC_REQUEST = createRpcRequest(TestService.getDescriptor().getFullName(),
  40. TestService.getDescriptor().getMethods().get(0).getName(),
  41. REQUEST.toByteString());
  42. }
  43. private RpcForwarder rpcForwarder;
  44. @Override
  45. protected void setUp() throws Exception {
  46. super.setUp();
  47. rpcForwarder = new RpcForwarder();
  48. }
  49. /**
  50. * Test good request/response
  51. */
  52. public void testGoodRpc() throws RpcException {
  53. // Create data
  54. Response response = Response.newBuilder().setStrData("Response Data")
  55. .build();
  56. // Register Service
  57. rpcForwarder.registerService(TestService.newReflectiveService(new FakeServiceImpl(REQUEST)
  58. .withResponse(response)));
  59. // Test doBlockingRpc
  60. SocketRpcProtos.Response rpcResponse =
  61. rpcForwarder.doBlockingRpc(RPC_REQUEST);
  62. assertTrue(rpcResponse.getCallback());
  63. assertEquals(response.toByteString(), rpcResponse.getResponseProto());
  64. // Test doRpc
  65. Callback<SocketRpcProtos.Response> rpcCallback =
  66. new Callback<SocketRpcProtos.Response>();
  67. rpcForwarder.doRpc(RPC_REQUEST, rpcCallback);
  68. assertTrue(rpcCallback.isInvoked());
  69. rpcResponse = rpcCallback.getResponse();
  70. assertTrue(rpcResponse.getCallback());
  71. assertEquals(response.toByteString(), rpcResponse.getResponseProto());
  72. // Register BlockingService
  73. response = Response.newBuilder().setStrData("New Data").build();
  74. rpcForwarder.registerBlockingService(new FakeServiceImpl(REQUEST)
  75. .withResponse(response).toBlockingService());
  76. // Test doBlockingRpc
  77. rpcResponse = rpcForwarder.doBlockingRpc(RPC_REQUEST);
  78. assertTrue(rpcResponse.getCallback());
  79. assertEquals(response.toByteString(), rpcResponse.getResponseProto());
  80. // Test doRpc
  81. rpcCallback = new Callback<SocketRpcProtos.Response>();
  82. rpcForwarder.doRpc(RPC_REQUEST, rpcCallback);
  83. assertTrue(rpcCallback.isInvoked());
  84. rpcResponse = rpcCallback.getResponse();
  85. assertTrue(rpcResponse.getCallback());
  86. assertEquals(response.toByteString(), rpcResponse.getResponseProto());
  87. }
  88. /**
  89. * Successful RPC but callback is not called.
  90. */
  91. public void testNoCallback() throws RpcException {
  92. // Register Service
  93. rpcForwarder.registerService(new FakeServiceImpl(REQUEST));
  94. // Test doBlockingRpc
  95. SocketRpcProtos.Response rpcResponse =
  96. rpcForwarder.doBlockingRpc(RPC_REQUEST);
  97. assertFalse(rpcResponse.getCallback());
  98. assertFalse(rpcResponse.hasResponseProto());
  99. // Test doRpc
  100. Callback<SocketRpcProtos.Response> rpcCallback =
  101. new Callback<SocketRpcProtos.Response>();
  102. rpcForwarder.doRpc(RPC_REQUEST, rpcCallback);
  103. assertFalse(rpcCallback.isInvoked());
  104. // Register BlockingService
  105. rpcForwarder.registerBlockingService(
  106. new FakeServiceImpl(REQUEST).toBlockingService());
  107. // Test doBlockingRpc
  108. rpcResponse = rpcForwarder.doBlockingRpc(RPC_REQUEST);
  109. assertTrue(rpcResponse.getCallback());
  110. assertFalse(rpcResponse.hasResponseProto());
  111. // Test doRpc
  112. rpcCallback = new Callback<SocketRpcProtos.Response>();
  113. rpcForwarder.doRpc(RPC_REQUEST, rpcCallback);
  114. assertTrue(rpcCallback.isInvoked());
  115. rpcResponse = rpcCallback.getResponse();
  116. assertTrue(rpcResponse.getCallback());
  117. assertFalse(rpcResponse.hasResponseProto());
  118. }
  119. /**
  120. * Successful RPC but callback is called with null.
  121. */
  122. public void testNullCallBack() throws RpcException {
  123. // Register Service
  124. rpcForwarder.registerService(
  125. new FakeServiceImpl(REQUEST).withResponse(null));
  126. // Test doBlockingRpc
  127. SocketRpcProtos.Response rpcResponse =
  128. rpcForwarder.doBlockingRpc(RPC_REQUEST);
  129. assertTrue(rpcResponse.getCallback());
  130. assertFalse(rpcResponse.hasResponseProto());
  131. // Test doRpc
  132. Callback<SocketRpcProtos.Response> rpcCallback =
  133. new Callback<SocketRpcProtos.Response>();
  134. rpcForwarder.doRpc(RPC_REQUEST, rpcCallback);
  135. assertTrue(rpcCallback.isInvoked());
  136. rpcResponse = rpcCallback.getResponse();
  137. assertTrue(rpcResponse.getCallback());
  138. assertFalse(rpcResponse.hasResponseProto());
  139. // Register BlockingService
  140. rpcForwarder.registerBlockingService(
  141. new FakeServiceImpl(REQUEST).withResponse(null).toBlockingService());
  142. // Test doBlockingRpc
  143. rpcResponse = rpcForwarder.doBlockingRpc(RPC_REQUEST);
  144. assertTrue(rpcResponse.getCallback());
  145. assertFalse(rpcResponse.hasResponseProto());
  146. // Test doRpc
  147. rpcCallback = new Callback<SocketRpcProtos.Response>();
  148. rpcForwarder.doRpc(RPC_REQUEST, rpcCallback);
  149. assertTrue(rpcCallback.isInvoked());
  150. rpcResponse = rpcCallback.getResponse();
  151. assertTrue(rpcResponse.getCallback());
  152. assertFalse(rpcResponse.hasResponseProto());
  153. }
  154. /**
  155. * Server is called with RPC for unknown service.
  156. */
  157. public void testInvalidService() {
  158. rpcForwarder.registerService(new FakeServiceImpl(REQUEST));
  159. rpcForwarder.registerBlockingService(
  160. new FakeServiceImpl(REQUEST).toBlockingService());
  161. // Test doBlockingRpc
  162. try {
  163. rpcForwarder.doBlockingRpc(
  164. createRpcRequest("BadService", "", REQUEST.toByteString()));
  165. fail("Should have failed");
  166. } catch (RpcException e) {
  167. assertEquals(ErrorReason.SERVICE_NOT_FOUND, e.errorReason);
  168. }
  169. // Test doRpc
  170. try {
  171. rpcForwarder.doRpc(
  172. createRpcRequest("BadService", "", REQUEST.toByteString()), null);
  173. fail("Should have failed");
  174. } catch (RpcException e) {
  175. assertEquals(ErrorReason.SERVICE_NOT_FOUND, e.errorReason);
  176. }
  177. }
  178. /**
  179. * Server is called with RPC for unknown method.
  180. */
  181. public void testInvalidMethod() {
  182. // Register Service
  183. rpcForwarder.registerService(new FakeServiceImpl(REQUEST));
  184. assertInvalidMethodFails();
  185. // Register BlockingService
  186. rpcForwarder.registerBlockingService(
  187. new FakeServiceImpl(REQUEST).toBlockingService());
  188. assertInvalidMethodFails();
  189. }
  190. private void assertInvalidMethodFails() {
  191. // Test doBlockingRpc
  192. try {
  193. rpcForwarder.doBlockingRpc(createRpcRequest(
  194. TestService.getDescriptor().getFullName(),
  195. "BadMethod",
  196. REQUEST.toByteString()));
  197. fail("Should have failed");
  198. } catch (RpcException e) {
  199. assertEquals(ErrorReason.METHOD_NOT_FOUND, e.errorReason);
  200. }
  201. // Test doRpc
  202. try {
  203. rpcForwarder.doRpc(createRpcRequest(
  204. TestService.getDescriptor().getFullName(),
  205. "BadMethod",
  206. REQUEST.toByteString()), null);
  207. fail("Should have failed");
  208. } catch (RpcException e) {
  209. assertEquals(ErrorReason.METHOD_NOT_FOUND, e.errorReason);
  210. }
  211. }
  212. /**
  213. * RPC Request proto is invalid.
  214. */
  215. public void testInvalidRequestProto() {
  216. // Register Service
  217. rpcForwarder.registerService(new FakeServiceImpl(null));
  218. assertBadRequestProtoFails();
  219. // Register BlockingService
  220. rpcForwarder.registerBlockingService(
  221. new FakeServiceImpl(null).toBlockingService());
  222. assertBadRequestProtoFails();
  223. }
  224. private void assertBadRequestProtoFails() {
  225. // Test doBlockingRpc
  226. try {
  227. rpcForwarder.doBlockingRpc(createRpcRequest(
  228. TestService.getDescriptor().getFullName(),
  229. TestService.getDescriptor().getMethods().get(0).getName(),
  230. ByteString.copyFrom("Bad Request".getBytes())));
  231. fail("Should have failed");
  232. } catch (RpcException e) {
  233. assertEquals(ErrorReason.BAD_REQUEST_PROTO, e.errorReason);
  234. }
  235. // Test doRpc
  236. try {
  237. rpcForwarder.doRpc(createRpcRequest(
  238. TestService.getDescriptor().getFullName(),
  239. TestService.getDescriptor().getMethods().get(0).getName(),
  240. ByteString.copyFrom("Bad Request".getBytes())), null);
  241. fail("Should have failed");
  242. } catch (RpcException e) {
  243. assertEquals(ErrorReason.BAD_REQUEST_PROTO, e.errorReason);
  244. }
  245. }
  246. /**
  247. * RPC throws exception.
  248. */
  249. public void testRpcException() {
  250. // Register Service
  251. RuntimeException error = new RuntimeException();
  252. rpcForwarder.registerService(
  253. new FakeServiceImpl(REQUEST).throwsException(error));
  254. assertRpcErrorFails(error);
  255. // Register BlockingService
  256. error = new RuntimeException();
  257. rpcForwarder.registerBlockingService(
  258. new FakeServiceImpl(REQUEST).throwsException(error).toBlockingService());
  259. assertRpcErrorFails(error);
  260. }
  261. private void assertRpcErrorFails(RuntimeException error) {
  262. // Test doBlockingRpc
  263. try {
  264. rpcForwarder.doBlockingRpc(RPC_REQUEST);
  265. fail("Should have failed");
  266. } catch (RpcException e) {
  267. assertEquals(ErrorReason.RPC_ERROR, e.errorReason);
  268. assertSame(error, e.getCause());
  269. }
  270. // Test doRpc
  271. try {
  272. rpcForwarder.doRpc(RPC_REQUEST, null);
  273. fail("Should have failed");
  274. } catch (RpcException e) {
  275. assertEquals(ErrorReason.RPC_ERROR, e.errorReason);
  276. assertSame(error, e.getCause());
  277. }
  278. }
  279. /**
  280. * RPC fails.
  281. */
  282. public void testRPCFailed() throws RpcException {
  283. // Register Service
  284. rpcForwarder.registerService(
  285. new FakeServiceImpl(REQUEST).failsWithError("Error"));
  286. // Test doBlockingRpc
  287. SocketRpcProtos.Response rpcResponse =
  288. rpcForwarder.doBlockingRpc(RPC_REQUEST);
  289. assertFalse(rpcResponse.getCallback());
  290. assertEquals("Error", rpcResponse.getError());
  291. assertEquals(ErrorReason.RPC_FAILED, rpcResponse.getErrorReason());
  292. // Test doRpc
  293. Callback<SocketRpcProtos.Response> rpcCallback =
  294. new Callback<SocketRpcProtos.Response>();
  295. rpcForwarder.doRpc(RPC_REQUEST, rpcCallback);
  296. assertFalse(rpcCallback.isInvoked());
  297. // Register BlockingService
  298. rpcForwarder.registerBlockingService(new FakeServiceImpl(REQUEST)
  299. .failsWithError("New Error").toBlockingService());
  300. // Test doBlockingRpc
  301. try {
  302. rpcForwarder.doBlockingRpc(RPC_REQUEST);
  303. } catch (RpcException e) {
  304. assertEquals(ErrorReason.RPC_FAILED, e.errorReason);
  305. assertEquals("New Error", e.getMessage());
  306. }
  307. // Test doRpc
  308. try {
  309. rpcForwarder.doRpc(RPC_REQUEST, null);
  310. } catch (RpcException e) {
  311. assertEquals(ErrorReason.RPC_FAILED, e.errorReason);
  312. assertEquals("New Error", e.getMessage());
  313. }
  314. }
  315. private static SocketRpcProtos.Request createRpcRequest(String service,
  316. String method, ByteString request) {
  317. return SocketRpcProtos.Request.newBuilder()
  318. .setServiceName(service)
  319. .setMethodName(method)
  320. .setRequestProto(request)
  321. .build();
  322. }
  323. }