### Question:
SamlProxySamlTransformationErrorExceptionMapper extends AbstractContextExceptionMapper<SamlTransformationErrorException> { @Override protected Response handleException(SamlTransformationErrorException exception) { UUID errorId = UUID.randomUUID(); eventSinkMessageSender.audit(exception, errorId, getSessionId().orElse(SessionId.NO_SESSION_CONTEXT_IN_ERROR)); levelLogger.log(exception.getLogLevel(), exception, errorId); ErrorStatusDto auditedErrorStatus = ErrorStatusDto.createAuditedErrorStatus(errorId, getExceptionTypeForSamlException(exception)); return Response.status(Response.Status.BAD_REQUEST).entity(auditedErrorStatus).build(); } @Inject  SamlProxySamlTransformationErrorExceptionMapper(
            Provider<HttpServletRequest> contextProvider,
            EventSinkMessageSender eventSinkMessageSender,
            LevelLoggerFactory<SamlProxySamlTransformationErrorExceptionMapper> levelLoggerFactory);   }### Answer:
@Test public void shouldLogToEventSinkWhenExceptionHasContextAndSessionId() throws Exception { TestSamlTransformationErrorException exception = new TestSamlTransformationErrorException("error", new RuntimeException(), Level.DEBUG); SessionId sessionId = SessionId.createNewSessionId(); when(httpServletRequest.getParameter(Urls.SharedUrls.SESSION_ID_PARAM)).thenReturn(sessionId.getSessionId()); exceptionMapper.handleException(exception); verify(eventSinkMessageSender).audit(eq(exception), any(UUID.class), eq(sessionId)); }
@Test public void shouldLogToEventSinkWhenExceptionHasContextAndNoSessionId() throws Exception { TestSamlTransformationErrorException exception = new TestSamlTransformationErrorException("error", new RuntimeException(), Level.DEBUG); exceptionMapper.handleException(exception); verify(eventSinkMessageSender).audit(eq(exception), any(UUID.class), eq(SessionId.NO_SESSION_CONTEXT_IN_ERROR)); }
@Test public void shouldCreateAuditedErrorResponseForInvalidSaml() throws Exception { Response response = exceptionMapper.handleException(new TestSamlTransformationErrorException("error", new RuntimeException(), Level.DEBUG)); ErrorStatusDto responseEntity = (ErrorStatusDto) response.getEntity(); assertThat(response.getStatus()).isEqualTo(Response.Status.BAD_REQUEST.getStatusCode()); assertThat(responseEntity.isAudited()).isTrue(); assertThat(responseEntity.getExceptionType()).isEqualTo(ExceptionType.INVALID_SAML); }
@Test public void shouldCreateAuditedErrorResponseForRequestTooOldError() throws Exception { Response response = exceptionMapper.handleException(new SamlRequestTooOldException("error", new RuntimeException(), Level.DEBUG)); ErrorStatusDto responseEntity = (ErrorStatusDto) response.getEntity(); assertThat(response.getStatus()).isEqualTo(Response.Status.BAD_REQUEST.getStatusCode()); assertThat(responseEntity.isAudited()).isTrue(); assertThat(responseEntity.getExceptionType()).isEqualTo(ExceptionType.INVALID_SAML_REQUEST_TOO_OLD); }
@Test public void shouldLogExceptionAtCorrectLevel() throws Exception { Level logLevel = Level.DEBUG; TestSamlTransformationErrorException exception = new TestSamlTransformationErrorException("error", new RuntimeException(), logLevel); exceptionMapper.handleException(exception); verify(levelLogger).log(eq(logLevel), eq(exception), any(UUID.class)); }