In this section we will look at Contract Driven Development using Spring Cloud Contract (SCC) from the perspective of a (Angular) client side developer. We will see how to use the SCC docker images offers for validating an API fulfills the behaviors defined in the contracts and run a stub server based upon the contracts giving a stable backend for client developers to develop and test against.
SCC offers a docker image
springcloud/spring-cloud-contract:2.1.1.RELEASE for running contracts against an API to validate the API matches the behavior described in the contracts.
In the root
contract-driven-developmentthere is the shell script
validate-produce-service.sh in this script file you will want to update line 11, with your github repo location.
docker run --rm \-e "APPLICATION_BASE_URL=http://18.104.22.168:31416" \-e "PUBLISH_ARTIFACTS=true" \-e "PROJECT_NAME=produce-service" \-e "PROJECT_GROUP=com.ibm.developer" \-e "REPO_WITH_BINARIES_URL=http://host.docker.internal:8081/artifactory/libs-release-local" \-e "PROJECT_VERSION=0.0.1-SNAPSHOT" \-e "EXTERNAL_CONTRACTS_GROUP_ID=com.ibm.developer" \-e "EXTERNAL_CONTRACTS_ARTIFACT_ID=produce-service" \-e "EXTERNAL_CONTRACTS_VERSION=0.0.1-SNAPSHOT" \-e "EXTERNAL_CONTRACTS_REPO_WITH_BINARIES_URL=git://https://github.com/wkorando/produce-contracts.git" \-v "$(pwd)/build-output:/spring-cloud-contract-output/" \-v "$(pwd)/contracts/:/contracts:ro" \springcloud/spring-cloud-contract:2.1.1.RELEASE
With that updated execute the script:
Four of the five contracts should fail, but one of them,
validate_find_produce_by_name_invalid_character will fail. From the root of
contract-driven-development navigate to
/build-output/reports/tests/test/index.html , this provides a report as to which tests failed and why. This can be helpful for debugging purposes.
If you update the second line of
validate-produce-service.sh to the below (note you will be pointing to port 31417 instead of 31416), all the tests should pass.
-e "APPLICATION_BASE_URL=http://22.214.171.124:31417" \
Once all the contracts pass
springcloud/spring-cloud-contract:2.1.1.RELEASE will generate an artifact in this case
produce-service-0.0.1-stubs.jar. This artifact contains json files that can be read by a WireMock server to mock out a server that is actually addressable over HTTP. This is the stable backend mentioned at the intro to this section that client developers can use for developing and test against.
To run the stub server from the root of
contract-driven-development execute the following:
This will start up the mock service residing at http://localhost:9876/api/v1/produce.
The angular application can be easily initialized to connect to either the mock API or the Real API.
To connect to the mock API run the following:
$ npm start:contract
To connect to the real API run the following:
npm run start:contract
The application will be available at http://localhost:4200. When connected to the real API you will see how the values returned are impacted by changes to state (new produce items being added) whereas the responses the angular app gets when calling the mock service always stay the same.