From c9f713bc12580f0c7c0bbfa84fed11150ee31584 Mon Sep 17 00:00:00 2001 From: RouxAntoine Date: Sat, 4 Nov 2023 18:28:47 +0100 Subject: [PATCH] feature: simple application test --- pom.xml | 4 ++ src/test/java/tk/antoine/roux/MainIT.java | 73 +++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/test/java/tk/antoine/roux/MainIT.java diff --git a/pom.xml b/pom.xml index 8b92a3d..b85391e 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,10 @@ + + org.springframework.boot + spring-boot-starter-webflux + org.springframework.boot spring-boot-starter-actuator diff --git a/src/test/java/tk/antoine/roux/MainIT.java b/src/test/java/tk/antoine/roux/MainIT.java new file mode 100644 index 0000000..cbc3172 --- /dev/null +++ b/src/test/java/tk/antoine/roux/MainIT.java @@ -0,0 +1,73 @@ +package tk.antoine.roux; + +import org.assertj.core.api.Assertions; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.reactive.server.EntityExchangeResult; +import org.springframework.test.web.reactive.server.FluxExchangeResult; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.client.MockMvcWebTestClient; + +import java.util.regex.Pattern; + +import static org.hamcrest.Matchers.containsString; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.http.MediaType.TEXT_PLAIN; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureWebTestClient +@AutoConfigureObservability +class MainIT { + + @Autowired + private WebTestClient webClient; + + @Test + @DisplayName("Check that actuator health endpoint return 200") + void actuatorHealth() { + // Given + + // When + ResponseSpec result = webClient.get() + .uri("/api/health") + .accept(APPLICATION_JSON) + .exchange(); + + // Then + result.expectStatus().isOk(); + } + + @Test + @DisplayName("Check that actuator prometheus contain metric 'application_ready_time_seconds'") + void actuatorPrometheus() { + // Given + + // When + ResponseSpec result = webClient.get() + .uri("/api/prometheus") + .accept(TEXT_PLAIN) + .exchange(); + + // Then + EntityExchangeResult stringEntityExchangeResult = result + .expectStatus().isOk() + .expectBody(String.class) + .returnResult(); + + String responseBody = stringEntityExchangeResult.getResponseBody(); + Assertions.assertThat(responseBody).containsPattern( + Pattern.compile("^application_ready_time_seconds.* [0-9]\\.[0-9]*$", Pattern.MULTILINE) + ); + } +}