### Question:
ProtectiveMonitoringLogger { public void logAuthnResponse(Response samlResponse, Direction direction, SignatureStatus signatureStatus) { Map<String, String> copyOfContextMap = Optional.ofNullable(MDC.getCopyOfContextMap()).orElse(Collections.emptyMap()); StatusCode statusCode = samlResponse.getStatus().getStatusCode(); MDC.setContextMap(Map.of( "responseId", samlResponse.getID(), "inResponseTo", samlResponse.getInResponseTo(), "status", statusCode.getValue(), "subStatus", Optional.ofNullable(statusCode.getStatusCode()).map(StatusCode::getValue).orElse("NoSubStatus"), "direction", direction.name(), "issuerId", Optional.ofNullable(samlResponse.getIssuer()).map(Issuer::getValue).orElse("NoIssuer"), "destination", Optional.ofNullable(samlResponse.getDestination()).orElse("NoDestination"), "signatureStatus", signatureStatus.name())); LOG.info(formatter.formatAuthnResponse(samlResponse, direction, signatureStatus)); MDC.clear(); MDC.setContextMap(copyOfContextMap); } @Inject  ProtectiveMonitoringLogger(ProtectiveMonitoringLogFormatter formatter); void logAuthnRequest(AuthnRequest request, Direction direction, SignatureStatus signatureStatus); void logAuthnResponse(Response samlResponse, Direction direction, SignatureStatus signatureStatus);  }### Answer:
@Test public void shouldLogWhenNoSignatureExists() { ProtectiveMonitoringLogger protectiveMonitoringLogger = new ProtectiveMonitoringLogger(new ProtectiveMonitoringLogFormatter()); when(samlResponse.getStatus()).thenReturn(status); when(samlResponse.getID()).thenReturn("ID"); when(samlResponse.getInResponseTo()).thenReturn("InResponseTo"); when(status.getStatusCode()).thenReturn(statusCode); when(statusCode.getValue()).thenReturn("all-good"); protectiveMonitoringLogger.logAuthnResponse(samlResponse, Direction.OUTBOUND, SignatureStatus.NO_SIGNATURE); assertThat(TestAppender.events.size()).isEqualTo(1); assertThat(TestAppender.events.get(0).getMDCPropertyMap()).contains( MapEntry.entry("signatureStatus", "NO_SIGNATURE") ); }
@Test public void shouldLogWhenSignatureIsPresentAndCorrect() { ProtectiveMonitoringLogger protectiveMonitoringLogger = new ProtectiveMonitoringLogger(new ProtectiveMonitoringLogFormatter()); when(samlResponse.getStatus()).thenReturn(status); when(samlResponse.getID()).thenReturn("ID"); when(samlResponse.getInResponseTo()).thenReturn("InResponseTo"); when(status.getStatusCode()).thenReturn(statusCode); when(statusCode.getValue()).thenReturn("all-good"); protectiveMonitoringLogger.logAuthnResponse(samlResponse, Direction.OUTBOUND, SignatureStatus.VALID_SIGNATURE); assertThat(TestAppender.events.size()).isEqualTo(1); assertThat(TestAppender.events.get(0).getMDCPropertyMap()).contains( MapEntry.entry("signatureStatus", "VALID_SIGNATURE") ); }
@Test public void shouldLogWhenSignatureIsPresentButInvalid() { ProtectiveMonitoringLogger protectiveMonitoringLogger = new ProtectiveMonitoringLogger(new ProtectiveMonitoringLogFormatter()); when(samlResponse.getStatus()).thenReturn(status); when(samlResponse.getID()).thenReturn("ID"); when(samlResponse.getInResponseTo()).thenReturn("InResponseTo"); when(status.getStatusCode()).thenReturn(statusCode); when(statusCode.getValue()).thenReturn("all-good"); protectiveMonitoringLogger.logAuthnResponse(samlResponse, Direction.OUTBOUND, SignatureStatus.INVALID_SIGNATURE); assertThat(TestAppender.events.size()).isEqualTo(1); assertThat(TestAppender.events.get(0).getMDCPropertyMap()).contains( MapEntry.entry("signatureStatus", "INVALID_SIGNATURE") ); }