JSONResponse { public static Response createResponse(Response.StatusType status, Object entity, String errormessage) { if (status.getFamily() != Response.Status.Family.SUCCESSFUL) { return INSTANCE.createErrorResponse(status, entity, errormessage); } return INSTANCE.createResponse(status, entity); } private  JSONResponse(); static Response createErrorResponse(Response.StatusType status, String errormessage); static Response createResponse(Response.StatusType status, Object entity, String errormessage);  }
@Test public void createMessageWithErrorStatusShouldCreateErrorResponse() { Response errorResponse = JSONResponse.createResponse(Status.INTERNAL_SERVER_ERROR, null, "error"); assertThat(errorResponse.getMediaType(), is(equalTo(MediaType.APPLICATION_JSON_TYPE))); assertThat(errorResponse.getStatus(), is(500)); JsonObject entity = ((JsonObject) errorResponse.getEntity()).get(JSONResponse.JSON_KEY_ERROR).getAsJsonObject(); assertThat(entity.get(JSONResponse.JSON_KEY_ERROR_MESSAGE).getAsString(), is("error")); assertThat(entity.get(JSONResponse.JSON_KEY_HTTPCODE).getAsInt(), is(500)); assertThat(entity.get(JSONResponse.JSON_KEY_ENTITY), is(nullValue())); }
@Test public void createMessageWithErrorStatusShouldCreateErrorResponseWithEntity() { Response errorResponse = JSONResponse.createResponse(Status.INTERNAL_SERVER_ERROR, ENTITY_VALUE, "error"); assertThat(errorResponse.getMediaType(), is(MediaType.APPLICATION_JSON_TYPE)); assertThat(errorResponse.getStatus(), is(500)); JsonObject resultJson = (JsonObject) errorResponse.getEntity(); assertThat(resultJson.get(JSONResponse.JSON_KEY_ENTITY).getAsString(), is(ENTITY_VALUE)); JsonObject errorJson = resultJson.get(JSONResponse.JSON_KEY_ERROR).getAsJsonObject(); assertThat(errorJson.get(JSONResponse.JSON_KEY_ERROR_MESSAGE).getAsString(), is("error")); assertThat(errorJson.get(JSONResponse.JSON_KEY_HTTPCODE).getAsInt(), is(500)); }
@Test public void shouldCreateSuccessResponseWithStreamEntity() throws IOException { Response response = JSONResponse.createResponse(Status.OK, ENTITY_VALUE, null); assertThat(response.getStatus(), is(200)); assertThat(response.getMediaType(), is(MediaType.APPLICATION_JSON_TYPE)); Object entity = response.getEntity(); assertThat(entity.getClass(), is(typeCompatibleWith(InputStream.class))); try (InputStream entityInStream = (InputStream) entity) { byte[] entityValue = new byte[ENTITY_JSON_VALUE.length()]; entityInStream.read(entityValue); assertThat(new String(entityValue), is(ENTITY_JSON_VALUE)); } }
@Test public void shouldCreateSuccessResponseWithNullEntity() throws Exception { Response response = JSONResponse.createResponse(Status.ACCEPTED, null, null); assertThat(response.getStatus(), is(202)); assertThat(response.getMediaType(), is(MediaType.APPLICATION_JSON_TYPE)); Object entity = response.getEntity(); assertThat(entity, is(nullValue())); }
@Test public void shouldCreateSuccessResponseWithLargeStreamEntity() throws IOException { Response response = JSONResponse.createResponse(Status.OK, new LargeEntity(), null); assertThat(response.getStatus(), is(200)); assertThat(response.getMediaType(), is(MediaType.APPLICATION_JSON_TYPE)); Object entity = response.getEntity(); assertThat(entity.getClass(), is(typeCompatibleWith(InputStream.class))); try (InputStream entityInStream = (InputStream) entity) { String largeEntityJSON = new String(entityInStream.readAllBytes(), StandardCharsets.UTF_8); assertThat(largeEntityJSON, is(notNullValue())); assertTrue(largeEntityJSON.startsWith("{")); assertTrue(largeEntityJSON.endsWith("}")); } }