From 98e68616769c60067c2ba73dc1592182f263c009 Mon Sep 17 00:00:00 2001 From: Daniel Patynski Date: Thu, 13 Feb 2020 16:40:38 +0000 Subject: [PATCH] SIDM-3692 Migrate HealthProve interface to abstract class to reduce code repetition. --- .../idam/health/am/AmIsAliveHealthProbe.java | 20 +++------------- .../health/am/AmPasswordGrantHealthProbe.java | 17 +++---------- .../health/backup/FileFreshnessProbe.java | 2 +- .../command/ReplicationCommandProbe.java | 2 +- .../idam/health/idm/IdmPingHealthProbe.java | 2 +- .../ldap/LdapReplicationHealthProbe.java | 2 +- .../reform/idam/health/probe/HealthProbe.java | 24 ++++++++++++++----- .../TokenStoreSearchHealthProbe.java | 2 +- .../UserStoreAuthenticationHealthProbe.java | 2 +- 9 files changed, 30 insertions(+), 43 deletions(-) diff --git a/src/main/java/uk/gov/hmcts/reform/idam/health/am/AmIsAliveHealthProbe.java b/src/main/java/uk/gov/hmcts/reform/idam/health/am/AmIsAliveHealthProbe.java index 6ed791a..4262231 100644 --- a/src/main/java/uk/gov/hmcts/reform/idam/health/am/AmIsAliveHealthProbe.java +++ b/src/main/java/uk/gov/hmcts/reform/idam/health/am/AmIsAliveHealthProbe.java @@ -6,12 +6,10 @@ import org.springframework.stereotype.Component; import uk.gov.hmcts.reform.idam.health.probe.HealthProbe; -import javax.annotation.Nullable; - @Component @Profile("am") @Slf4j -public class AmIsAliveHealthProbe implements HealthProbe { +public class AmIsAliveHealthProbe extends HealthProbe { private static final String TAG = "AM IsAlive: "; @@ -19,8 +17,6 @@ public class AmIsAliveHealthProbe implements HealthProbe { private final AmProvider amProvider; - private String details = null; - public AmIsAliveHealthProbe(AmProvider amProvider) { this.amProvider = amProvider; } @@ -33,21 +29,11 @@ public boolean probe() { log.info(TAG + "success"); return true; } else { - String msg = TAG + "response did not contain expected value"; - log.error(msg); - details = msg; + setDetails(TAG + "response did not contain expected value"); } } catch (Exception e) { - String msg = TAG + e.getMessage() + " [" + e.getClass().getSimpleName() + "]"; - log.error(msg); - details = msg; + setDetails(TAG + e.getMessage()); } return false; } - - @Nullable - @Override - public String getDetails() { - return details; - } } diff --git a/src/main/java/uk/gov/hmcts/reform/idam/health/am/AmPasswordGrantHealthProbe.java b/src/main/java/uk/gov/hmcts/reform/idam/health/am/AmPasswordGrantHealthProbe.java index bebf097..1715dc7 100644 --- a/src/main/java/uk/gov/hmcts/reform/idam/health/am/AmPasswordGrantHealthProbe.java +++ b/src/main/java/uk/gov/hmcts/reform/idam/health/am/AmPasswordGrantHealthProbe.java @@ -14,7 +14,7 @@ @Component @Profile("am") @Slf4j -public class AmPasswordGrantHealthProbe implements HealthProbe { +public class AmPasswordGrantHealthProbe extends HealthProbe { private static final String TAG = "AM PasswordGrant: "; @@ -26,8 +26,6 @@ public class AmPasswordGrantHealthProbe implements HealthProbe { private final ProbeUserProperties probeUserProperties; private final String authorization; - private String details = null; - public AmPasswordGrantHealthProbe( AmProvider amProvider, AmHealthProbeProperties healthProbeProperties, @@ -40,11 +38,6 @@ public AmPasswordGrantHealthProbe( this.authorization = "Basic " + encode(agentProperties.getName(), agentProperties.getSecret()); } - @Override - public String getDetails() { - return details; - } - @Override public boolean probe() { try { @@ -59,14 +52,10 @@ public boolean probe() { log.info(TAG + "success"); return true; } else { - String msg = TAG + "response did not contain expected value"; - log.error(msg); - details = msg; + setDetails(TAG + "response did not contain expected value"); } } catch (Exception e) { - String msg = TAG + e.getMessage() + " [" + e.getClass().getSimpleName() + "]"; - log.error(msg); - details = msg; + setDetails(TAG + e.getMessage()); } return false; } diff --git a/src/main/java/uk/gov/hmcts/reform/idam/health/backup/FileFreshnessProbe.java b/src/main/java/uk/gov/hmcts/reform/idam/health/backup/FileFreshnessProbe.java index 464dfef..2b84787 100644 --- a/src/main/java/uk/gov/hmcts/reform/idam/health/backup/FileFreshnessProbe.java +++ b/src/main/java/uk/gov/hmcts/reform/idam/health/backup/FileFreshnessProbe.java @@ -14,7 +14,7 @@ import java.time.temporal.ChronoUnit; @Slf4j -public class FileFreshnessProbe implements HealthProbe { +public class FileFreshnessProbe extends HealthProbe { private final String probeName; private final Path checkPath; diff --git a/src/main/java/uk/gov/hmcts/reform/idam/health/command/ReplicationCommandProbe.java b/src/main/java/uk/gov/hmcts/reform/idam/health/command/ReplicationCommandProbe.java index 5acbe04..0be771b 100644 --- a/src/main/java/uk/gov/hmcts/reform/idam/health/command/ReplicationCommandProbe.java +++ b/src/main/java/uk/gov/hmcts/reform/idam/health/command/ReplicationCommandProbe.java @@ -15,7 +15,7 @@ @Component @Profile({"userstore","tokenstore","replication"}) @Slf4j -public class ReplicationCommandProbe implements HealthProbe { +public class ReplicationCommandProbe extends HealthProbe { private static final String SPACE = " "; private static final String RESULT_DELIM = "\t"; diff --git a/src/main/java/uk/gov/hmcts/reform/idam/health/idm/IdmPingHealthProbe.java b/src/main/java/uk/gov/hmcts/reform/idam/health/idm/IdmPingHealthProbe.java index 2679234..47b6fe5 100644 --- a/src/main/java/uk/gov/hmcts/reform/idam/health/idm/IdmPingHealthProbe.java +++ b/src/main/java/uk/gov/hmcts/reform/idam/health/idm/IdmPingHealthProbe.java @@ -12,7 +12,7 @@ @Component @Profile("idm") @Slf4j -public class IdmPingHealthProbe implements HealthProbe { +public class IdmPingHealthProbe extends HealthProbe { private static final String TAG = "IDM Ping: "; diff --git a/src/main/java/uk/gov/hmcts/reform/idam/health/ldap/LdapReplicationHealthProbe.java b/src/main/java/uk/gov/hmcts/reform/idam/health/ldap/LdapReplicationHealthProbe.java index 2cd3cbf..fe85165 100644 --- a/src/main/java/uk/gov/hmcts/reform/idam/health/ldap/LdapReplicationHealthProbe.java +++ b/src/main/java/uk/gov/hmcts/reform/idam/health/ldap/LdapReplicationHealthProbe.java @@ -28,7 +28,7 @@ @Component @Slf4j @Profile({"tokenstore", "userstore", "ldap"}) -public class LdapReplicationHealthProbe implements HealthProbe { +public class LdapReplicationHealthProbe extends HealthProbe { private static final String TAG = "LDAP Replication: "; diff --git a/src/main/java/uk/gov/hmcts/reform/idam/health/probe/HealthProbe.java b/src/main/java/uk/gov/hmcts/reform/idam/health/probe/HealthProbe.java index 2b504f7..e6fa2ce 100644 --- a/src/main/java/uk/gov/hmcts/reform/idam/health/probe/HealthProbe.java +++ b/src/main/java/uk/gov/hmcts/reform/idam/health/probe/HealthProbe.java @@ -1,17 +1,29 @@ package uk.gov.hmcts.reform.idam.health.probe; +import lombok.extern.slf4j.Slf4j; + +import javax.annotation.Nonnull; import javax.annotation.Nullable; -public interface HealthProbe { +@Slf4j +public abstract class HealthProbe { + + private String details; - boolean probe(); + abstract public boolean probe(); - default String getName() { + @Nonnull + public String getName() { return this.getClass().getSimpleName(); - }; + } @Nullable - default String getDetails() { - return null; + public String getDetails() { + return details; + } + + public void setDetails(@Nullable String details) { + log.info(details + " [" + getName() + "]"); + this.details = details; } } diff --git a/src/main/java/uk/gov/hmcts/reform/idam/health/tokenstore/TokenStoreSearchHealthProbe.java b/src/main/java/uk/gov/hmcts/reform/idam/health/tokenstore/TokenStoreSearchHealthProbe.java index 2ca6aea..48770a3 100644 --- a/src/main/java/uk/gov/hmcts/reform/idam/health/tokenstore/TokenStoreSearchHealthProbe.java +++ b/src/main/java/uk/gov/hmcts/reform/idam/health/tokenstore/TokenStoreSearchHealthProbe.java @@ -13,7 +13,7 @@ @Component @Profile("tokenstore") @Slf4j -public class TokenStoreSearchHealthProbe implements HealthProbe { +public class TokenStoreSearchHealthProbe extends HealthProbe { private final String TAG = "TokenStore Search: "; diff --git a/src/main/java/uk/gov/hmcts/reform/idam/health/userstore/UserStoreAuthenticationHealthProbe.java b/src/main/java/uk/gov/hmcts/reform/idam/health/userstore/UserStoreAuthenticationHealthProbe.java index 460e163..089b5d0 100644 --- a/src/main/java/uk/gov/hmcts/reform/idam/health/userstore/UserStoreAuthenticationHealthProbe.java +++ b/src/main/java/uk/gov/hmcts/reform/idam/health/userstore/UserStoreAuthenticationHealthProbe.java @@ -14,7 +14,7 @@ @Component @Profile("userstore") @Slf4j -public class UserStoreAuthenticationHealthProbe implements HealthProbe { +public class UserStoreAuthenticationHealthProbe extends HealthProbe { private static final String LDAP_CN_ATTRIBUTE = "cn"; private final String TAG = "UserStore Auth: ";