Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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)


12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<version>${spring.boot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring.boot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
Expand Down Expand Up @@ -140,6 +146,12 @@
<artifactId>jaxb-runtime</artifactId>
<version>4.0.9</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/store/config/SecurityConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ open class SecurityConfig {
.sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.addFilter(filter)
.authorizeRequests()
.requestMatchers("/actuator/health").permitAll()
.anyRequest()
.authenticated()
return http.build()
Expand All @@ -31,4 +32,4 @@ open class SecurityConfig {
companion object {
const val API_TOKEN = "authenticate"
}
}
}
17 changes: 16 additions & 1 deletion src/test/java/com/store/ContractTestUsingTestContainerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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",
Expand Down Expand Up @@ -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<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
Assertions.assertThat(response.statusCode()).isEqualTo(200);
});
stubContainer.start();
testContainer.start();
boolean hasSucceeded = testContainer.getLogs().contains("Failures: 0");
Expand Down
Loading