cdc-workshop
cdc-workshop copied to clipboard
A workshop on Consumer-Driven-Contracts
Workshop on Consumer-Driven Contracts
This repository points to some material that can be used in a hands-on workshop to get a feel for Consumer-Driven Contracts with the frameworks Pact and / or Spring Cloud Contract.
Preparation
If you're new to Consumer-Driven Contracts, read this article as an introduction to CDC.
The setup takes some time since dependencies have to be loaded from the internet. So it makes sense to follow these steps in advance:
- make sure you have Java 8 installed
- for the Angular example, make sure you have the latest NodeJS installed
- clone this repository
1. REST Contracts
Task #1: Define a REST contract
In a group of 2-3, dream up a use case and define a contract between a REST consumer and a REST provider.
The contract should consist of 2-3 interactions (request / response pairs).
Per interaction, define
- the path of the request
- the HTTP method of the request
- potential request headers
- potential query parameters in the request
- the JSON structure of the request body
- the JSON structure of the response body
- the HTTP status code of the response
- potential response headers.
Write down the definition in a text file or on paper for later reference.
REST Contracts with Java
Task #2: Implement a REST Consumer with Spring, Feign & Pact
- Start with the code in the folder consumer/pact-feign-consumer
- Go through the steps explained in this tutorial
to set up a consumer test that creates a pact file
- use one interaction of the API you defined in Task #1 instead of the API described in the tutorial
- choose different consumer and provider names to make your implementation unique
- If you need orientation, have a look at the complete example
- Run
./gradlew buildto run the tests and check that a pact file has been created in the foldertarget/pacts
Task #3: Implement a REST Provider with Spring & Pact
- Start with the code in the folder provider/pact-spring-provider
- Go through the steps explained in this tutorial to set up a Spring REST Controller that satisfies the contract from Task #1
- If you need orientation, have a look at the complete example
- Run
./gradlew buildto run the provider test and check that it works
Task #4: Implement a REST Consumer & Provider with Spring Cloud Contract
- Start with the code in the folders consumer/scc-consumer and provider/scc-provider
- Go through the steps explained in this tutorial
and this tutorial
to set up a REST Consumer & Provider with Spring Cloud Contract
- use one interaction of the API you defined in Task #1 instead of the API described in the tutorials
- If you need orientation, have a look at the complete example: consumer and producer
- Run
./gradlew buildin the consumer and producer codebase to check that the consumer and producer tests work as expected
Task #5: Use a Pact Broker
- Insert the pact broker credentials you have been provided into the
pactsection of thebuild.gradlefile of the consumer you created in Task #2 - Run
./gradlew buildin the consumer code base to create the pact file - Run
./gradlew pactPublishin the consumer code base to publish the pact to the broker - Visit the Pact Broker in your browser and check that the pact has been published
- Replace the
@PactFolderannotation in the provider test from Task #3 with the@PactBrokerannotation to load the pact from the broker instead of from the file system
REST Contracts with Node
Task #6 Implement a REST Consumer with Node / React & Pact
- Start with an empty workspace
- Go through the steps explained in this tutorial
to set up a consumer test that creates a pact file
- use one interaction of the API you defined in Task #1 instead of the API described in the tutorial
- choose different consumer and provider names to make your implementation unique
- to publish the contract on a pact broker, use the pact broker credentials you have been provided
- If you need orientation, have a look at the complete example
- Run
npm run test:pactto run the tests and check that a pact file has been created in the folder./pacts
Task #7: Implement a REST Consumer with Angular & Pact
- Download and install the latest NodeJS distribution at https://nodejs.org/en/download/
- Verify that npm and Node are installed properly by calling
node -vandnpm -v - Start with the code in the folder consumer/pact-angular-consumer
- Run
npm installin the folder and check that anode_modulesfolder has been created - Run
npm run testto verify that the Angular tests run on your machine (you need to have Google Chrome installed) - Go through the steps explained in this tutorial
to set up a consumer test that creates a pact file
- use one interaction of the API you defined in Task #1 instead of the API described in the tutorial
- choose different consumer and provider names to make your implementation unique
- If you need orientation, have a look at the complete example
- Run
npm run testagain to run the tests and check that a pact file has been created in thepactsfolder
Task #8: Implement a REST Provider with Node & Pact
- Start with an empty workspace
- Go through the steps explained in this tutorial
to set up a provider test against the contract you have created in task #5 or #6
- to load the contract from the pact broker, use the pact broker credentials you have been provided
- If you need orientation, have a look at the complete example
- Run
npm run test:pactto run the provider tests and check make the test pass
Messaging Contracts
Task #9: Define a Messaging Contract
In a group of 2-3, dream up a use case and define a contract between a message consumer and a message producer.
- The contract should consist of one or two message types.
- Per message, define the JSON structure of the message body.
- Write down the definition in a text file or on paper for later reference.
Task #10: Implement a Spring Messaging Consumer & Provider with Pact
- Start with the code in the folders consumer/pact-message-consumer and provider/pact-message-producer
- Go through the steps explained in this tutorial to set up a Spring Messaging Consumer and Producer that satisfy the contract from Task #8
- If you need orientation, have a look at the complete example: consumer and producer
- Run
./gradlew buildin the consumer and producer codebase to check that the consumer and producer tests work as expected
Task #11: Implement a Node Messaging Consumer & Provider with Pact
- Start with an empty workspace
- Go through the steps explained in this tutorial to set up a Node Messaging Consumer and Producer that satisfy the contract from Task #8
- If you need orientation, have a look at the complete example: consumer
- Run
npm run test:pact:consumerandnpm run test:pact:providerto check that the consumer and producer tests work as expected
GraphQL Contracts
Task 12: Define a GraphQL Contract
In a group of 2-3, dream up a use case and define a GraphQL Schema
- The contract should consist of one or two interactions
- Write down the definition in a text file or on paper for later reference.
Task #13: Implement a Node-based GraphQL Consumer with Pact
- Start with an empty workspace
- Go through the steps explained in this tutorial
to set up a Node GraphQL Consumer
- use one interaction of the API you defined in Task #12
- choose different consumer and provider names than the ones used in the tutorial to make your implementation unique
- to publish the contract on a pact broker, use the pact broker credentials you have been provided
- If you need orientation, have a look at the complete example
- Run
npm run test:pact:graphqlto check that the consumer test works as expected
Task #14 Implement a Node-based GraphQL Provider with Pact
- Start with an empty workspace
- Go through the steps explained in this tutorial
to set up a Node GraphQL Provider
- to load the contract from the pact broker, use the pact broker credentials you have been provided
- If you need orientation, have a look at the complete example
- Run
npm run test:pact:graphqlto check that the provider test works as expected