RequestDispatcher { public void dispatch(String endpointId, JsonRpcRequest request) throws JsonRpcException { checkNotNull(endpointId, "Endpoint ID must not be null"); checkArgument(!endpointId.isEmpty(), "Endpoint ID must not be empty"); checkNotNull(request, "Request must not be null"); LOGGER.trace("Dispatching request: " + request + ", endpoint: " + endpointId); String method = request.getMethod(); JsonRpcParams params = request.getParams(); if (request.hasId()) { LOGGER.trace("Request has ID"); String requestId = request.getId(); checkRequestHandlerRegistration(method, requestId); requestHandlerManager.handle(endpointId, requestId, method, params); } else { LOGGER.trace("Request has no ID -> it is a notification"); checkNotificationHandlerRegistration(method); requestHandlerManager.handle(endpointId, method, params); } } @Inject  RequestDispatcher(RequestHandlerManager requestHandlerManager); void dispatch(String endpointId, JsonRpcRequest request);  }
@Test public void shouldHandleRequest() throws Exception { when(request.hasId()).thenReturn(true); requestDispatcher.dispatch(ENDPOINT_ID, request); verify(requestHandlerManager).handle(ENDPOINT_ID, REQUEST_ID, REQUEST_METHOD, params); }
@Test public void shouldHandleNotification() throws Exception { when(request.hasId()).thenReturn(false); requestDispatcher.dispatch(ENDPOINT_ID, request); verify(requestHandlerManager).handle(ENDPOINT_ID, REQUEST_METHOD, params); }
@Test(expectedExceptions = JsonRpcException.class) public void shouldThrowExceptionOnNotRegisteredRequestHandler() throws Exception { when(requestHandlerManager.isRegistered(REQUEST_METHOD)).thenReturn(false); requestDispatcher.dispatch(ENDPOINT_ID, request); }
@Test(expectedExceptions = JsonRpcException.class) public void shouldThrowExceptionOnNotRegisteredNotificationHandler() throws Exception { when(requestHandlerManager.isRegistered(REQUEST_METHOD)).thenReturn(false); requestDispatcher.dispatch(ENDPOINT_ID, request); }