contracts: consumer-driven contract testing lab (Spring Cloud Contract 5.0.3, Stubborn Contract 0.2.4, Pact JVM 4.7.7 on Spring Boot 4.2.0)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b7h4ZNF4BDUpJk3Zou
This commit is contained in:
Claude
2026-11-29 02:29:20 +05:30
parent d7497c8ab1
commit 8a429ca578
46 changed files with 1986 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
"""Reduce a Maven run to the lines that carry evidence: test results, assertion messages, pact verification
output. Removes launcher noise, Spring Boot banners and startup logs, WireMock request dumps and stack frames.
Usage: clean.py ROOT < raw > clean"""
import re, sys
root = sys.argv[1]
drop = re.compile(r"""^(
Picked\ up|WARNING:|OpenJDK\ 64-Bit|Mockito\ is\ currently|\[INFO\]\ (---|Scanning|Replacing|The\ original|Building|\ \ from|Deleting|Recompiling|Compiling|
skip|Will\ use|Directory\ with|Test\ Source|Using\ \[|Creating\ new|Generated|Converting|\ \ \ \ \ Spring|Stub\ Server|Files\ matching|
Total\ time|Finished|Results|Using\ auto|\ T\ E\ S\ T\ S|-----|Generating|Installing|Building\ jar|Copying|Nothing|Download)|
\d{4}-\d\d-\d\dT|\d\d:\d\d:\d\d\.\d+\ \[main\]|\s+at\ |\s+\.\.\.\ \d+\ more|
\s*\.\ \ \ ____|\s*/\\\\|\s*\(\ \(|\s*\\\\/|\s*'\ \ \||\s*=====|\ ::\ Spring\ Boot|
\s*Please\ note:|\s*and\ operating|\s*variable\ to|\[ERROR\]\s*$|\[INFO\]\s*$|\[ERROR\]\ (For\ more|Re-run|To\ see|Failed\ to\ execute|->\ \[Help|\[Help|See\ |Please\ read)|
Nov\ \d+,|org\.junit\.jupiter\.engine|OpenJDK|Found\ multiple|You\ may\ wish|\s+(jar:file|Content-Type|Host:|Content-Length|Connection|User-Agent)
)""", re.X)
out, skip_block = [], False
for line in sys.stdin:
line = line.rstrip("\n")
line = line.replace(root, "<repo>/contracts").replace("/root/.m2", "~/.m2")
line = re.sub(r"Time elapsed: [0-9.]+ s", "Time elapsed: N s", line)
# WireMock request/response dumps print between "127.0.0.1 - <verb>" / "Request received:" and the next test line
if re.match(r"^(127\.0\.0\.1 - |Request received:|Matched response definition:|Response:|-{10,}$)", line):
skip_block = True
if line.startswith("[INFO] Running") or line.startswith("[INFO] Tests run") or line.startswith("[ERROR] Tests run") or line.startswith("[INFO] BUILD") or line.startswith("[ERROR] Failures") or line.startswith("[ERROR] Errors"):
skip_block = False
if skip_block or drop.match(line):
continue
out.append(line)
text = "\n".join(out)
text = re.sub(r"\n{3,}", "\n\n", text).strip("\n")
print(text)
+79
View File
@@ -0,0 +1,79 @@
#!/usr/bin/env bash
# Regenerates every file in contracts/output/. Needs JDK 25, Maven, internet for the first build.
# Order matters: the pact is written by the consumer, then verified by the producer; the stubs are
# published by the producer, then used by the consumer.
set -uo pipefail
cd "$(dirname "$0")/.."
ROOT=$(pwd)
OUT=$ROOT/output
export pact_do_not_track=true
# keep only evidence: see scripts/clean.py
clean() { python3 -I scripts/clean.py "$ROOT"; }
mkdir -p "$OUT"
# FROM=n resumes at stage n without deleting earlier output (handy when something else on the machine kills a JVM)
[ -z "${FROM:-}" ] && rm -f "$OUT"/*.txt "$OUT"/*.json
stage() { [ "$1" -ge "${FROM:-1}" ]; }
run() { # run <dir> <outfile> <env...> -- <mvn args>
local dir=$1 file=$2; shift 2
local envs=(); while [ "$1" != "--" ]; do envs+=("$1"); shift; done; shift
( cd "$ROOT/$dir" && env "${envs[@]}" mvn -B "$@" 2>&1 ) | clean > "$OUT/$file"
}
{
echo "# JDK"; java -version 2>&1 | grep -v 'Picked up'
echo; echo "# Maven"; mvn -v 2>&1 | grep -v 'Picked up' | head -1
echo; echo "# producer, resolved test-scope contract libraries"
( cd orders-producer && mvn -B dependency:tree -Dscope=test 2>&1 ) | grep -E "spring-cloud-starter-contract-verifier|spring-cloud-contract-verifier:|wiremock-standalone|rest-assured:jar|groovy:jar|pact.provider:junit5|au.com.dius.pact:provider|spring-boot-starter-parent" | sed -E 's/^\[INFO\] [|+\\ -]*//'
echo; echo "# consumer, resolved test-scope libraries"
( cd checkout-consumer && mvn -B dependency:tree -Dscope=test 2>&1 ) | grep -E "stub-runner:jar|wiremock|pact.consumer:junit5|spring-boot-starter-restclient:" | sed -E 's/^\[INFO\] [|+\\ -]*//'
} > "$OUT/01-versions.txt"
stage 1 && {
# 1. consumer writes the pact (also proves OrderClient works against a mock shaped like the contract)
run checkout-consumer 02-consumer-pact-run.txt -- clean test -Dtest=OrdersPactConsumerTest
cp checkout-consumer/target/pacts/checkout-consumer-orders-producer.json "$OUT/03-pact-file.json"
mkdir -p orders-producer/src/test/resources/pacts
cp checkout-consumer/target/pacts/checkout-consumer-orders-producer.json orders-producer/src/test/resources/pacts/
# 2. producer: generate tests from contracts, verify the pact, publish stubs
run orders-producer 04-producer-baseline.txt -- clean install
cp orders-producer/target/generated-test-sources/contracts/com/ankurm/contracts/orders/OrdersTest.java "$OUT/05-generated-test.txt"
unzip -l ~/.m2/repository/com/ankurm/contracts/orders-producer/1.0.0/orders-producer-1.0.0-stubs.jar | sed -E 's/ +[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9:]+ +/ /' > "$OUT/06-stubs-jar.txt"
unzip -p ~/.m2/repository/com/ankurm/contracts/orders-producer/1.0.0/orders-producer-1.0.0-stubs.jar 'META-INF/com.ankurm.contracts/orders-producer/1.0.0/mappings/orders/shouldReturnOrder.json' > "$OUT/07-wiremock-mapping.json"
}
stage 3 && {
# 3. consumer runs against the published stubs
run checkout-consumer 08-consumer-stub-run.txt -- clean test -Dtest=OrdersStubConsumerTest
}
stage 4 && {
# 4. producer changes, one at a time. Same environment variable drives both tools.
i=10
for v in add-field rename-total drop-items total-string; do
run orders-producer "$i-variant-$v.txt" "ORDERS_VARIANT=$v" -- clean test; i=$((i+1))
done
}
stage 5 && {
# 5. REST Assured 5.5.7 (what Spring Cloud Contract 5.0.3 ships) against Groovy 5.1.3 (what Boot 4.2 manages)
run orders-producer 20-restassured-5-groovy-5.txt -- clean test -Dtest=OrdersTest -Drest-assured.version=5.5.7
}
stage 6 && {
# 6. the producer evolves its contract on purpose (rename total) and republishes stubs (SCC tests only: the old pact would rightly block it); the consumer meets the new stubs. Scratch copy keeps the repo sources untouched.
SCR=$(mktemp -d); cp -r orders-producer "$SCR/p"; rm -rf "$SCR/p/target"
sed -i 's/total:/totalAmount:/' "$SCR/p"/src/test/resources/contracts/orders/*.yml
( cd "$SCR/p" && ORDERS_VARIANT=rename-total mvn -B clean install -Dtest=OrdersTest 2>&1 ) | clean > "$OUT/21-evolved-contract-producer.txt"
run checkout-consumer 22-evolved-contract-consumer.txt -- clean test -Dtest=OrdersStubConsumerTest
rm -rf "$SCR"
# put the baseline stubs back
( cd orders-producer && mvn -B -q clean install 2>&1 ) >/dev/null
}
stage 7 && {
# 7. the same contracts under Stubborn Contract 0.2.4
run orders-producer 30-stubborn-producer.txt -- -f pom-stubborn.xml clean install
run checkout-consumer 31-stubborn-consumer.txt -- -f pom-stubborn.xml clean test
}
echo "done: $(ls "$OUT" | wc -l) files in $OUT/"