public JsonRpcResponse(JsonElement id, JsonElement result, JsonElement error, int statusCode) { setId(id); setResult(result); setError(error); setStatusCode(statusCode); }
@Test public void getIdAsString_NullId_Null() { // given JsonRpcRequest req = new JsonRpcRequest(null, "something", null); // when String result = req.getIdAsString(); // then assertThat(result).isNull(); }
@Test public void testJsonResponseWithRpcException() { String jsonRpcException = "{\"category\":400,\"message\":\"You fool!\"," + "\"source\":\"com.sixt.service.foobar\",\"code\":\"SERVICE_PROTOBUF_ENUM\"," + "\"data\":\"my data\",\"retriable\":true}"; String input = "{\"error\":" + jsonRpcException + ",\"result\":{\"foo\":\"bar\"}}"; JsonRpcResponse response = JsonRpcResponse.fromString(input); assertThat(response.getError()).isNotNull(); assertThat(response.getStatusCode()).isEqualTo(HttpServletResponse.SC_BAD_REQUEST); }
@Test public void testJsonArrayError() { String input = "{\"error\":\"[{},{}]\",\"result\":{\"foo\":\"bar\"}}"; JsonRpcResponse response = JsonRpcResponse.fromString(input); assertThat(response.getStatusCode()).isEqualTo(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }
@Test public void verifyToString() { JsonRpcResponse response = new JsonRpcResponse(new JsonPrimitive(42), JsonNull.INSTANCE, new JsonPrimitive("none"), 200); assertThat(response.getId()).isEqualTo(new JsonPrimitive(42)); assertThat(response.toString()).contains("[id=42,result=null,error=\"none\"]"); }
private void readRpcError(String data) { try { JsonRpcResponse json = JsonRpcResponse.fromString(data); this.errorMessage = json.getError().toString(); } catch (Exception ex) { logger.warn("Caught exception parsing response"); } }
@Test public void testParseRpcRequestStringNumericId() { String input = "{\"service\":\"x\",\"method\":\"a\",\"params\":[{\"data\":\"\"}],\"id\":\"234234\"}"; JsonRpcRequest request = servlet.parseRpcRequest(input); JsonElement idElement = request.getId(); assertThat(idElement.toString()).isEqualTo("\"234234\""); }
public long extractTimestamp(JsonObject json, String name) { String timestamp = extractString(json, name); if (timestamp != null) { try { Instant instant = Instant.parse(timestamp); return instant.toEpochMilli(); } catch (Exception ex) { logger.info("Invalid timestamp: {}", timestamp); } } return 0; }
public int extractInteger(JsonObject json, String name, int defaultValue) { if (json != null) { int dotIndex = name.indexOf('.'); if (dotIndex > 0) { String baseName = name.substring(0, dotIndex); JsonElement childElement = json.get(baseName); return extractInteger((JsonObject) childElement, name.substring(dotIndex + 1), defaultValue); } JsonElement element = json.get(name); if (element != null && ! element.isJsonNull()) { return element.getAsInt(); } } return defaultValue; }
public double extractDouble(JsonObject json, String name, int defaultValue) { if (json != null) { int dotIndex = name.indexOf('.'); if (dotIndex > 0) { String baseName = name.substring(0, dotIndex); JsonElement childElement = json.get(baseName); return extractDouble((JsonObject) childElement, name.substring(dotIndex + 1), defaultValue); } JsonElement element = json.get(name); if (element != null && ! element.isJsonNull()) { return element.getAsDouble(); } } return defaultValue; }
@Test public void getIdAsString_NullJson_Null() { // given JsonRpcRequest req = new JsonRpcRequest(JsonNull.INSTANCE, "something", null); // when String result = req.getIdAsString(); // then assertThat(result).isNull(); } }
@Test public void testJsonResponseWithSuccessCode() { String input = "{\"error\":\"\",\"result\":{\"foo\":\"bar\"}}"; JsonRpcResponse response = JsonRpcResponse.fromString(input); assertThat(response.getStatusCode()).isEqualTo(HttpServletResponse.SC_OK); }
@Test public void testParseRpcRequestStringId() { String input = "{\"service\":\"x\",\"method\":\"a\",\"params\":[{\"data\":\"\"}],\"id\":\"dead-beef\"}"; JsonRpcRequest request = servlet.parseRpcRequest(input); JsonElement idElement = request.getId(); assertThat(idElement.toString()).isEqualTo("\"dead-beef\""); }
public String extractString(JsonObject json, String name) { if (json != null) { int dotIndex = name.indexOf('.'); if (dotIndex > 0) { String baseName = name.substring(0, dotIndex); JsonElement childElement = json.get(baseName); return extractString((JsonObject) childElement, name.substring(dotIndex + 1)); } JsonElement element = json.get(name); if (element != null && ! element.isJsonNull()) { return element.getAsString(); } } return null; }
@Test public void getIdAsString_SomeIdAsNumber_TheNumberAsString() { // given JsonElement jsonElement = new JsonPrimitive(20); JsonRpcRequest req = new JsonRpcRequest(jsonElement, "something", null); // when String result = req.getIdAsString(); // then assertThat(result).isNotNull(); assertThat(result).isEqualTo("20"); }
@Test public void testParseRpcRequestNumericId() { String input = "{\"service\":\"x\",\"method\":\"a\",\"params\":[{\"data\":\"\"}],\"id\":123456789}"; JsonRpcRequest request = servlet.parseRpcRequest(input); JsonElement idElement = request.getId(); assertThat(idElement.toString()).isEqualTo("123456789"); }
@Test public void testParseRpcRequestNoId() { String input = "{\"service\":\"x\",\"method\":\"a\",\"params\":[{\"data\":\"\"}]}"; JsonRpcRequest request = servlet.parseRpcRequest(input); assertThat(request.getId()).isNull(); }
@Test public void testParseRpcRequestNullId() { String input = "{\"service\":\"x\",\"method\":\"a\",\"params\":[{\"data\":\"\"}],\"id\":null}"; JsonRpcRequest request = servlet.parseRpcRequest(input); assertThat(request.getId()).isInstanceOf(JsonNull.class); }