@Test public void testRequest() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); endpoint.consume(new RequestMessage() {{ setId("1"); setMethod("foo"); setParams("myparam"); }}); Entry<RequestMessage, CompletableFuture<Object>> entry = endp.requests.entrySet().iterator().next(); entry.getValue().complete("success"); assertEquals("foo", entry.getKey().getMethod()); assertEquals("myparam", entry.getKey().getParams()); assertEquals("success", ((ResponseMessage)consumer.messages.get(0)).getResult()); }
@Test public void testCancellation() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new RemoteEndpoint(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"); }
@SuppressWarnings({ "unchecked" }) @Test public void testParseList() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<List<? extends Entry>>() {}.getType(), new TypeToken<List<? extends Entry>>() {}.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id)->"foo"); Message message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + " \"body\": [\n" + " {\"name\":\"$schema\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":1,\"character\":3},\"end\":{\"line\":1,\"character\":55}}}},\n" + " {\"name\":\"type\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":2,\"character\":3},\"end\":{\"line\":2,\"character\":19}}}},\n" + " {\"name\":\"title\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":3,\"character\":3},\"end\":{\"line\":3,\"character\":50}}}},\n" + " {\"name\":\"additionalProperties\",\"kind\":17,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":4,\"character\":4},\"end\":{\"line\":4,\"character\":32}}}},\n" + " {\"name\":\"properties\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":5,\"character\":3},\"end\":{\"line\":5,\"character\":20}}}}\n" + "]}"); List<? extends Entry> result = (List<? extends Entry>) ((ResponseMessage)message).getResult(); Assert.assertEquals(5, result.size()); for (Entry e : result) { Assert.assertTrue(e.location.uri, e.location.uri.startsWith("file:/home/mistria")); } }
@SuppressWarnings({ "unchecked" }) @Test public void testParseList_02() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Set<Entry>>() {}.getType(), new TypeToken<Set<Entry>>() {}.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id)->"foo"); Message message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + " \"body\": [\n" + " {\"name\":\"$schema\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":1,\"character\":3},\"end\":{\"line\":1,\"character\":55}}}},\n" + " {\"name\":\"type\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":2,\"character\":3},\"end\":{\"line\":2,\"character\":19}}}},\n" + " {\"name\":\"title\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":3,\"character\":3},\"end\":{\"line\":3,\"character\":50}}}},\n" + " {\"name\":\"additionalProperties\",\"kind\":17,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":4,\"character\":4},\"end\":{\"line\":4,\"character\":32}}}},\n" + " {\"name\":\"properties\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":5,\"character\":3},\"end\":{\"line\":5,\"character\":20}}}}\n" + "]}"); Set<Entry> result = (Set<Entry>) ((ResponseMessage)message).getResult(); Assert.assertEquals(5, result.size()); for (Entry e : result) { Assert.assertTrue(e.location.uri, e.location.uri.startsWith("file:/home/mistria")); } }
@SuppressWarnings({ "unchecked" }) @Test public void testEither_02() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Either<MyEnum, Map<String,String>>>() {}.getType(), new TypeToken<Object>() {}.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); Message message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + "\"body\": 2\n" + "}"); Either<MyEnum, List<Map<String, String>>> result = (Either<MyEnum, List<Map<String,String>>>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isLeft()); Assert.assertEquals(MyEnum.B, result.getLeft()); }
@Test public void testRequest() { 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().complete("success"); assertEquals("foo", entry.getKey().getMethod()); assertEquals("myparam", entry.getKey().getParams()); assertEquals("success", ((ResponseMessage)consumer.messages.get(0)).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"); }
@SuppressWarnings({ "unchecked" }) @Test public void testEither_02() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Either<Integer, Map<String,String>>>() {}.getType(), new TypeToken<Object>() {}.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); Message message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + "\"body\": 2\n" + "}"); Either<Integer, List<Map<String, String>>> result = (Either<Integer, List<Map<String,String>>>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isLeft()); Assert.assertEquals(Integer.valueOf(2), result.getLeft()); }
@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()); }); }
@Override public CompletableFuture<Object> request(String method, Object parameter) { RequestMessage requestMessage = createRequestMessage(method, parameter); final String id = requestMessage.getId(); final CompletableFuture<Object> result = new CompletableFuture<Object>() { @Override public boolean cancel(boolean mayInterruptIfRunning) { sendCancelNotification(id); return super.cancel(mayInterruptIfRunning); } }; Consumer<ResponseMessage> responseHandler = (responseMessage) -> { if (responseMessage.getError() != null) { result.completeExceptionally(new ResponseErrorException(responseMessage.getError())); } else { result.complete(responseMessage.getResult()); } }; synchronized(sentRequestMap) { sentRequestMap.put(id, new PendingRequestInfo(requestMessage, responseHandler)); } out.consume(requestMessage); return result; }
@SuppressWarnings({ "unchecked" }) @Test public void testParseList() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<List<? extends Entry>>() {}.getType(), new TypeToken<List<? extends Entry>>() {}.getType())); MessageJsonHandler handler = new MessageJsonHandler(supportedMethods); handler.setMethodProvider((id)->"foo"); Message message = handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + " \"result\": [\n" + " {\"name\":\"$schema\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":1,\"character\":3},\"end\":{\"line\":1,\"character\":55}}}},\n" + " {\"name\":\"type\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":2,\"character\":3},\"end\":{\"line\":2,\"character\":19}}}},\n" + " {\"name\":\"title\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":3,\"character\":3},\"end\":{\"line\":3,\"character\":50}}}},\n" + " {\"name\":\"additionalProperties\",\"kind\":17,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":4,\"character\":4},\"end\":{\"line\":4,\"character\":32}}}},\n" + " {\"name\":\"properties\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":5,\"character\":3},\"end\":{\"line\":5,\"character\":20}}}}\n" + "]}"); List<? extends Entry> result = (List<? extends Entry>) ((ResponseMessage)message).getResult(); Assert.assertEquals(5, result.size()); for (Entry e : result) { Assert.assertTrue(e.location.uri, e.location.uri.startsWith("file:/home/mistria")); } }
@SuppressWarnings({ "unchecked" }) @Test public void testParseList_02() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Set<Entry>>() {}.getType(), new TypeToken<Set<Entry>>() {}.getType())); MessageJsonHandler handler = new MessageJsonHandler(supportedMethods); handler.setMethodProvider((id)->"foo"); Message message = handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + " \"result\": [\n" + " {\"name\":\"$schema\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":1,\"character\":3},\"end\":{\"line\":1,\"character\":55}}}},\n" + " {\"name\":\"type\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":2,\"character\":3},\"end\":{\"line\":2,\"character\":19}}}},\n" + " {\"name\":\"title\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":3,\"character\":3},\"end\":{\"line\":3,\"character\":50}}}},\n" + " {\"name\":\"additionalProperties\",\"kind\":17,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":4,\"character\":4},\"end\":{\"line\":4,\"character\":32}}}},\n" + " {\"name\":\"properties\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":5,\"character\":3},\"end\":{\"line\":5,\"character\":20}}}}\n" + "]}"); Set<Entry> result = (Set<Entry>) ((ResponseMessage)message).getResult(); Assert.assertEquals(5, result.size()); for (Entry e : result) { Assert.assertTrue(e.location.uri, e.location.uri.startsWith("file:/home/mistria")); } }
@SuppressWarnings({ "unchecked" }) @Test public void testEither_02() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Either<MyEnum, Map<String,String>>>() {}.getType(), new TypeToken<Object>() {}.getType())); MessageJsonHandler handler = new MessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); Message message = handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + "\"result\": 2\n" + "}"); Either<MyEnum, List<Map<String, String>>> result = (Either<MyEnum, List<Map<String,String>>>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isLeft()); Assert.assertEquals(MyEnum.B, result.getLeft()); }
@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 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(); Map<String, String> expected = new HashMap<>(); expected.put("uri", "failed"); Assert.assertEquals(expected, data); Assert.assertNull(message.getResult()); }); }
@SuppressWarnings({ "unchecked" }) @Test public void testEither_01() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Either<String, List<Map<String,String>>>>() {}.getType(), new TypeToken<Either<String, Integer>>() {}.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); Message message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + " \"body\": [\n" + " {\"name\":\"foo\"},\n" + " {\"name\":\"bar\"}\n" + "]}"); Either<String, List<Map<String, String>>> result = (Either<String, List<Map<String,String>>>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isRight()); for (Map<String, String> e : result.getRight()) { Assert.assertNotNull(e.get("name")); } message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + "\"body\": \"name\"\n" + "}"); result = (Either<String, List<Map<String,String>>>) ((ResponseMessage)message).getResult(); Assert.assertFalse(result.isRight()); Assert.assertEquals("name",result.getLeft()); }
@SuppressWarnings({ "unchecked" }) @Test public void testEither_04() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Either<MyClass, List<? extends MyClass>>>() {}.getType(), new TypeToken<Object>() {}.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); Message message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + "\"body\": {\n" + " value:\"foo\"\n" + "}}"); Either<MyClass, List<? extends MyClass>> result = (Either<MyClass, List<? extends MyClass>>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isLeft()); Assert.assertEquals("foo", result.getLeft().getValue()); message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + "\"body\": [{\n" + " value:\"bar\"\n" + "}]}"); result = (Either<MyClass, List<? extends MyClass>>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isRight()); Assert.assertEquals("bar", result.getRight().get(0).getValue()); }
@SuppressWarnings({ "unchecked" }) @Test public void testEither_05() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Either<List<MyClass>, MyClassList>>() {}.getType(), new TypeToken<Object>() {}.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); Message message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + "\"body\": [{\n" + " value:\"foo\"\n" + "}]}"); Either<List<MyClass>, MyClassList> result = (Either<List<MyClass>, MyClassList>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isLeft()); Assert.assertEquals("foo", result.getLeft().get(0).getValue()); message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + "\"body\": {\n" + " items: [{\n" + " value:\"bar\"\n" + "}]}}"); result = (Either<List<MyClass>, MyClassList>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isRight()); Assert.assertEquals("bar", result.getRight().getItems().get(0).getValue()); }
@Override public CompletableFuture<Object> request(String method, Object parameter) { RequestMessage requestMessage = new RequestMessage(); final String id = String.valueOf(nextRequestId.incrementAndGet()); requestMessage.setId(id); requestMessage.setMethod(method); requestMessage.setParams(parameter); final CompletableFuture<Object> result = new CompletableFuture<Object>() { @Override public boolean cancel(boolean mayInterruptIfRunning) { sendCancelNotification(id); return super.cancel(mayInterruptIfRunning); } }; Consumer<ResponseMessage> responseHandler = (responseMessage) -> { if (responseMessage.getError() != null) { result.completeExceptionally(new ResponseErrorException(responseMessage.getError())); } else { result.complete(responseMessage.getResult()); } }; synchronized(sentRequestMap) { sentRequestMap.put(id, new PendingRequestInfo(requestMessage, responseHandler)); } out.consume(requestMessage); return result; }
@Override public void consume(Message message) { if (message instanceof NotificationMessage) { NotificationMessage notificationMessage = (NotificationMessage) message; handleNotification(notificationMessage); } else if (message instanceof RequestMessage) { RequestMessage requestMessage = (RequestMessage) message; handleRequest(requestMessage); } else if (message instanceof ResponseMessage) { ResponseMessage responseMessage = (ResponseMessage) message; handleResponse(responseMessage); } }
protected void handleResponse(ResponseMessage responseMessage) { PendingRequestInfo pendingRequestInfo; synchronized (sentRequestMap) { pendingRequestInfo = sentRequestMap.remove(responseMessage.getId()); } if (pendingRequestInfo == null) { LOG.log(Level.WARNING, "Unmatched response message: " + responseMessage); } else { try { pendingRequestInfo.responseHandler.accept(responseMessage); } catch (RuntimeException e) { LOG.log(Level.WARNING, "Handling repsonse threw an exception: " + responseMessage, e); } } }
@Test public void testInvalidCodeLens() { ResponseMessage message = new ResponseMessage(); message.setId("1"); CodeLens codeLens = new CodeLens(new Range(new Position(3, 32), new Position(3, 35)), null, null); // forbidden self reference! codeLens.setData(codeLens); message.setResult(codeLens); assertIssues(message, "An element of the message has a direct or indirect reference to itself."); }
@Test public void testResponseError() { ResponseMessage _responseMessage = new ResponseMessage(); final Procedure1<ResponseMessage> _function = (ResponseMessage it) -> { it.setJsonrpc("2.0"); it.setId("12"); ResponseError _responseError = new ResponseError(); final Procedure1<ResponseError> _function_1 = (ResponseError it_1) -> { it_1.setCode(ResponseErrorCode.InvalidRequest); it_1.setMessage("Could not parse request."); }; ResponseError _doubleArrow = ObjectExtensions.<ResponseError>operator_doubleArrow(_responseError, _function_1); it.setError(_doubleArrow); }; final ResponseMessage message = ObjectExtensions.<ResponseMessage>operator_doubleArrow(_responseMessage, _function); StringConcatenation _builder = new StringConcatenation(); _builder.append("{"); _builder.newLine(); _builder.append(" "); _builder.append("\"jsonrpc\": \"2.0\","); _builder.newLine(); _builder.append(" "); _builder.append("\"id\": \"12\","); _builder.newLine(); _builder.append(" "); _builder.append("\"error\": {"); _builder.newLine(); _builder.append(" "); _builder.append("\"code\": -32600,"); _builder.newLine(); _builder.append(" "); _builder.append("\"message\": \"Could not parse request.\""); _builder.newLine(); _builder.append(" "); _builder.append("}"); _builder.newLine(); _builder.append("}"); _builder.newLine(); this.assertSerialize(message, _builder); }
@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()); }); }
protected void handleResponse(ResponseMessage responseMessage) { PendingRequestInfo pendingRequestInfo; synchronized (sentRequestMap) { pendingRequestInfo = sentRequestMap.remove(responseMessage.getId()); } if (pendingRequestInfo == null) { LOG.log(Level.WARNING, "Unmatched response message: " + responseMessage); } else { try { pendingRequestInfo.responseHandler.accept(responseMessage); } catch (RuntimeException e) { LOG.log(Level.WARNING, "Handling response threw an exception: " + responseMessage, e); } } }
@SuppressWarnings({ "unchecked" }) @Test public void testEither_01() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Either<String, List<Map<String,String>>>>() {}.getType(), new TypeToken<Either<String, Integer>>() {}.getType())); MessageJsonHandler handler = new MessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); Message message = handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + " \"result\": [\n" + " {\"name\":\"foo\"},\n" + " {\"name\":\"bar\"}\n" + "]}"); Either<String, List<Map<String, String>>> result = (Either<String, List<Map<String,String>>>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isRight()); for (Map<String, String> e : result.getRight()) { Assert.assertNotNull(e.get("name")); } message = handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + "\"result\": \"name\"\n" + "}"); result = (Either<String, List<Map<String,String>>>) ((ResponseMessage)message).getResult(); Assert.assertFalse(result.isRight()); Assert.assertEquals("name",result.getLeft()); }
@SuppressWarnings({ "unchecked" }) @Test public void testEither_04() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Either<MyClass, List<? extends MyClass>>>() {}.getType(), new TypeToken<Object>() {}.getType())); MessageJsonHandler handler = new MessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); Message message = handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + "\"result\": {\n" + " value:\"foo\"\n" + "}}"); Either<MyClass, List<? extends MyClass>> result = (Either<MyClass, List<? extends MyClass>>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isLeft()); Assert.assertEquals("foo", result.getLeft().getValue()); message = handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + "\"result\": [{\n" + " value:\"bar\"\n" + "}]}"); result = (Either<MyClass, List<? extends MyClass>>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isRight()); Assert.assertEquals("bar", result.getRight().get(0).getValue()); }
@SuppressWarnings({ "unchecked" }) @Test public void testEither_05() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Either<List<MyClass>, MyClassList>>() {}.getType(), new TypeToken<Object>() {}.getType())); MessageJsonHandler handler = new MessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); Message message = handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + "\"result\": [{\n" + " value:\"foo\"\n" + "}]}"); Either<List<MyClass>, MyClassList> result = (Either<List<MyClass>, MyClassList>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isLeft()); Assert.assertEquals("foo", result.getLeft().get(0).getValue()); message = handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + "\"result\": {\n" + " items: [{\n" + " value:\"bar\"\n" + "}]}}"); result = (Either<List<MyClass>, MyClassList>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isRight()); Assert.assertEquals("bar", result.getRight().getItems().get(0).getValue()); }
@Test public void testException() { 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(); RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); endpoint.consume(new RequestMessage() {{ setId("1"); setMethod("foo"); setParams("myparam"); }}); ResponseMessage response = (ResponseMessage) consumer.messages.get(0); 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())); } finally { logMessages.unregister(); } }
@Test public void testExceptionHandlerMisbehaving01() { 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(new RequestMessage() {{ setId("1"); setMethod("foo"); 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(); } }
@Test public void testExceptionHandlerMisbehaving02() { 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) { return CompletableFuture.supplyAsync(() -> "baz"); } }; TestMessageConsumer2 consumer = new TestMessageConsumer2(); // Misbehaving exception handler that returns null RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp, (e) -> null); endpoint.consume(new RequestMessage() {{ setId("1"); setMethod("foo"); setParams("myparam"); }}); assertEquals("Check some response received", 1, consumer.messages.size()); ResponseMessage response = (ResponseMessage) consumer.messages.get(0); assertNotNull("Check response has error", response.getError()); assertEquals(ResponseErrorCode.InternalError.getValue(), response.getError().getCode()); } finally { logMessages.unregister(); } }
@SuppressWarnings({ "unchecked" }) @Test public void testEither_03() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Either<Either<MyEnum, Map<String,String>>, List<Either<MyEnum, Map<String,String>>>>>() {}.getType(), new TypeToken<Object>() {}.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); Message message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + "\"body\": 2\n" + "}"); Either<Either<MyEnum, Map<String,String>>, List<Either<MyEnum, Map<String,String>>>> result = (Either<Either<MyEnum, Map<String, String>>, List<Either<MyEnum, Map<String, String>>>>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isLeft()); Assert.assertTrue(result.getLeft().isLeft()); Assert.assertEquals(MyEnum.B, result.getLeft().getLeft()); message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + " \"body\": {\n" + " \"foo\":\"1\",\n" + " \"bar\":\"2\"\n" + "}}"); result = (Either<Either<MyEnum, Map<String, String>>, List<Either<MyEnum, Map<String, String>>>>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isLeft()); Assert.assertTrue(result.getLeft().isRight()); Assert.assertEquals("1", result.getLeft().getRight().get("foo")); Assert.assertEquals("2", result.getLeft().getRight().get("bar")); message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + " \"body\": [{\n" + " \"foo\":\"1\",\n" + " \"bar\":\"2\"\n" + "}]}"); result = (Either<Either<MyEnum, Map<String, String>>, List<Either<MyEnum, Map<String, String>>>>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isRight()); Assert.assertTrue(result.getRight().get(0).isRight()); Assert.assertEquals("1", result.getRight().get(0).getRight().get("foo")); Assert.assertEquals("2", result.getRight().get(0).getRight().get("bar")); message = handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"response\",\n" + " \"body\": [\n" + " 2\n" + "]}"); result = (Either<Either<MyEnum, Map<String, String>>, List<Either<MyEnum, Map<String, String>>>>) ((ResponseMessage)message).getResult(); Assert.assertTrue(result.isRight()); Assert.assertTrue(result.getRight().get(0).isLeft()); Assert.assertEquals(MyEnum.B, result.getRight().get(0).getLeft()); }