### Question:
LdapRealm extends DefaultLdapRealm implements RealmLifecycle, ShiroAuthorizationInfoProvider { @Override protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals ) { try { AuthorizationInfo info = super.doGetAuthorizationInfo( principals ); securityLog.debug( withRealm( "Queried for authorization info for user '%s'", principals.getPrimaryPrincipal() ) ); return info; } catch ( AuthorizationException e ) { securityLog.warn( withRealm( "Failed to get authorization info: '%s' caused by '%s'", e.getMessage(), e.getCause().getMessage() ) ); return null; } } LdapRealm( Config config, SecurityLog securityLog, SecureHasher secureHasher ); @Override boolean supports( AuthenticationToken token ); @Override void initialize(); @Override void start(); @Override void stop(); @Override void shutdown(); @Override AuthorizationInfo getAuthorizationInfoSnapshot( PrincipalCollection principalCollection ); static final String LDAP_REALM; static final String LDAP_CONNECTION_TIMEOUT_CLIENT_MESSAGE; static final String LDAP_READ_TIMEOUT_CLIENT_MESSAGE; static final String LDAP_AUTHORIZATION_FAILURE_CLIENT_MESSAGE; static final String LDAP_CONNECTION_REFUSED_CLIENT_MESSAGE; }### Answer:
@Test public void shouldLogSuccessfulAuthorizationQueries() { when( config.get( SecuritySettings.ldap_use_starttls ) ).thenReturn( true ); LdapRealm realm = new TestLdapRealm( config, securityLog, false ); JndiLdapContextFactory jndiLdapContectFactory = mock( JndiLdapContextFactory.class ); when( jndiLdapContectFactory.getUrl() ).thenReturn( "ldap: realm.doGetAuthorizationInfo( new SimplePrincipalCollection( "olivia", "LdapRealm" ) ); verify( securityLog ).debug( contains( "{LdapRealm}: Queried for authorization info for user 'olivia'" ) ); }
@Test public void shouldLogFailedAuthorizationQueries() { when( config.get( SecuritySettings.ldap_use_starttls ) ).thenReturn( true ); LdapRealm realm = new TestLdapRealm( config, securityLog, true ); JndiLdapContextFactory jndiLdapContectFactory = mock( JndiLdapContextFactory.class ); when( jndiLdapContectFactory.getUrl() ).thenReturn( "ldap: AuthorizationInfo info = realm.doGetAuthorizationInfo( new SimplePrincipalCollection( "olivia", "LdapRealm" ) ); assertNull( info ); verify( securityLog ).warn( contains( "{LdapRealm}: Failed to get authorization info: " + "'LDAP naming error while attempting to retrieve authorization for user [olivia].'" + " caused by 'Simulated failure'" ) ); }