Search in sources :

Example 1 with CommunicationException

use of org.springframework.ldap.CommunicationException in project spring-boot by spring-projects.

the class LdapHealthIndicatorTests method ldapIsDown.

@Test
@SuppressWarnings("unchecked")
public void ldapIsDown() {
    LdapTemplate ldapTemplate = mock(LdapTemplate.class);
    given(ldapTemplate.executeReadOnly((ContextExecutor<String>) any())).willThrow(new CommunicationException(new javax.naming.CommunicationException("Connection failed")));
    LdapHealthIndicator healthIndicator = new LdapHealthIndicator(ldapTemplate);
    Health health = healthIndicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.DOWN);
    assertThat((String) health.getDetails().get("error")).contains("Connection failed");
    verify(ldapTemplate).executeReadOnly((ContextExecutor<String>) any());
}
Also used : CommunicationException(org.springframework.ldap.CommunicationException) LdapTemplate(org.springframework.ldap.core.LdapTemplate) Test(org.junit.Test)

Example 2 with CommunicationException

use of org.springframework.ldap.CommunicationException in project spring-security by spring-projects.

the class LdapAuthenticationProviderTests method authenticateWithNamingException.

@Test
public void authenticateWithNamingException() {
    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben", "benspassword");
    LdapAuthenticator mockAuthenticator = mock(LdapAuthenticator.class);
    CommunicationException expectedCause = new CommunicationException(new javax.naming.CommunicationException());
    when(mockAuthenticator.authenticate(authRequest)).thenThrow(expectedCause);
    LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(mockAuthenticator);
    try {
        ldapProvider.authenticate(authRequest);
        fail("Expected Exception");
    } catch (InternalAuthenticationServiceException success) {
        assertThat(success.getCause()).isSameAs(expectedCause);
    }
}
Also used : CommunicationException(org.springframework.ldap.CommunicationException) InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 CommunicationException (org.springframework.ldap.CommunicationException)2 LdapTemplate (org.springframework.ldap.core.LdapTemplate)1 InternalAuthenticationServiceException (org.springframework.security.authentication.InternalAuthenticationServiceException)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1