ResponseLoggerHelper { public static String toResponseTrace(final Response response) { final Optional<Integer> responseCode = Optional.ofNullable(Integer.valueOf(response.getStatus())); final Optional<MediaType> mediaType = Optional.ofNullable(response.getMediaType()); final Optional<String> cppid = Optional.ofNullable(response.getHeaderString(ID)); final JsonObjectBuilder builder = Json.createObjectBuilder(); responseCode.ifPresent(integer -> builder.add("ResponseCode", integer)); mediaType.ifPresent(s -> builder.add("MediaType", s.getType())); cppid.ifPresent(s -> builder.add(ID, s)); return builder.build().toString(); } private  ResponseLoggerHelper(); static String toResponseTrace(final Response response);  }
@Test public void shouldPrintResponseParameters() { when(response.getHeaderString(ID)).thenReturn(CPP_ID); when(response.getMediaType()).thenReturn(mediaType); when(response.getStatus()).thenReturn(STATUS_CODE); when(mediaType.getType()).thenReturn(MEDIA_TYPE); JsonAssert.with(toResponseTrace(response)) .assertEquals("MediaType", MEDIA_TYPE) .assertEquals(ID, CPP_ID) .assertEquals("ResponseCode", STATUS_CODE); }
@Test public void shouldNotPrintMissingResponseParameters() { when(response.getHeaderString(ID)).thenReturn(CPP_ID); when(response.getMediaType()).thenReturn(null); when(response.getStatus()).thenReturn(404); JsonAssert.with(toResponseTrace(response)) .assertNotDefined("MediaType") .assertEquals(ID, CPP_ID) .assertEquals("ResponseCode", 404); }