-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
486a5ed
commit 0486d26
Showing
3 changed files
with
63 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 39 additions & 18 deletions
57
backend-spring-boot/src/test/java/no/vicx/backend/ActuatorHealthTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,64 @@ | ||
package no.vicx.backend; | ||
|
||
import no.vicx.backend.testconfiguration.TestSecurityConfig; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.boot.test.web.server.LocalManagementPort; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsInAnyOrder; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@AutoConfigureWebTestClient | ||
@Import(TestSecurityConfig.class) | ||
class ActuatorHealthTest { | ||
|
||
@LocalManagementPort | ||
private int managementPort; | ||
|
||
@Autowired | ||
TestRestTemplate restTemplate; | ||
WebTestClient webClient; | ||
|
||
private String baseManagementUrl; | ||
@ParameterizedTest | ||
@MethodSource("probeEndpoints") | ||
void getHealthProbe_givenExpectedComponents_expectComponentsAsInConfig( | ||
String probeEndpoint, List<String> expectedComponents) { | ||
|
||
@BeforeEach | ||
void setup() { | ||
baseManagementUrl = "http://localhost:" + managementPort + "/actuator/health"; | ||
} | ||
var currentProbeUrl = "http://localhost:%d/actuator/health%s" | ||
.formatted(managementPort, probeEndpoint); | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"/liveness", "/readiness"}) | ||
@DisplayName("Health Endpoint Probes Should Return HTTP 200") | ||
void testHealthProbes(String probePath) { | ||
var response = restTemplate.getForEntity(baseManagementUrl + probePath, String.class); | ||
webClient.get() | ||
.uri(currentProbeUrl) | ||
.exchange() | ||
.expectStatus().isOk() | ||
.expectBody(new ParameterizedTypeReference<Map<String, Object>>() { | ||
}) | ||
.consumeWith(result -> { | ||
@SuppressWarnings({"unchecked", "DataFlowIssue"}) | ||
var actualComponents = (Map<String, Object>) result.getResponseBody().get("components"); | ||
|
||
assertEquals(expectedComponents.size(), actualComponents.size()); | ||
assertThat(actualComponents.keySet(), containsInAnyOrder(expectedComponents.toArray())); | ||
}); | ||
} | ||
|
||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
assertEquals("{\"status\":\"UP\"}", response.getBody()); | ||
private static Stream<Arguments> probeEndpoints() { | ||
return Stream.of( | ||
Arguments.of("/readiness", List.of("db", "readinessState")), | ||
Arguments.of("/liveness", Collections.singletonList("livenessState")) | ||
); | ||
} | ||
} |