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:
Executable
+79
@@ -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/"
|
||||
Reference in New Issue
Block a user