### Question:
JsonRpcServlet extends ApiServlet { protected Object getJSONResponse(String key, ResponseItem responseItem) { Map<String, Object> result = Maps.newHashMap(); if (key != null) { result.put("id", key); } if (responseItem.getErrorCode() < 200 || responseItem.getErrorCode() >= 400) { result.put("error", getErrorJson(responseItem)); } else { Object response = responseItem.getResponse(); if (response instanceof DataCollection) { addResult(result, ((DataCollection) response).getEntry()); } else if (response instanceof RestfulCollection) { Map<String, Object> map = Maps.newHashMap(); RestfulCollection<?> collection = (RestfulCollection<?>) response; if (collection.getTotalResults() != collection.getList().size()) { map.put("startIndex", collection.getStartIndex()); map.put("itemsPerPage", collection.getItemsPerPage()); } map.put("totalResults", collection.getTotalResults()); map.put("filtered", collection.isFiltered()); map.put("updatedSince", collection.isUpdatedSince()); map.put("sorted", collection.isSorted()); map.put("list", collection.getList()); addResult(result, map); } else { addResult(result, response); } } return result; }   static final Set<String> ALLOWED_CONTENT_TYPES; static final String REQUEST_PARAM; }### Answer:
@Test public void testGetJsonResponseWithKey() throws Exception { ResponseItem responseItem = new ResponseItem("Name"); Object result = servlet.getJSONResponse("my-key", responseItem); JsonAssert.assertObjectEquals("{id: 'my-key', result: 'Name'}", result); }
@Test public void testGetJsonResponseWithoutKey() throws Exception { ResponseItem responseItem = new ResponseItem("Name"); Object result = servlet.getJSONResponse(null, responseItem); JsonAssert.assertObjectEquals("{result: 'Name'}", result); }
@Test public void testGetJsonResponseErrorWithData() throws Exception { ResponseItem responseItem = new ResponseItem(401, "Error Message", "Optional Data"); Object result = servlet.getJSONResponse(null, responseItem); JsonAssert.assertObjectEquals( "{error: {message: 'unauthorized: Error Message', data: 'Optional Data', code: 401}}", result); }
@Test public void testGetJsonResponseErrorWithoutData() throws Exception { ResponseItem responseItem = new ResponseItem(401, "Error Message"); Object result = servlet.getJSONResponse(null, responseItem); JsonAssert.assertObjectEquals( "{error: {message:'unauthorized: Error Message', code:401}}", result); }