|
| 1 | +package fi.hsl.common.health; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.concurrent.atomic.AtomicInteger; |
| 9 | +import java.util.function.BooleanSupplier; |
| 10 | +import org.junit.After; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +public class HealthServerTest { |
| 15 | + |
| 16 | + private HealthServer healthServer; |
| 17 | + private final int testPort = 0; |
| 18 | + private final String testEndpoint = "/healthz"; |
| 19 | + |
| 20 | + private static class CountingWrapper implements BooleanSupplier { |
| 21 | + |
| 22 | + private final BooleanSupplier delegate; |
| 23 | + private final AtomicInteger callCount = new AtomicInteger(0); |
| 24 | + |
| 25 | + public CountingWrapper(BooleanSupplier delegate) { |
| 26 | + this.delegate = delegate; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public boolean getAsBoolean() { |
| 31 | + callCount.incrementAndGet(); |
| 32 | + return delegate.getAsBoolean(); |
| 33 | + } |
| 34 | + |
| 35 | + public int getCallCount() { |
| 36 | + return callCount.get(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @Before |
| 41 | + public void setUp() throws IOException { |
| 42 | + healthServer = new HealthServer(testPort, testEndpoint); |
| 43 | + } |
| 44 | + |
| 45 | + @After |
| 46 | + public void tearDown() { |
| 47 | + if (healthServer != null) { |
| 48 | + healthServer.close(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void singleUnhealthyCheckReturnsFalse() { |
| 54 | + CountingWrapper unhealthyCheck = new CountingWrapper(() -> false); |
| 55 | + healthServer.addCheck(unhealthyCheck); |
| 56 | + boolean healthStatus = healthServer.checkHealth(); |
| 57 | + assertFalse( |
| 58 | + "Health status should be false when one check is unhealthy.", |
| 59 | + healthStatus |
| 60 | + ); |
| 61 | + assertEquals( |
| 62 | + "UnhealthyCheck should have been called once.", |
| 63 | + 1, |
| 64 | + unhealthyCheck.getCallCount() |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void allHealthyChecksReturnsTrue() { |
| 70 | + CountingWrapper healthyCheck1 = new CountingWrapper(() -> true); |
| 71 | + CountingWrapper healthyCheck2 = new CountingWrapper(() -> true); |
| 72 | + healthServer.addCheck(healthyCheck1); |
| 73 | + healthServer.addCheck(healthyCheck2); |
| 74 | + boolean healthStatus = healthServer.checkHealth(); |
| 75 | + assertTrue( |
| 76 | + "Health status should be true when all checks are healthy.", |
| 77 | + healthStatus |
| 78 | + ); |
| 79 | + assertEquals( |
| 80 | + "HealthyCheck1 should have been called once.", |
| 81 | + 1, |
| 82 | + healthyCheck1.getCallCount() |
| 83 | + ); |
| 84 | + assertEquals( |
| 85 | + "HealthyCheck2 should have been called once.", |
| 86 | + 1, |
| 87 | + healthyCheck2.getCallCount() |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testCheckHealth_CheckThrowsException_ReturnsFalse() { |
| 93 | + final RuntimeException testException = new RuntimeException( |
| 94 | + "Simulated check failure" |
| 95 | + ); |
| 96 | + CountingWrapper exceptionThrowingCheck = new CountingWrapper(() -> { |
| 97 | + throw testException; |
| 98 | + }); |
| 99 | + healthServer.addCheck(exceptionThrowingCheck); |
| 100 | + boolean healthStatus = healthServer.checkHealth(); |
| 101 | + assertFalse( |
| 102 | + "Health status should be false when a check throws an exception.", |
| 103 | + healthStatus |
| 104 | + ); |
| 105 | + assertEquals( |
| 106 | + "ExceptionThrowingCheck should have been called once.", |
| 107 | + 1, |
| 108 | + exceptionThrowingCheck.getCallCount() |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
0 commit comments