/** * Serialize a JSON-RPC request into a ByteBuffer for use as a Lambda request payload. * * @throws IOException if serializing the request payload fails */ private ByteBuffer writeRequest(String methodName, Object argument) throws IOException { ByteArrayOutputStream requestStream = new ByteArrayOutputStream(); streamClient.invoke(methodName, argument, requestStream); log.trace("Raw request payload: " + requestStream.toString()); return ByteBuffer.wrap(requestStream.toByteArray()); }
/** * Invokes the given method on the remote service passing * the given argument. An id is generated automatically. To read * the response {@link #readResponse(Type, InputStream)} must be * subsequently called. * * @param methodName the method to invoke * @param argument the arguments to pass to the method * @param output the {@link OutputStream} to write to * @throws IOException on error * @see #writeRequest(String, Object, OutputStream, String) */ public void invoke(String methodName, Object argument, OutputStream output) throws IOException { invoke(methodName, argument, output, this.requestIDGenerator.generateID()); }
/** * Invokes the given method on the remote service * passing the given arguments and reads a response. * * @param methodName the method to invoke * @param argument the argument to pass to the method * @param returnType the expected return type * @param output the {@link OutputStream} to write to * @param input the {@link InputStream} to read from * @param id id to send with the JSON-RPC request * @return the returned Object * @throws Throwable if there is an error * while reading the response * @see #writeRequest(String, Object, OutputStream, String) */ private Object invokeAndReadResponse(String methodName, Object argument, Type returnType, OutputStream output, InputStream input, String id) throws Throwable { invoke(methodName, argument, output, id); return readResponse(returnType, input, id); }
@Test public void testInvokeHashParams() throws Throwable { Map<String, Object> params = new HashMap<>(); params.put("hello", "test"); params.put("x", 1); client.invoke("test", params, byteArrayOutputStream); JsonNode node = readJSON(byteArrayOutputStream); assertTrue(node.has(PARAMS)); assertTrue(node.get(PARAMS).isObject()); assertEquals("test", node.get(PARAMS).get("hello").textValue()); assertEquals(1, node.get(PARAMS).get("x").intValue()); }
@Test public void testRandomIDGeneration() throws IOException { Map<String, Object> params = new HashMap<>(); params.put("hello", "test"); params.put("x", 1); client.invoke("test", params, byteArrayOutputStream); JsonNode node = readJSON(byteArrayOutputStream); assertTrue(node.has(PARAMS)); assertTrue(node.get(PARAMS).isObject()); try { Long.parseLong(node.get(ID).asText()); } catch (NumberFormatException e) { fail(); } }
@Test public void testInvokeNoParams() throws Throwable { client.invoke("test", new Object[0], byteArrayOutputStream); JsonNode node = readJSON(byteArrayOutputStream); assertFalse(node.has(PARAMS)); }
@SuppressWarnings("unused") public void testMultipleClientCallsBeforeReadResponse() throws Throwable { StreamServer streamServer = createAndStartServer(); Socket socket = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort()); InputStream ips = socket.getInputStream(); OutputStream ops = socket.getOutputStream(); for (int i = 0; i < 10; i++) { jsonRpcClient.invoke("inc", null, ops); } for (int i = 0; i < 10; i++) { Integer value = jsonRpcClient.readResponse(Integer.class, ips); assertEquals(i, value.intValue()); } socket.close(); while (streamServer.getNumberOfConnections() > 0) { Thread.yield(); } streamServer.stop(); }
@Test public void testIDGeneration() throws IOException { client.setRequestIDGenerator(new RequestIDGenerator() { @Override public String generateID() { return "test"; } }); Map<String, Object> params = new HashMap<>(); params.put("hello", "test"); params.put("x", 1); client.invoke("test", params, byteArrayOutputStream); JsonNode node = readJSON(byteArrayOutputStream); assertTrue(node.has(PARAMS)); assertTrue(node.get(PARAMS).isObject()); assertEquals("test", node.get(ID).asText()); }
@Test public void testInvokeArrayParams() throws Throwable { client.invoke("test", new Object[]{1, 2}, byteArrayOutputStream); JsonNode node = readJSON(byteArrayOutputStream); assertTrue(node.has(PARAMS)); assertTrue(node.get(PARAMS).isArray()); assertEquals(1, node.get(PARAMS).get(0).intValue()); assertEquals(2, node.get(PARAMS).get(1).intValue()); }
@Test public void testInvokeAdditionalJsonContent() throws Throwable { final String auth = "auth"; final String authValue = "secret"; client.setAdditionalJsonContent(new HashMap<String, Object>() { { put(auth, authValue); } }); client.invoke("test", new Object[]{1, 2}, byteArrayOutputStream); JsonNode node = readJSON(byteArrayOutputStream); assertTrue(node.has(auth)); assertEquals(node.get(auth).textValue(), authValue); }