### Question:
CacheHealthImpl implements CacheHealth { @Override public HealthStatus getStatus() { if (!isComponentHealthy() || cache.getAdvancedCache().getAvailability() == AvailabilityMode.DEGRADED_MODE) { return HealthStatus.DEGRADED; } DistributionManager distributionManager = SecurityActions.getDistributionManager(cache); if (distributionManager != null && distributionManager.isRehashInProgress()) { return HealthStatus.HEALTHY_REBALANCING; } return HealthStatus.HEALTHY; } CacheHealthImpl(Cache<?, ?> cache); @Override String getCacheName(); @Override HealthStatus getStatus();  }### Answer:
@Test public void testHealthyStatus() { CacheImpl<Object, Object> cache = spy(new CacheImpl<>("test")); DistributionManager distributionManagerMock = mock(DistributionManager.class); doReturn(false).when(distributionManagerMock).isRehashInProgress(); doReturn(distributionManagerMock).when(cache).getDistributionManager(); doReturn(ComponentStatus.RUNNING).when(cache).getStatus(); doReturn(AvailabilityMode.AVAILABLE).when(cache).getAvailability(); CacheHealth cacheHealth = new CacheHealthImpl(cache); HealthStatus status = cacheHealth.getStatus(); assertEquals(status, HealthStatus.HEALTHY); }
@Test public void testUnhealthyStatusWithFailedComponent() { CacheImpl<Object, Object> cache = spy(new CacheImpl<>("test")); doReturn(ComponentStatus.FAILED).when(cache).getStatus(); CacheHealth cacheHealth = new CacheHealthImpl(cache); HealthStatus status = cacheHealth.getStatus(); assertEquals(status, HealthStatus.DEGRADED); }
@Test public void testUnhealthyStatusWithTerminatedComponent() { CacheImpl<Object, Object> cache = spy(new CacheImpl<>("test")); doReturn(ComponentStatus.TERMINATED).when(cache).getStatus(); CacheHealth cacheHealth = new CacheHealthImpl(cache); HealthStatus status = cacheHealth.getStatus(); assertEquals(status, HealthStatus.DEGRADED); }
@Test public void testUnhealthyStatusWithStoppingComponent() { CacheImpl<Object, Object> cache = spy(new CacheImpl<>("test")); doReturn(ComponentStatus.STOPPING).when(cache).getStatus(); CacheHealth cacheHealth = new CacheHealthImpl(cache); HealthStatus status = cacheHealth.getStatus(); assertEquals(status, HealthStatus.DEGRADED); }
@Test public void testUnhealthyStatusWithDegradedPartition() { CacheImpl<Object, Object> cache = spy(new CacheImpl<>("test")); doReturn(ComponentStatus.RUNNING).when(cache).getStatus(); doReturn(AvailabilityMode.DEGRADED_MODE).when(cache).getAvailability(); CacheHealth cacheHealth = new CacheHealthImpl(cache); HealthStatus status = cacheHealth.getStatus(); assertEquals(status, HealthStatus.DEGRADED); }
@Test public void testRebalancingStatusOnRebalance() { CacheImpl<Object, Object> cache = spy(new CacheImpl<>("test")); DistributionManager distributionManagerMock = mock(DistributionManager.class); doReturn(true).when(distributionManagerMock).isRehashInProgress(); doReturn(distributionManagerMock).when(cache).getDistributionManager(); doReturn(ComponentStatus.RUNNING).when(cache).getStatus(); doReturn(AvailabilityMode.AVAILABLE).when(cache).getAvailability(); CacheHealth cacheHealth = new CacheHealthImpl(cache); HealthStatus status = cacheHealth.getStatus(); assertEquals(status, HealthStatus.HEALTHY_REBALANCING); }