JsonRpcMessageReceiver implements WebSocketMessageReceiver { @Override public void receive(String combinedEndpointId, String message) { checkNotNull(combinedEndpointId, "Endpoint ID must not be null"); checkArgument(!combinedEndpointId.isEmpty(), "Endpoint ID name must not be empty"); checkNotNull(message, "Message must not be null"); checkArgument(!message.isEmpty(), "Message must not be empty"); LOGGER.trace("Receiving message: {}, from endpoint: {}", message, combinedEndpointId); if (!jsonRpcQualifier.isValidJson(message)) { String error = "An error occurred on the server while parsing the JSON text"; errorTransmitter.transmit(combinedEndpointId, new JsonRpcException(-32700, error)); } List<String> messages = jsonRpcUnmarshaller.unmarshalArray(message); for (String innerMessage : messages) { if (jsonRpcQualifier.isJsonRpcRequest(innerMessage)) { String endpointId = combinedEndpointId.split(SEPARATOR)[1]; ProcessRequestTask task = new ProcessRequestTask(combinedEndpointId, innerMessage); requestProcessor.process(endpointId, task); } else if (jsonRpcQualifier.isJsonRpcResponse(innerMessage)) { processResponse(combinedEndpointId, innerMessage); } else { processError(); } } } @Inject  JsonRpcMessageReceiver(
      RequestDispatcher requestDispatcher,
      ResponseDispatcher responseDispatcher,
      JsonRpcErrorTransmitter errorTransmitter,
      JsonRpcQualifier jsonRpcQualifier,
      JsonRpcUnmarshaller jsonRpcUnmarshaller,
      RequestProcessor requestProcessor); @Override void receive(String combinedEndpointId, String message);  }
@Test public void shouldValidateMessage() throws Exception { jsonRpcMessageReceiver.receive(ENDPOINT_ID, MESSAGE); verify(jsonRpcQualifier).isValidJson(MESSAGE); }
@Test public void shouldTransmitErrorWhenValidationFailed() throws Exception { when(jsonRpcQualifier.isValidJson(MESSAGE)).thenReturn(false); jsonRpcMessageReceiver.receive(ENDPOINT_ID, MESSAGE); verify(errorTransmitter).transmit(eq(ENDPOINT_ID), any(JsonRpcException.class)); }
@Test public void shouldNotTransmitErrorWhenValidationSucceeded() throws Exception { when(jsonRpcQualifier.isValidJson(MESSAGE)).thenReturn(true); jsonRpcMessageReceiver.receive(ENDPOINT_ID, MESSAGE); verify(errorTransmitter, never()).transmit(eq(ENDPOINT_ID), any(JsonRpcException.class)); }
@Test public void shouldUnmarshalArray() throws Exception { jsonRpcMessageReceiver.receive(ENDPOINT_ID, MESSAGE); verify(jsonRpcUnmarshaller).unmarshalArray(MESSAGE); }
@Test public void shouldDispatchResponseIfResponseReceived() throws Exception { when(jsonRpcQualifier.isJsonRpcResponse(MESSAGE)).thenReturn(true); when(jsonRpcQualifier.isJsonRpcRequest(MESSAGE)).thenReturn(false); when(jsonRpcUnmarshaller.unmarshalArray(any())).thenReturn(singletonList(MESSAGE)); JsonRpcResponse jsonRpcResponse = Mockito.mock(JsonRpcResponse.class); when(jsonRpcUnmarshaller.unmarshalResponse(any())).thenReturn(jsonRpcResponse); jsonRpcMessageReceiver.receive(ENDPOINT_ID, MESSAGE); verify(responseDispatcher).dispatch(eq(ENDPOINT_ID), any(JsonRpcResponse.class)); }
@Test public void shouldDispatchRequestIfRequestReceived() throws Exception { when(jsonRpcQualifier.isJsonRpcRequest(MESSAGE)).thenReturn(true); when(jsonRpcUnmarshaller.unmarshalArray(any())).thenReturn(singletonList(MESSAGE)); jsonRpcMessageReceiver.receive(ENDPOINT_ID, MESSAGE); verify(requestProcessor).process(any(), any()); }