Merge pull request '补齐灰度审计服务真实健康端点' (#21) from test into main
This commit was merged in pull request #21.
This commit is contained in:
@@ -53,6 +53,10 @@
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.baiyue.audit;
|
||||
|
||||
import com.baiyue.common.web.CommonWebAutoConfiguration;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.health.HealthEndpointAutoConfiguration;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
|
||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
class AuditHealthEndpointTest {
|
||||
private final WebApplicationContextRunner context = new WebApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(
|
||||
JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
|
||||
WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class, EndpointAutoConfiguration.class,
|
||||
HealthEndpointAutoConfiguration.class, WebEndpointAutoConfiguration.class,
|
||||
ManagementContextAutoConfiguration.class, CommonWebAutoConfiguration.class))
|
||||
.withPropertyValues("management.endpoints.web.exposure.include=health");
|
||||
|
||||
@Test
|
||||
void actuatorHealthReturnsUpInsteadOfBusinessResourceNotFound() {
|
||||
context.withBean("dependencyHealthIndicator", HealthIndicator.class, () -> () -> Health.up().build())
|
||||
.run(application -> MockMvcBuilders.webAppContextSetup(application).build()
|
||||
.perform(get("/actuator/health"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value("UP"))
|
||||
.andExpect(jsonPath("$.code").doesNotExist()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedHealthContributorReturnsDownAndServiceUnavailable() {
|
||||
context.withBean("dependencyHealthIndicator", HealthIndicator.class, () -> () -> Health.down().build())
|
||||
.run(application -> MockMvcBuilders.webAppContextSetup(application).build()
|
||||
.perform(get("/actuator/health"))
|
||||
.andExpect(status().isServiceUnavailable())
|
||||
.andExpect(jsonPath("$.status").value("DOWN"))
|
||||
.andExpect(jsonPath("$.code").doesNotExist()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user