Search in sources :

Example 1 with HealthStateView

use of io.dropwizard.health.HealthStateView in project dropwizard by dropwizard.

the class JsonHealthResponseProviderTest method shouldThrowExceptionWhenJsonProcessorExceptionOccurs.

@Test
void shouldThrowExceptionWhenJsonProcessorExceptionOccurs() throws IOException {
    // given
    final ObjectMapper mapperMock = mock(ObjectMapper.class);
    this.jsonHealthResponseProvider = new JsonHealthResponseProvider(healthStatusChecker, healthStateAggregator, mapperMock);
    final HealthStateView view = new HealthStateView("foo", true, HealthCheckType.READY, true);
    final Map<String, Collection<String>> queryParams = Collections.singletonMap(JsonHealthResponseProvider.NAME_QUERY_PARAM, Collections.singleton(view.getName()));
    final JsonMappingException exception = JsonMappingException.fromUnexpectedIOE(new IOException("uh oh"));
    // when
    when(healthStateAggregator.healthStateView(view.getName())).thenReturn(Optional.of(view));
    when(mapperMock.writeValueAsString(any())).thenThrow(exception);
    // then
    assertThatThrownBy(() -> jsonHealthResponseProvider.healthResponse(queryParams)).isInstanceOf(RuntimeException.class).hasCauseReference(exception);
    verifyNoInteractions(healthStatusChecker);
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) HealthStateView(io.dropwizard.health.HealthStateView) Collection(java.util.Collection) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 2 with HealthStateView

use of io.dropwizard.health.HealthStateView in project dropwizard by dropwizard.

the class JsonHealthResponseProviderTest method shouldHandleMultipleHealthStateViewsCorrectly.

@Test
void shouldHandleMultipleHealthStateViewsCorrectly() throws IOException {
    // given
    final HealthStateView fooView = new HealthStateView("foo", true, HealthCheckType.READY, true);
    final HealthStateView barView = new HealthStateView("bar", true, HealthCheckType.ALIVE, true);
    final HealthStateView bazView = new HealthStateView("baz", false, HealthCheckType.READY, false);
    final Collection<String> names = new ArrayList<>();
    names.add(fooView.getName());
    names.add(barView.getName());
    names.add(bazView.getName());
    final Map<String, Collection<String>> queryParams = Collections.singletonMap(JsonHealthResponseProvider.NAME_QUERY_PARAM, names);
    // when
    when(healthStateAggregator.healthStateView(fooView.getName())).thenReturn(Optional.of(fooView));
    when(healthStateAggregator.healthStateView(barView.getName())).thenReturn(Optional.of(barView));
    when(healthStateAggregator.healthStateView(bazView.getName())).thenReturn(Optional.of(bazView));
    when(healthStatusChecker.isHealthy(isNull())).thenReturn(true);
    final HealthResponse response = jsonHealthResponseProvider.healthResponse(queryParams);
    // then
    assertThat(response.isHealthy()).isTrue();
    assertThat(response.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
    assertThat(response.getMessage()).isEqualToIgnoringWhitespace(fixture("/json/multiple-healthy-responses.json"));
}
Also used : ArrayList(java.util.ArrayList) HealthStateView(io.dropwizard.health.HealthStateView) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Example 3 with HealthStateView

use of io.dropwizard.health.HealthStateView in project dropwizard by dropwizard.

the class JsonHealthResponseProviderTest method shouldHandleSingleHealthStateViewCorrectly.

@Test
void shouldHandleSingleHealthStateViewCorrectly() throws IOException {
    // given
    final HealthStateView view = new HealthStateView("foo", true, HealthCheckType.READY, true);
    final Map<String, Collection<String>> queryParams = Collections.singletonMap(JsonHealthResponseProvider.NAME_QUERY_PARAM, Collections.singleton(view.getName()));
    // when
    when(healthStateAggregator.healthStateView(view.getName())).thenReturn(Optional.of(view));
    when(healthStatusChecker.isHealthy(isNull())).thenReturn(true);
    final HealthResponse response = jsonHealthResponseProvider.healthResponse(queryParams);
    // then
    assertThat(response.isHealthy()).isTrue();
    assertThat(response.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
    assertThat(response.getMessage()).isEqualToIgnoringWhitespace(fixture("/json/single-healthy-response.json"));
}
Also used : HealthStateView(io.dropwizard.health.HealthStateView) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Aggregations

HealthStateView (io.dropwizard.health.HealthStateView)3 Collection (java.util.Collection)3 Test (org.junit.jupiter.api.Test)3 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1