Search in sources :

Example 11 with Health

use of org.springframework.boot.actuate.health.Health in project spring-boot by spring-projects.

the class CloudFoundryHealthMvcEndpointTests method cloudFoundryHealthEndpointShouldAlwaysReturnAllHealthDetails.

@Test
public void cloudFoundryHealthEndpointShouldAlwaysReturnAllHealthDetails() throws Exception {
    HealthEndpoint endpoint = mock(HealthEndpoint.class);
    given(endpoint.isEnabled()).willReturn(true);
    CloudFoundryHealthMvcEndpoint mvc = new CloudFoundryHealthMvcEndpoint(endpoint);
    given(endpoint.invoke()).willReturn(new Health.Builder().up().withDetail("foo", "bar").build());
    given(endpoint.isSensitive()).willReturn(false);
    Object result = mvc.invoke(null, null);
    assertThat(result instanceof Health).isTrue();
    assertThat(((Health) result).getStatus() == Status.UP).isTrue();
    assertThat(((Health) result).getDetails().get("foo")).isEqualTo("bar");
}
Also used : HealthEndpoint(org.springframework.boot.actuate.endpoint.HealthEndpoint) Health(org.springframework.boot.actuate.health.Health) Test(org.junit.Test)

Example 12 with Health

use of org.springframework.boot.actuate.health.Health in project spring-boot by spring-projects.

the class EndpointAutoConfigurationTests method healthEndpoint.

@Test
public void healthEndpoint() {
    load(EmbeddedDataSourceConfiguration.class, EndpointAutoConfiguration.class, HealthIndicatorAutoConfiguration.class);
    HealthEndpoint bean = this.context.getBean(HealthEndpoint.class);
    assertThat(bean).isNotNull();
    Health result = bean.invoke();
    assertThat(result).isNotNull();
    assertThat(result.getDetails().containsKey("db")).isTrue();
}
Also used : HealthEndpoint(org.springframework.boot.actuate.endpoint.HealthEndpoint) Health(org.springframework.boot.actuate.health.Health) Test(org.junit.Test)

Example 13 with Health

use of org.springframework.boot.actuate.health.Health in project spring-boot by spring-projects.

the class HealthMvcEndpointTests method rightAuthorityPresentShouldExposeDetails.

@Test
public void rightAuthorityPresentShouldExposeDetails() throws Exception {
    this.environment.getPropertySources().addLast(SECURITY_ROLES);
    Authentication principal = mock(Authentication.class);
    Set<SimpleGrantedAuthority> authorities = Collections.singleton(new SimpleGrantedAuthority("HERO"));
    doReturn(authorities).when(principal).getAuthorities();
    given(this.endpoint.invoke()).willReturn(new Health.Builder().up().withDetail("foo", "bar").build());
    Object result = this.mvc.invoke(this.defaultUser, principal);
    assertThat(result instanceof Health).isTrue();
    assertThat(((Health) result).getStatus() == Status.UP).isTrue();
    assertThat(((Health) result).getDetails().get("foo")).isEqualTo("bar");
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Health(org.springframework.boot.actuate.health.Health) Authentication(org.springframework.security.core.Authentication) Test(org.junit.Test)

Example 14 with Health

use of org.springframework.boot.actuate.health.Health in project spring-boot by spring-projects.

the class HealthMvcEndpointTests method healthIsCached.

@Test
public void healthIsCached() {
    given(this.endpoint.getTimeToLive()).willReturn(10000L);
    given(this.endpoint.invoke()).willReturn(new Health.Builder().up().withDetail("foo", "bar").build());
    Object result = this.mvc.invoke(this.defaultUser, null);
    assertThat(result instanceof Health).isTrue();
    Health health = (Health) result;
    assertThat(health.getStatus() == Status.UP).isTrue();
    assertThat(health.getDetails()).hasSize(1);
    assertThat(health.getDetails().get("foo")).isEqualTo("bar");
    given(this.endpoint.invoke()).willReturn(new Health.Builder().down().build());
    // insecure now
    result = this.mvc.invoke(this.request, null);
    assertThat(result instanceof Health).isTrue();
    health = (Health) result;
    // so the result is cached
    assertThat(health.getStatus() == Status.UP).isTrue();
    // but the details are hidden
    assertThat(health.getDetails()).isEmpty();
}
Also used : Health(org.springframework.boot.actuate.health.Health) Test(org.junit.Test)

Example 15 with Health

use of org.springframework.boot.actuate.health.Health in project spring-boot by spring-projects.

the class HealthMvcEndpointTests method customMapping.

@Test
@SuppressWarnings("unchecked")
public void customMapping() {
    given(this.endpoint.invoke()).willReturn(new Health.Builder().status("OK").build());
    this.mvc.setStatusMapping(Collections.singletonMap("OK", HttpStatus.INTERNAL_SERVER_ERROR));
    Object result = this.mvc.invoke(this.request, null);
    assertThat(result instanceof ResponseEntity).isTrue();
    ResponseEntity<Health> response = (ResponseEntity<Health>) result;
    assertThat(response.getBody().getStatus().equals(new Status("OK"))).isTrue();
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
Also used : Status(org.springframework.boot.actuate.health.Status) HttpStatus(org.springframework.http.HttpStatus) ResponseEntity(org.springframework.http.ResponseEntity) Health(org.springframework.boot.actuate.health.Health) Test(org.junit.Test)

Aggregations

Health (org.springframework.boot.actuate.health.Health)22 Test (org.junit.Test)20 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 ResponseEntity (org.springframework.http.ResponseEntity)4 HealthEndpoint (org.springframework.boot.actuate.endpoint.HealthEndpoint)3 HttpStatus (org.springframework.http.HttpStatus)3 Status (org.springframework.boot.actuate.health.Status)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Writer (java.io.Writer)1 MockServiceTicket (org.apereo.cas.mock.MockServiceTicket)1 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 Authentication (org.springframework.security.core.Authentication)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 Rollback (org.springframework.test.annotation.Rollback)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1