Skip to content

Commit 5189856

Browse files
authored
feat: Add readiness and liveness endpoints #28
* feat: add readiness and liveness endpoints to actuator #28 * feat: show health detalis when authorized #28 * test: test application availability #28 * feat: add actuator endpoint to swagger 28 * feat: add favicon to backend #28 * test: test routes of actuator #28 * test: adding missing tests or correcting existing tests #28 * refactor: remove double semicolon #28 * test: add test for health endpoint #28
1 parent 393eb0e commit 5189856

4 files changed

Lines changed: 72 additions & 2 deletions

File tree

backend/src/main/resources/application-dev.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ spring.datasource.driver-class-name=org.postgresql.Driver
99
spring.flyway.locations=classpath:db/migration,classpath:db/dev-data-migration
1010
spring.flyway.schemas=public
1111

12-
springdoc.api-docs.enabled=true
12+
springdoc.api-docs.enabled=true
13+
springdoc.show-actuator=true

backend/src/main/resources/application.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ spring.flyway.locations=classpath:db/migration
66
# debug options
77
logging.level.org.flywaydb.core=warn
88

9-
springdoc.api-docs.enabled=false
9+
springdoc.api-docs.enabled=false
10+
11+
management.endpoint.health.show-details=when_authorized
12+
management.endpoint.health.probes.enabled=true
2.19 KB
Binary file not shown.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package ch.puzzle.pcts;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
5+
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.ActiveProfiles;
12+
import org.springframework.test.context.DynamicPropertyRegistry;
13+
import org.springframework.test.context.DynamicPropertySource;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
import org.testcontainers.containers.PostgreSQLContainer;
16+
import org.testcontainers.junit.jupiter.Container;
17+
import org.testcontainers.junit.jupiter.Testcontainers;
18+
19+
@SpringBootTest
20+
@ActiveProfiles("test")
21+
@AutoConfigureMockMvc
22+
@Testcontainers
23+
class ApplicationAvailabilityTest {
24+
private static final String BASEURL = "/actuator/health/";
25+
@Container
26+
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:17-alpine");
27+
@Autowired
28+
private MockMvc mvc;
29+
30+
@DynamicPropertySource
31+
static void configureProperties(DynamicPropertyRegistry registry) {
32+
registry.add("spring.datasource.url", postgres::getJdbcUrl);
33+
registry.add("spring.datasource.username", postgres::getUsername);
34+
registry.add("spring.datasource.password", postgres::getPassword);
35+
}
36+
37+
@DisplayName("Should be healthy")
38+
@Test
39+
void shouldBeHealthy() throws Exception {
40+
mvc
41+
.perform(get(BASEURL))
42+
.andExpect(status().isOk())
43+
.andExpect(jsonPath("$.status").value("UP"))
44+
.andExpect(content().string("{\"status\":\"UP\",\"groups\":[\"liveness\",\"readiness\"]}"));
45+
}
46+
47+
@DisplayName("Should accept traffic")
48+
@Test
49+
void shouldAcceptTraffic() throws Exception {
50+
mvc
51+
.perform(get(BASEURL + "readiness"))
52+
.andExpect(status().isOk())
53+
.andExpect(jsonPath("$.status").value("UP"))
54+
.andExpect(content().string("{\"status\":\"UP\"}"));
55+
}
56+
57+
@DisplayName("Should be live")
58+
@Test
59+
void shouldBeLive() throws Exception {
60+
mvc
61+
.perform(get(BASEURL + "liveness"))
62+
.andExpect(status().isOk())
63+
.andExpect(jsonPath("$.status").value("UP"))
64+
.andExpect(content().string("{\"status\":\"UP\"}"));
65+
}
66+
}

0 commit comments

Comments
 (0)