CC
diff --git a/src/main/java/pta/de/health/ConfigFilePresentHealthCheck.java b/src/main/java/pta/de/health/ConfigFilePresentHealthCheck.java index dc3ade7..e8de912 100644 --- a/src/main/java/pta/de/health/ConfigFilePresentHealthCheck.java +++ b/src/main/java/pta/de/health/ConfigFilePresentHealthCheck.java
@@ -9,7 +9,7 @@ protected Result check() throws Exception { ServiceDistributionCluster[] sdc = ServicesConfigCache.getInstance().getCache(); // Throws Exception if it fails - if( sdc.length > 0 ) { + if (sdc != null && sdc.length > 0) { return Result.healthy(); } else {
diff --git a/src/test/java/pta/de/health/ConfigFilePresentHealthCheckTest.java b/src/test/java/pta/de/health/ConfigFilePresentHealthCheckTest.java new file mode 100644 index 0000000..d8798dc --- /dev/null +++ b/src/test/java/pta/de/health/ConfigFilePresentHealthCheckTest.java
@@ -0,0 +1,42 @@ +package pta.de.health; + +import com.codahale.metrics.health.HealthCheck; +import org.junit.Test; +import org.powermock.reflect.Whitebox; +import pta.de.api.ServiceDistributionCluster; +import pta.de.core.controller.ServicesConfigCache; + +import static junit.framework.TestCase.assertFalse; +import static org.junit.Assert.assertTrue; + +public class ConfigFilePresentHealthCheckTest { + private boolean isHealthy(ConfigFilePresentHealthCheck hc) throws Exception { + HealthCheck.Result r = Whitebox.invokeMethod(hc, "check"); + return r.isHealthy(); + } + + @Test + public void testConfigFilePresent_False() throws Exception { + ServicesConfigCache scc = ServicesConfigCache.getInstance(); + ServiceDistributionCluster[] oldCache = scc.getCache(); + + try { + ConfigFilePresentHealthCheck hc = new ConfigFilePresentHealthCheck(); + + // test with invalid cluster + Whitebox.setInternalState(scc, "cache", (Object) null); + assertFalse(isHealthy(hc)); + + // test with invalid cluster + Whitebox.setInternalState(scc, "cache", new ServiceDistributionCluster[0]); + assertFalse(isHealthy(hc)); + + ServiceDistributionCluster[] arr = new ServiceDistributionCluster[1]; + arr[0] = new ServiceDistributionCluster(); + Whitebox.setInternalState(scc, "cache", arr); + assertTrue(isHealthy(hc)); + } finally { + Whitebox.setInternalState(scc, "cache", oldCache); + } + } +}