### Question:
Response extends BaseResponse { @Override public JsonNode toJson() { ObjectNode node=(ObjectNode)super.toJson(); if(entityData!=null) { node.set(PROPERTY_PROCESSED, entityData); } if(resultMetadata!=null) { ArrayNode arr=jsonNodeFactory.arrayNode(); for(ResultMetadata x:resultMetadata) { arr.add(x==null?jsonNodeFactory.nullNode():x.toJson()); } node.set(PROPERTY_RESULT_METADATA,arr); } return node; } Response(JsonNodeFactory jsonNodeFactory, OperationStatus status);  Response(Response r);  Response(BaseResponse r); void setResultSizeThresholds(int maxResultSetSizeB, int warnResultSetSizeB, final Request forRequest); JsonNode getEntityData(); void setEntityData(JsonNode node); void addEntityData(JsonNode doc); List<ResultMetadata> getResultMetadata(); void setResultMetadata(List<ResultMetadata> l); @Override JsonNode toJson(); int getResponseDataSizeB(); static final String ERR_RESULT_SIZE_TOO_LARGE; }### Answer:
@Test public void testWithDataErrors() { ObjectNode objNode = JsonObject.getFactory().objectNode(); ArrayNode arr = JsonObject.getFactory().arrayNode(); for (DataError err : getPopulatedDataErrors(3)) { arr.add(err.toJson()); } objNode.set("dataErrors", arr); builder.withDataErrors(arr); for (int i = 0; i < builder.buildResponse().getDataErrors().size(); i++) { DataError de = builder.buildResponse().getDataErrors().get(i); for (int j = 0; j < de.getErrors().size(); j++) { Error error = de.getErrors().get(j); assertTrue(error.getErrorCode().equals(getPopulatedErrors(3).get(j).getErrorCode())); } } }
@Test public void testWithErrors() { ObjectNode objNode = JsonObject.getFactory().objectNode(); ArrayNode arr = JsonObject.getFactory().arrayNode(); for (Error err : getPopulatedErrors(3)) { arr.add(err.toJson()); } objNode.set("errors", arr); builder.withErrors(arr); for (int i = 0; i < builder.buildResponse().getErrors().size(); i++) { Error e = builder.buildResponse().getErrors().get(i); assertTrue(e.getErrorCode().equals(getPopulatedErrors(3).get(i).getErrorCode())); } }
@Test public void testToJson() { response.setStatus(OperationStatus.COMPLETE); response.setModifiedCount(Integer.MAX_VALUE); response.setMatchCount(Integer.MIN_VALUE); response.setTaskHandle("taskHandle"); response.setSessionInfo(null); response.setEntityData(JsonObject.getFactory().objectNode()); response.getDataErrors().addAll(getPopulatedDataErrors(3)); response.getErrors().addAll(getPopulatedErrors(3)); response.setHostname("localhost"); ObjectNode expectedNode = JsonObject.getFactory().objectNode(); expectedNode.put("status", OperationStatus.COMPLETE.name().toLowerCase()); expectedNode.put("modifiedCount", Integer.MAX_VALUE); expectedNode.put("matchCount", Integer.MIN_VALUE); expectedNode.put("taskHandle", "taskHandle"); expectedNode.set("session", JsonObject.getFactory().objectNode()); expectedNode.set("entityData", JsonObject.getFactory().objectNode()); expectedNode.put("hostname", "localhost"); ArrayNode arr = JsonObject.getFactory().arrayNode(); expectedNode.set("dataErrors", arr); for (DataError err : getPopulatedDataErrors(3)) { arr.add(err.toJson()); } ArrayNode arr2 = JsonObject.getFactory().arrayNode(); expectedNode.set("errors", arr2); for (Error err : getPopulatedErrors(3)) { arr2.add(err.toJson()); } assertFalse(response.toJson().equals(expectedNode)); }
@Test public void testBuildJsonNull() throws UnknownHostException { ObjectNode expectedNode = JsonObject.getFactory().objectNode(); expectedNode.put("modifiedCount", 0L); expectedNode.put("matchCount", 0L); expectedNode.put("hostname", InetAddress.getLocalHost().getHostName()); expectedNode.put("status","COMPLETE"); assertTrue(response.toJson().equals(expectedNode)); }