diff --git a/README.md b/README.md index d75acd1..b50bd96 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ The interaction between Order API in this project and it's consumers is governed 2. Specmatic 3. Docker Desktop (for running contract tests with containers) +The service exposes its Actuator health endpoint at `/actuator/health` for readiness checks. + ## Run Contract Tests Specmatic contract tests use the [specmatic.yaml](specmatic.yaml) configuration file to start the required specmatic mock before verifying the Order API. @@ -79,4 +81,3 @@ docker run --rm --network host -v "%cd%/specmatic.yaml:/usr/src/app/specmatic.ya * [Specmatic Website](https://specmatic.io) * [Specmatic Documentation](https://docs.specmatic.io) - diff --git a/pom.xml b/pom.xml index 2f6e6e8..717e5ff 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,12 @@ ${spring.boot.version} + + org.springframework.boot + spring-boot-starter-actuator + ${spring.boot.version} + + org.springframework.boot spring-boot-starter-security @@ -140,6 +146,12 @@ jaxb-runtime 4.0.9 + + javax.xml.bind + jaxb-api + 2.3.1 + test + diff --git a/src/main/java/com/store/config/SecurityConfig.kt b/src/main/java/com/store/config/SecurityConfig.kt index 9a426bd..4718651 100644 --- a/src/main/java/com/store/config/SecurityConfig.kt +++ b/src/main/java/com/store/config/SecurityConfig.kt @@ -23,6 +23,7 @@ open class SecurityConfig { .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() .addFilter(filter) .authorizeRequests() + .requestMatchers("/actuator/health").permitAll() .anyRequest() .authenticated() return http.build() @@ -31,4 +32,4 @@ open class SecurityConfig { companion object { const val API_TOKEN = "authenticate" } -} \ No newline at end of file +} diff --git a/src/test/java/com/store/ContractTestUsingTestContainerTest.java b/src/test/java/com/store/ContractTestUsingTestContainerTest.java index 5761728..d184ebe 100644 --- a/src/test/java/com/store/ContractTestUsingTestContainerTest.java +++ b/src/test/java/com/store/ContractTestUsingTestContainerTest.java @@ -2,6 +2,7 @@ import com.store.model.DB; import org.assertj.core.api.Assertions; +import org.awaitility.Awaitility; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -13,6 +14,12 @@ import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.junit.jupiter.Testcontainers; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; + @Testcontainers @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @EnabledIf(value = "isNonCIOrLinux", disabledReason = "Run only on Linux in CI; all platforms allowed locally") @@ -33,6 +40,7 @@ private static String enterpriseImage(){ static { testContainer = new GenericContainer<>(enterpriseImage()) .withCommand("test") + .withFileSystemBind(System.getProperty("user.home") + "/.specmatic", "/root/.specmatic", BindMode.READ_ONLY) .withFileSystemBind("./specmatic.yaml", "/usr/src/app/specmatic.yaml", BindMode.READ_ONLY) .withFileSystemBind("./build/reports/specmatic", "/usr/src/app/build/reports/specmatic", BindMode.READ_WRITE) .withNetworkMode("host") @@ -42,6 +50,7 @@ private static String enterpriseImage(){ stubContainer = new GenericContainer<>(enterpriseImage()) .withCommand("mock") + .withFileSystemBind(System.getProperty("user.home") + "/.specmatic", "/root/.specmatic", BindMode.READ_ONLY) .withFileSystemBind( "./wsdls", "/usr/src/app/wsdls", @@ -70,7 +79,13 @@ public static void cleanUp(){ } @Test - void specmaticContractTest() { + void specmaticContractTest() throws Exception { + HttpClient client = HttpClient.newHttpClient(); + Awaitility.await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> { + HttpRequest request = HttpRequest.newBuilder(URI.create("http://localhost:" + APPLICATION_PORT + "/actuator/health")).GET().build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.discarding()); + Assertions.assertThat(response.statusCode()).isEqualTo(200); + }); stubContainer.start(); testContainer.start(); boolean hasSucceeded = testContainer.getLogs().contains("Failures: 0");