@Test public void responseTest() { Data data = new Data(); data.data1 = "Value1"; data.data2 = "Value2"; Response<Data> request = new Response<Data>(1, data); String requestJson = JsonUtils.toJsonResponse(request); log.info(requestJson); Response<Data> newRequest = JsonUtils.fromJsonResponse(requestJson, Data.class); Assert.assertEquals(data.data1, newRequest.getResult().data1); Assert.assertEquals(data.data2, newRequest.getResult().data2); }
@Test public void noResultResponseTest() { Response<Void> response = new Response<Void>(1); response.setSessionId("xxxxxxx"); String responseJson = response.toString(); JsonParser parser = new JsonParser(); JsonObject expected = (JsonObject) parser.parse("{\"id\":1,\"result\":{\"sessionId\":\"xxxxxxx\"},\"jsonrpc\":\"2.0\"}"); JsonObject result = (JsonObject) parser.parse(responseJson); Assert.assertEquals(expected, result); log.info(responseJson); Response<Void> newResponse = JsonUtils.fromJsonResponse(responseJson, Void.class); // Assert.assertEquals(null, newResponse.getResult()); Assert.assertEquals(newResponse.getSessionId(), "xxxxxxx"); }
@Test public void responseTest() { Data data = new Data(); data.data1 = "Value1"; Response<Data> response = new Response<Data>(1, data); response.setSessionId("xxxxxxx"); String responseJson = response.toString(); Assert.assertEquals( "{\"id\":1,\"result\":{\"data1\":\"Value1\",\"sessionId\":\"xxxxxxx\"},\"jsonrpc\":\"2.0\"}", responseJson); log.info(responseJson); Response<Data> newResponse = JsonUtils.fromJsonResponse(responseJson, Data.class); Assert.assertEquals(data.data1, newResponse.getResult().data1); Assert.assertEquals(newResponse.getSessionId(), "xxxxxxx"); }