ApplicationHealthIndicator implements HealthIndicator { @Override public Health health() { return Health.up() .withDetail(RESPONSE_PARAM_WEBAPP_NAME, formatValue(name)) .withDetail(RESPONSE_PARAM_VERSION, formatValue(version)) .withDetail(RESPONSE_PARAM_BUILD_TIME, formatValue(getFormattedTime(built))) .withDetail(RESPONSE_PARAM_START_TIME, formatValue(getFormattedTime(instanceStarted))) .withDetail(RESPONSE_PARAM_CURRENT_TIME, formatValue(getFormattedTime(ZonedDateTime.now()))) .build(); } ApplicationHealthIndicator(final ServletContext servletContext); @Override Health health(); static final String MANIFEST_PARAM_NAME; static final String MANIFEST_PARAM_VERSION; static final String MANIFEST_PARAM_BUILD_TIME; static final String DEFAULT_DATE_TIME_FORMAT; static final String NOT_AVAILABLE; static final String RESPONSE_PARAM_WEBAPP_NAME; static final String RESPONSE_PARAM_VERSION; static final String RESPONSE_PARAM_BUILD_TIME; static final String RESPONSE_PARAM_START_TIME; static final String RESPONSE_PARAM_CURRENT_TIME; }
@Test public void whenParametersMissingInManifestFile() { applicationHealthIndicator = new ApplicationHealthIndicator(mock(ServletContext.class)); Health health = applicationHealthIndicator.health(); assertEquals(health.getStatus(), Status.UP); assertEquals( Stream.of( ApplicationHealthIndicator.RESPONSE_PARAM_WEBAPP_NAME, ApplicationHealthIndicator.RESPONSE_PARAM_VERSION, ApplicationHealthIndicator.RESPONSE_PARAM_BUILD_TIME, ApplicationHealthIndicator.RESPONSE_PARAM_START_TIME, ApplicationHealthIndicator.RESPONSE_PARAM_CURRENT_TIME ).collect(Collectors.toCollection(LinkedHashSet::new)), health.getDetails().keySet()); assertEquals(NOT_AVAILABLE, health.getDetails().get(ApplicationHealthIndicator.RESPONSE_PARAM_WEBAPP_NAME)); assertEquals(NOT_AVAILABLE, health.getDetails().get(ApplicationHealthIndicator.RESPONSE_PARAM_VERSION)); assertEquals(NOT_AVAILABLE, health.getDetails().get(ApplicationHealthIndicator.RESPONSE_PARAM_BUILD_TIME)); verifyCorrectStartAndServerTime(health); }
@Test public void whenBuildTimeHasInvalidFormatInManifestFile() { when(Manifests.read(ApplicationHealthIndicator.MANIFEST_PARAM_NAME)).thenReturn(TEST_WEBAPP); when(Manifests.read(ApplicationHealthIndicator.MANIFEST_PARAM_BUILD_TIME)).thenReturn("ABCDEFG1234"); when(Manifests.read(ApplicationHealthIndicator.MANIFEST_PARAM_VERSION)).thenReturn(TEST_VERSION); applicationHealthIndicator = new ApplicationHealthIndicator(mock(ServletContext.class)); Health health = applicationHealthIndicator.health(); assertEquals(health.getStatus(), Status.UP); assertEquals(TEST_WEBAPP, health.getDetails().get(ApplicationHealthIndicator.RESPONSE_PARAM_WEBAPP_NAME)); assertEquals(TEST_VERSION, health.getDetails().get(ApplicationHealthIndicator.RESPONSE_PARAM_VERSION)); assertEquals(NOT_AVAILABLE, health.getDetails().get(ApplicationHealthIndicator.RESPONSE_PARAM_BUILD_TIME)); }