### Question:
Xs2aHttpLogSanitizer { public String sanitizeResponseBody(Object responseBody, String contentType) { if (contentType.startsWith(APPLICATION_JSON)) { try { String json; if (responseBody instanceof String) { json = (String) responseBody; } else { json = objectMapper.writeValueAsString(responseBody); } return sanitizeStringifiedJsonBody(json); } catch (Exception e) { logger.error("Can't parse response as json. It will be replaced with {}", REPLACEMENT); } } return REPLACEMENT; } Xs2aHttpLogSanitizer(); String sanitizeHeader(String name, String value); String sanitize(String data); String sanitizeRequestBody(HttpEntity entity, String contentType); String sanitizeResponseBody(Object responseBody, String contentType);  }### Answer:
@Test void sanitizeResponseBody() throws IOException { String json = "{" + "\"consentStatus\":\"received\"," + "\"consentId\":\"40b01787-a5eb-48c0-bc38-050b8e657a88\"," + "\"_links\":{" + "\"scaStatus\":{" + "\"href\":\"https: "}," + "\"startAuthorisationWithEncryptedPsuAuthentication\":{" + "\"href\":\"https: "}," + "\"self\":{" + "\"href\":\"https: "}," + "\"aspspCertificates\":{" + "\"href\":\"https: "}," + "\"status\":{" + "\"href\":\"https: "}" + " }" + "}"; String expectedJson = "{" + "\"consentStatus\":\"******\"," + "\"consentId\":\"******\"," + "\"_links\":{" + "\"scaStatus\":{" + "\"href\":\"******\"" + "}," + "\"startAuthorisationWithEncryptedPsuAuthentication\":{" + "\"href\":\"******\"" + "}," + "\"self\":{" + "\"href\":\"******\"" + "}," + "\"aspspCertificates\":{" + "\"href\":\"******\"" + "}," + "\"status\":{" + "\"href\":\"******\"" + "}" + "}" + "}"; ConsentsResponse201 consentCreationResponse = new ObjectMapper().readValue(json, ConsentsResponse201.class); String body = anonymizer.sanitizeResponseBody(consentCreationResponse, ContentType.APPLICATION_JSON); assertThat(body).isEqualTo(expectedJson); }
@Test void sanitizeResponseBodyNotJsonContentType() { String body = anonymizer.sanitizeResponseBody(new Object(), "application/xml"); assertThat(body).isEqualTo(REPLACEMENT); }
@Test void sanitizeResponseBodyWithSerializationError() { String body = anonymizer.sanitizeResponseBody(Arrays.asList("abc", "123"), "application/json"); assertThat(body).isEqualTo("[\"" + REPLACEMENT + "\",\"" + REPLACEMENT + "\"]"); }
@Test void sanitizeResponseBodyAsString() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); Map<String, String> map = new HashMap<>(); map.put("key", "value"); String body = anonymizer.sanitizeResponseBody(mapper.writeValueAsString(map), "application/json"); assertThat(body).isEqualTo("{\"key\":\"" + REPLACEMENT + "\"}"); }