mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-17 12:09:16 +00:00
Merge pull request #51441 from 2heunxun
Closes gh-51441 * pr/51441: Polish LdapHealthIndicator Fix LdapHealthIndicator reporting when LDAP version is unavailable
This commit is contained in:
+4
-18
@@ -16,9 +16,6 @@
|
||||
|
||||
package org.springframework.boot.ldap.health;
|
||||
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.DirContext;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.boot.health.contributor.AbstractHealthIndicator;
|
||||
@@ -37,7 +34,8 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class LdapHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
private static final ContextExecutor<@Nullable String> versionContextExecutor = new VersionContextExecutor();
|
||||
private static final ContextExecutor<@Nullable String> versionContextExecutor = (
|
||||
dirContext) -> (String) dirContext.getEnvironment().get("java.naming.ldap.version");
|
||||
|
||||
private final LdapOperations ldapOperations;
|
||||
|
||||
@@ -49,23 +47,11 @@ public class LdapHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
builder.up();
|
||||
String version = this.ldapOperations.executeReadOnly(versionContextExecutor);
|
||||
if (version != null) {
|
||||
builder.up().withDetail("version", version);
|
||||
builder.withDetail("version", version);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class VersionContextExecutor implements ContextExecutor<@Nullable String> {
|
||||
|
||||
@Override
|
||||
public @Nullable String executeWithContext(DirContext ctx) throws NamingException {
|
||||
Object version = ctx.getEnvironment().get("java.naming.ldap.version");
|
||||
if (version != null) {
|
||||
return (String) version;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+12
@@ -49,6 +49,18 @@ class LdapHealthIndicatorTests {
|
||||
then(ldapTemplate).should().executeReadOnly((ContextExecutor<String>) any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void ldapIsUpWhenVersionIsUnavailable() {
|
||||
LdapTemplate ldapTemplate = mock(LdapTemplate.class);
|
||||
given(ldapTemplate.executeReadOnly((ContextExecutor<String>) any())).willReturn(null);
|
||||
LdapHealthIndicator healthIndicator = new LdapHealthIndicator(ldapTemplate);
|
||||
Health health = healthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertThat(health.getDetails()).doesNotContainKey("version");
|
||||
then(ldapTemplate).should().executeReadOnly((ContextExecutor<String>) any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void ldapIsDown() {
|
||||
|
||||
Reference in New Issue
Block a user