protected void handleResponse(ResponseMessage responseMessage) { PendingRequestInfo requestInfo; synchronized (sentRequestMap) { requestInfo = sentRequestMap.remove(responseMessage.getId()); } if (requestInfo == null) { // We have no pending request information that matches the id given in the response LOG.log(Level.WARNING, "Unmatched response message: " + responseMessage); } else if (responseMessage.getError() != null) { // The remote service has replied with an error requestInfo.future.completeExceptionally(new ResponseErrorException(responseMessage.getError())); } else { // The remote service has replied with a result object requestInfo.future.complete(responseMessage.getResult()); } }
protected void handleResponse(ResponseMessage responseMessage) { PendingRequestInfo requestInfo; synchronized (sentRequestMap) { requestInfo = sentRequestMap.remove(responseMessage.getId()); } if (requestInfo == null) { // We have no pending request information that matches the id given in the response LOG.log(Level.WARNING, "Unmatched response message: " + responseMessage); } else if (responseMessage.getError() != null) { // The remote service has replied with an error requestInfo.future.completeExceptionally(new ResponseErrorException(responseMessage.getError())); } else { // The remote service has replied with a result object requestInfo.future.complete(responseMessage.getResult()); } }
@Test public void testResponseErrorData() { MessageJsonHandler handler = new MessageJsonHandler(Collections.emptyMap()); ResponseMessage message = (ResponseMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + "\"error\": { \"code\": -32001, \"message\": \"foo\",\n" + " \"data\": { \"uri\": \"file:/foo\", \"version\": 5, \"list\": [\"a\", \"b\", \"c\"] }\n" + " }\n" + "}"); ResponseError error = message.getError(); Assert.assertTrue("Expected a JsonObject in error.data", error.getData() instanceof JsonObject); JsonObject data = (JsonObject) error.getData(); Assert.assertEquals("file:/foo", data.get("uri").getAsString()); Assert.assertEquals(5, data.get("version").getAsInt()); JsonArray list = data.get("list").getAsJsonArray(); Assert.assertEquals("a", list.get(0).getAsString()); Assert.assertEquals("b", list.get(1).getAsString()); Assert.assertEquals("c", list.get(2).getAsString()); }
@Test public void testHandleRequestIssues() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); endpoint.handle(init(new RequestMessage(), it -> { it.setId("1"); it.setMethod("foo"); it.setParams("myparam"); }), Collections.singletonList(new MessageIssue("bar"))); ResponseMessage responseMessage = (ResponseMessage) consumer.messages.get(0); assertNotNull(responseMessage.getError()); assertEquals("bar", responseMessage.getError().getMessage()); }
@Test public void testErrorResponse_AllOrders() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Location>() {}.getType(), new TypeToken<Void>() { }.getType())); MessageJsonHandler handler = new MessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); String[] properties = new String[] { "\"jsonrpc\":\"2.0\"", "\"id\":2", "\"message\": \"failed\"", "\"error\": {\"code\": 123456, \"message\": \"failed\", \"data\": {\"uri\": \"failed\"}}" }; testAllPermutations(properties, json -> { ResponseMessage message = (ResponseMessage) handler.parseMessage(json); Assert.assertEquals("failed", message.getError().getMessage()); Object data = message.getError().getData(); JsonObject expected = new JsonObject(); expected.addProperty("uri", "failed"); Assert.assertEquals(expected, data); Assert.assertNull(message.getResult()); }); }
@Test public void testCancellation() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new DebugRemoteEndpoint(consumer, endp); endpoint.consume(new RequestMessage() {{ setId("1"); setMethod("foo"); setParams("myparam"); }}); Entry<RequestMessage, CompletableFuture<Object>> entry = endp.requests.entrySet().iterator().next(); entry.getValue().cancel(true); ResponseMessage message = (ResponseMessage) consumer.messages.get(0); assertNotNull(message); ResponseError error = message.getError(); assertNotNull(error); assertEquals(error.getCode(), ResponseErrorCode.RequestCancelled.getValue()); assertEquals(error.getMessage(), "The request (id: 1, method: 'foo') has been cancelled"); }
@Test public void testMissingSuccessResponse_AllOrders() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Location>() {}.getType(), new TypeToken<Void>() { }.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); String[] properties = new String[] { "\"seq\":2", "\"type\":\"response\"", "\"request_seq\":5", "\"message\": \"failed\"", "\"body\": {\"uri\": \"failed\"}" }; testAllPermutations(properties, json -> { ResponseMessage message = (ResponseMessage) handler.parseMessage(json); Assert.assertEquals("failed", message.getError().getMessage()); Object data = message.getError().getData(); Map<String, String> expected = new HashMap<>(); expected.put("uri", "failed"); Assert.assertEquals(expected, data); Assert.assertNull(message.getResult()); }); }
@Test public void testErrorResponse_AllOrders() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Location>() {}.getType(), new TypeToken<Void>() { }.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); String[] properties = new String[] { "\"seq\":2", "\"type\":\"response\"", "\"request_seq\":5", "\"success\":false", "\"message\": \"failed\"", "\"body\": {\"uri\": \"failed\"}" }; testAllPermutations(properties, json -> { ResponseMessage message = (ResponseMessage) handler.parseMessage(json); Assert.assertEquals("failed", message.getError().getMessage()); Object data = message.getError().getData(); Map<String, String> expected = new HashMap<>(); expected.put("uri", "failed"); Assert.assertEquals(expected, data); Assert.assertNull(message.getResult()); }); }
@Test public void testCancellation() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); endpoint.consume(init(new RequestMessage(), it -> { it.setId("1"); it.setMethod("foo"); it.setParams("myparam"); })); Entry<RequestMessage, CompletableFuture<Object>> entry = endp.requests.entrySet().iterator().next(); entry.getValue().cancel(true); ResponseMessage message = (ResponseMessage) consumer.messages.get(0); assertNotNull(message); ResponseError error = message.getError(); assertNotNull(error); assertEquals(error.getCode(), ResponseErrorCode.RequestCancelled.getValue()); assertEquals(error.getMessage(), "The request (id: 1, method: 'foo') has been cancelled"); }
assertEquals("Internal error.", response.getError().getMessage()); assertEquals(ResponseErrorCode.InternalError.getValue(), response.getError().getCode()); String exception = (String) response.getError().getData(); String expected = "java.lang.RuntimeException: BAAZ\n\tat org.eclipse.lsp4j.jsonrpc.test.RemoteEndpointTest"; assertEquals(expected, exception.substring(0, expected.length()));
@Test public void testNormalResponseExtraFields_AllOrders() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Location>() {}.getType(), new TypeToken<Void>() { }.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); String[] properties = new String[] { "\"seq\":2", "\"type\":\"response\"", "\"request_seq\":5", "\"success\":true", "\"body\": {\"uri\": \"dummy://mymodel.mydsl\"}", "\"message\": null" }; testAllPermutations(properties, json -> { ResponseMessage message = (ResponseMessage) handler.parseMessage(json); Object result = message.getResult(); Class<? extends Object> class1 = result.getClass(); Assert.assertEquals(Location.class, class1); Assert.assertEquals("dummy://mymodel.mydsl", ((Location)result).uri); Assert.assertNull(message.getError()); }); }
@Test public void testNormalResponse_AllOrders() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Location>() {}.getType(), new TypeToken<Void>() { }.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); String[] properties = new String[] { "\"seq\":2", "\"type\":\"response\"", "\"request_seq\":5", "\"success\":true", "\"body\": {\"uri\": \"dummy://mymodel.mydsl\"}" }; testAllPermutations(properties, json -> { ResponseMessage message = (ResponseMessage) handler.parseMessage(json); Object result = message.getResult(); Class<? extends Object> class1 = result.getClass(); Assert.assertEquals(Location.class, class1); Assert.assertEquals("dummy://mymodel.mydsl", ((Location)result).uri); Assert.assertNull(message.getError()); }); }
@Test public void testNormalResponse_AllOrders() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Location>() {}.getType(), new TypeToken<Void>() { }.getType())); MessageJsonHandler handler = new MessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); String[] properties = new String[] { "\"jsonrpc\":\"2.0\"", "\"id\":2", "\"result\": {\"uri\": \"dummy://mymodel.mydsl\"}" }; testAllPermutations(properties, json -> { ResponseMessage message = (ResponseMessage) handler.parseMessage(json); Object result = message.getResult(); Class<? extends Object> class1 = result.getClass(); Assert.assertEquals(Location.class, class1); Assert.assertEquals("dummy://mymodel.mydsl", ((Location)result).uri); Assert.assertNull(message.getError()); }); }
@Test public void testExceptionHandlerMisbehaving1() { LogMessageAccumulator logMessages = new LogMessageAccumulator(); try { // Don't show the exception in the test execution log logMessages.registerTo(RemoteEndpoint.class); TestEndpoint endp = new TestEndpoint() { @Override public CompletableFuture<Object> request(String method, Object parameter) { throw new RuntimeException("BAAZ"); } }; TestMessageConsumer consumer = new TestMessageConsumer(); // Misbehaving exception handler that returns null RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp, (e) -> null); endpoint.consume(init(new RequestMessage(), it -> { it.setId("1"); it.setMethod("foo"); it.setParams("myparam"); })); assertEquals("Check some response received", 1, consumer.messages.size()); ResponseMessage response = (ResponseMessage) consumer.messages.get(0); assertEquals(ResponseErrorCode.InternalError.getValue(), response.getError().getCode()); } finally { logMessages.unregister(); } }