### Question:
AuditAuthenticator implements IAuthenticator { @Override public SaslNegotiator newSaslNegotiator() { return new AuditSaslNegotiator(wrappedAuthenticator.newDecoratedSaslNegotiator()); } AuditAuthenticator();   AuditAuthenticator(IDecoratedAuthenticator wrappedAuthenticator, AuditAdapter auditAdapter); @Override boolean requireAuthentication(); @Override Set<? extends IResource> protectedResources(); @Override void validateConfiguration(); @Override void setup(); @Override SaslNegotiator newSaslNegotiator(); @Override AuthenticatedUser legacyAuthenticate(Map<String, String> credentials);  }### Answer:
@Test public void testWhenUserIsProvidedAuthenticationAttemptIsLogged() { AuthenticatedUser expected = mock(AuthenticatedUser.class); when(mockSaslNegotiator.getAuthenticatedUser()).thenReturn(expected); when(mockSaslNegotiator.getUser()).thenReturn("audited_user"); SaslNegotiator negotiator = auditAuthenticator.newSaslNegotiator(); AuthenticatedUser actual = negotiator.getAuthenticatedUser(); assertThat(actual).isSameAs(expected); verify(mockAuditAdapter, times(1)).auditAuth(eq("audited_user"), eq(Status.ATTEMPT), anyLong()); verify(mockAuditAdapter, times(1)).auditAuth(eq("audited_user"), eq(Status.SUCCEEDED), anyLong()); }
@Test public void testWhenAuthenticationFailsIsStillAudited() { when(mockSaslNegotiator.getUser()).thenReturn("audited_user"); when(mockSaslNegotiator.getAuthenticatedUser()).thenThrow(new AuthenticationException("audited_user")); SaslNegotiator negotiator = auditAuthenticator.newSaslNegotiator(); assertThatExceptionOfType(AuthenticationException.class).isThrownBy(negotiator::getAuthenticatedUser); verify(mockAuditAdapter, times(1)).auditAuth(eq("audited_user"), eq(Status.ATTEMPT), anyLong()); verify(mockAuditAdapter, times(1)).auditAuth(eq("audited_user"), eq(Status.FAILED), anyLong()); }
@Test public void testAuthWithSubject() { AuthenticatedUser expected = mock(AuthenticatedUser.class); when(mockSaslNegotiator.getAuthenticatedUser()).thenReturn(expected); when(mockSaslNegotiator.getUser()).thenReturn("audited_user"); when(mockSaslNegotiator.getSubject()).thenReturn(Optional.of("subject_user")); SaslNegotiator negotiator = auditAuthenticator.newSaslNegotiator(); AuthenticatedUser actual = negotiator.getAuthenticatedUser(); assertThat(actual).isSameAs(expected); verify(mockAuditAdapter, times(1)).auditAuth(eq("audited_user"), eq("subject_user"), eq(Status.ATTEMPT), anyLong()); verify(mockAuditAdapter, times(1)).auditAuth(eq("audited_user"), eq("subject_user"), eq(Status.SUCCEEDED), anyLong()); }