SlideShare a Scribd company logo
1 of 71
Consumer Driven Contracts
in Java world
About myself
● Application Architect @Thomas
Cook
● Tech Lead - Platformers team
● +9 years Java
Content
● Consumer Driven Contracts - general concepts;
● Consumer Driven Contracts - hygiene for APIs;
● Spring Cloud Contracts;
● Pact;
● Demo.
Monolith: Past expectation
Monolith: Today’s Reality
Conclusion...
We need microservices!!!
They will be small and tidy
Well, yes, but....
And in a year we will see something like:
Conclusion...
Microservices require
more attention to
standards
Will your cool, modern microservices bring any
value in terms of time-to-market...
Will your cool, modern microservices bring any
value in terms of time-to-market...
...without agreements/contracts between them?
In terms of Quality Attributes:
It’s never enough to comply with Runtime
attributes
when your Design attributes are not in order.
We started with API Documentation at the
time of decomposing our monolith
But as time goes by we have come to realize
it’s not enough...
Because our
documented API
looks more like this:
And our expectation was a system where
services know about each other
Standards are required for things like monitoring,
logging, backward compatibility, and API contracts.
API Contracts
Lean principle:
A service is of value to the business only to the
extent it is consumed.
https://martinfowler.com/articles/consumerDrivenContracts.html
Make sure the Contracts can be imposed
Set of rules and
technologies that should be
implemented and adopted
on both sides.
Do it as early as you can!
Option 1: testing a chain of services without
contracts
You can go the hard way and deploy all
microservices at once to test them
together
Drawbacks:
● Long cycle;
● Full Infrastructure required;
● Hard to debug;
● Very late feedback;
Option 2: Mock other microservices in unit /
integration tests
Drawbacks:
● It simple has nothing to do with
the reality if you do it in isolation.
Postel's law
“Be conservative in what you do, be liberal in
what you accept from others
(often reworded as "Be conservative in what you
send, be liberal in what you accept").”
Doesn’t Contract couple our services?
Enter Consumer Driven Contracts
Producer should have a contract for every
specific Consumer
It means that potentially there can be multiple
contracts, a separate one for each Consumer,
Contract visualisation:
API:
get: customers/{id}
post: customers/
put: customers/{id}
delete: customers/{id}
get: customers/{id}/bookings/{id}
post: customers/{id}/bookings/
delete: customers/{id}/bookings/{id}Producer
API:
get: customers/{id}
post: customers/
put: customers/{id}
delete: customers/{id}
get: customers/{id}/bookings/{id}
post: customers/{id}/bookings/
delete: customers/{id}/bookings/{id}
Consumer 1
Producer
get: customers/{id}
post: customers/
put: customers/{id}
Contract visualisation:
API:
get: customers/{id}
post: customers/
put: customers/{id}
delete: customers/{id}
get: customers/{id}/bookings/{id}
post: customers/{id}/bookings/
delete: customers/{id}/bookings/{id}
Consumer 1
Producer
Consumer 2
get: customers/{id}
post: customers/
put: customers/{id}
put: customers/{id}
delete: customers/{id}
Contract visualisation:
API:
get: customers/{id}
post: customers/
put: customers/{id}
delete: customers/{id}
get: customers/{id}/bookings/{id}
post: customers/{id}/bookings/
delete: customers/{id}/bookings/{id}
Consumer 1
Producer
Consumer 2
get: customers/{id}
post: customers/
put: customers/{id}
put: customers/{id}
delete: customers/{id}
Consumer 3
get: customers/
{id}/bookings/{id}
post: customers/{id}/
bookings/
Contract visualisation:
API:
get: customers/{id}
post: customers/
put: customers/{id}
delete: customers/{id}
get: customers/{id}/bookings/{id}
post: customers/{id}/bookings/
delete: customers/{id}/bookings/{id}
Consumer 1
Producer
Consumer 2
get: customers/{id}
post: customers/
put: customers/{id}
put: customers/{id}
delete: customers/{id}
Consumer 3
get: customers/
{id}/bookings/{id}
post: customers/{id}/
bookings/
Overlapping part of 2 contracts
Contract visualisation:
API:
get: customers/{id}
post: customers/
put: customers/{id}
delete: customers/{id}
get:
customers/{id}/bookings/{id}
post:
customers/{id}/bookings/
Consumer 1
Producer
Consumer 2
get: customers/{id}
post: customers/
put: customers/{id}
put: customers/{id}
delete: customers/{id}
Consumer 3
get: customers/
{id}/bookings/{id}
post: customers/{id}/
bookings/
Overlapping part of 2 contracts
Completely independent contract
delete: customers/{id}/bookings/{id}
Contract visualisation:
API:
get: customers/{id}
post: customers/
put: customers/{id}
delete: customers/{id}
get:
customers/{id}/bookings/{id}
post:
customers/{id}/bookings/
Consumer 1
Producer
Consumer 2
get: customers/{id}
post: customers/
put: customers/{id}
put: customers/{id}
delete: customers/{id}
Consumer 3
get: customers/
{id}/bookings/{id}
post: customers/{id}/
bookings/
Overlapping part of 2 contracts
Completely independent contract
delete:
customers/{id}/bookings/{id}
No in use
Contract visualisation:
Some parts of the APIs might not be used and
will not impact anyone. They might either be in a
state of active development or about to be
decommissioned
Demystification
These tests are not component tests. They do not
test the behaviour of the service deeply but that
the inputs and outputs of service calls contain
required attributes.
Contract: specific scenario rather than schema
Their main advantage is that they are fast and
reliable.
What tools are there for Java devs?
There are 2 main options:
1. Spring Cloud Contracts;
2. Pact.
Spring Cloud Contracts
● Spring Cloud Contract Verifier - the
producer side;
● Spring Cloud Contract Stub Runner - the
consumer side
Spring Cloud Contracts: Contract Verifier
Shipped with Contract Definition Language
(DSL) written in Groovy or YAML.
Spring Cloud Contract Verifier provides:
● Alignment between stubs and actual
implementation;
● Acceptance test driven development;
● Immediate visibility of contracts on both sides;
● Autogenerated boilerplate tests.
Spring Cloud Contracts: Contract Stub Runner
● Fetches the stubs of the producer;
● Runs tests against the stubs.
Spring Cloud Contracts: Contract Definition
Language
There’s more if you read into specs:
● Groovy DSL
● YAML
Spring Cloud Contracts: Contract Definition
Language
There’s more if you read into specs:
● Groovy DSL
● YAML
● Pact JSON
● Spring Rest Docs
Spring Cloud Contracts: Contract Definition
Language
Spring Cloud Contracts - catchline
“Spring Cloud Contract Verifier moves TDD to
the level of software architecture.”
SCC guarantees:
● Stubs reliability: generated on successful test
execution.
● Stubs reusability: packed in a separate artifact
with a stub classifier.
Spring Cloud Contract: look inside
Server / Producer side utilization:
Maven/gradle plugin + verifier
Server / Producer side: contract DSL
Groovy YAML
package contracts
import org.springframework...t
Contract.make {
request {
method 'PUT'
url '/bookings/4'
body("""
{
"id": 4,
"destination": "USA",
….
}
response {
status 204
request:
method: PUT
url: /bookings/4
body:
id: 4,
destination: "USA"
response:
status: 204
Server / Producer side: base class
<baseClassForTests>....api.controllers.BookingControllerImplTest</...>
Server / Producer side: base class
The responsibility to initialising your MockMvc is on you:
java.lang.IllegalStateException: You haven't configured a MockMVC
instance. You can do this statically
RestAssuredMockMvc.mockMvc(..)
RestAssuredMockMvc.standaloneSetup(..);
RestAssuredMockMvc.webAppContextSetup(..);
or using the DSL:
given().
mockMvc(..). ..
Server / Producer side: producer stubs
Spring Cloud Contract Verifier will convert the contracts into an HTTP
server stub definitions(Wiremock mapping jsons).
Demo time!
● Run Producer tests;
● Spring-cloud-contract-maven-plugin options;
● Run Consumer tests;
● Groovy DSL vs YAML;
● Add stateful behaviour(real MySql instance) for visibility;
● Non-binary storage(git);
● Explain stubrunner yaml property and how it impacts tests;
● Wiremock scenarios;
● produce(), consume() and matchers;
● execute() with custom utility methods;
● Integration with the rest of Spring Cloud.
Pact
The cross-language distributed
contract testing framework.
Pact: language support
● Ruby Pact
● JVM Pact and Scala-Pact
● .NET Pact
● JS Pact
● Go Pact (there is also a v1.1 native Pact Go)
● Swift / Objective-C Pact
● Python
● PHP
Pact: objective
The requirements of the consumer(s)
are used to drive the features of the
provider.
Pact: diagram
Consumer
● Specifies the contracts;
● Generates the pact files;
● Push them into Pact Broker
Pact: diagram
Consumer
● Specifies the contracts;
● Generates the pact files;
● Push them into Pact Broker
Pact Broker
● Stores pact contracts;
● Integrates into CI tools.
Pact: diagram
Consumer
● Specifies the contracts;
● Generates the pact files;
● Push them into Pact Broker
Pact Broker
● Stores pact contracts;
● Integrates into CI tools.
Producer
● Implement a service
according to Consumer
specs.
Demo time!
● Pact core concepts: State, Broker, Verification;
● Versioning;
● Pact extension for Spring Cloud Contracts;
Useful links
● https://github.com/spring-cloud-samples/spring-cloud-contract-samples
● https://github.com/DiUS/pact-workshop-jvm
● https://github.com/DiUS/pact-jvm
● https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-maven
Summary
Use API Contracts ?: Adopt a design first approach ?: Document
your APIs ?: You are hopeless
Thank you!
Mail: yura.v.nosenko@gmail.com
Linkedin: https://www.linkedin.com/in/yuranos/
Github example:
https://github.com/yuranos/raml-based-producer-application,
https://github.com/yuranos/raml-based-consumer-application
(consumer_driven_contracts branches)

More Related Content

Similar to Consumer driven contracts in java world

Consumer Driven Contracts for microservices
Consumer Driven Contracts for microservicesConsumer Driven Contracts for microservices
Consumer Driven Contracts for microservicesReshmi Krishna
 
TDD for Microservices
TDD for MicroservicesTDD for Microservices
TDD for MicroservicesVMware Tanzu
 
Liberty Buildpack: Designed for Extension - Integrating your services in Blue...
Liberty Buildpack: Designed for Extension - Integrating your services in Blue...Liberty Buildpack: Designed for Extension - Integrating your services in Blue...
Liberty Buildpack: Designed for Extension - Integrating your services in Blue...Rohit Kelapure
 
Collaborative Contract Driven Development
Collaborative Contract Driven DevelopmentCollaborative Contract Driven Development
Collaborative Contract Driven DevelopmentBilly Korando
 
Why You Should Be Doing Contract-First API Development
Why You Should Be Doing Contract-First API DevelopmentWhy You Should Be Doing Contract-First API Development
Why You Should Be Doing Contract-First API DevelopmentDevenPhillips
 
Consumer Driven Contracts and Your Microservice Architecture
Consumer Driven Contracts and Your Microservice ArchitectureConsumer Driven Contracts and Your Microservice Architecture
Consumer Driven Contracts and Your Microservice ArchitectureMarcin Grzejszczak
 
Consumer-Driven Contract Testing
Consumer-Driven Contract TestingConsumer-Driven Contract Testing
Consumer-Driven Contract TestingPaulo Clavijo
 
Ultimate Guide to Microservice Architecture on Kubernetes
Ultimate Guide to Microservice Architecture on KubernetesUltimate Guide to Microservice Architecture on Kubernetes
Ultimate Guide to Microservice Architecture on Kuberneteskloia
 
Wcf best practice
Wcf best practiceWcf best practice
Wcf best practiceYu GUAN
 
Workshop: .NET Code Contracts
Workshop: .NET Code ContractsWorkshop: .NET Code Contracts
Workshop: .NET Code ContractsRainer Stropek
 
apidays Australia 2022 - Schemas are not contracts!, Matt Fellows, Pactflow
apidays Australia 2022 - Schemas are not contracts!, Matt Fellows, Pactflowapidays Australia 2022 - Schemas are not contracts!, Matt Fellows, Pactflow
apidays Australia 2022 - Schemas are not contracts!, Matt Fellows, Pactflowapidays
 
Announcing: Native MQTT Integration with HiveMQ and InfluxDB Cloud
Announcing: Native MQTT Integration with HiveMQ and InfluxDB Cloud Announcing: Native MQTT Integration with HiveMQ and InfluxDB Cloud
Announcing: Native MQTT Integration with HiveMQ and InfluxDB Cloud InfluxData
 
Contract Testing
Contract TestingContract Testing
Contract Testingkloia
 
Delivery Pipelines as a First Class Citizen @deliverAgile2019
Delivery Pipelines as a First Class Citizen @deliverAgile2019Delivery Pipelines as a First Class Citizen @deliverAgile2019
Delivery Pipelines as a First Class Citizen @deliverAgile2019ciberkleid
 
Software contracts - Global Enterprise Agile 2023.pdf
Software contracts - Global Enterprise Agile 2023.pdfSoftware contracts - Global Enterprise Agile 2023.pdf
Software contracts - Global Enterprise Agile 2023.pdfSeb Rose
 
How to Live in a Post-Spring-Cloud-Netflix World - Olga Maciaszek-Sharma & Ja...
How to Live in a Post-Spring-Cloud-Netflix World - Olga Maciaszek-Sharma & Ja...How to Live in a Post-Spring-Cloud-Netflix World - Olga Maciaszek-Sharma & Ja...
How to Live in a Post-Spring-Cloud-Netflix World - Olga Maciaszek-Sharma & Ja...VMware Tanzu
 
Consumer driven contract testing
Consumer driven contract testingConsumer driven contract testing
Consumer driven contract testingMike van Vendeloo
 
Building and Scaling a WebSockets Pubsub System
Building and Scaling a WebSockets Pubsub SystemBuilding and Scaling a WebSockets Pubsub System
Building and Scaling a WebSockets Pubsub SystemKapil Reddy
 

Similar to Consumer driven contracts in java world (20)

Consumer Driven Contracts for microservices
Consumer Driven Contracts for microservicesConsumer Driven Contracts for microservices
Consumer Driven Contracts for microservices
 
TDD for Microservices
TDD for MicroservicesTDD for Microservices
TDD for Microservices
 
Liberty Buildpack: Designed for Extension - Integrating your services in Blue...
Liberty Buildpack: Designed for Extension - Integrating your services in Blue...Liberty Buildpack: Designed for Extension - Integrating your services in Blue...
Liberty Buildpack: Designed for Extension - Integrating your services in Blue...
 
Collaborative Contract Driven Development
Collaborative Contract Driven DevelopmentCollaborative Contract Driven Development
Collaborative Contract Driven Development
 
Why You Should Be Doing Contract-First API Development
Why You Should Be Doing Contract-First API DevelopmentWhy You Should Be Doing Contract-First API Development
Why You Should Be Doing Contract-First API Development
 
Consumer Driven Contracts and Your Microservice Architecture
Consumer Driven Contracts and Your Microservice ArchitectureConsumer Driven Contracts and Your Microservice Architecture
Consumer Driven Contracts and Your Microservice Architecture
 
Consumer-Driven Contract Testing
Consumer-Driven Contract TestingConsumer-Driven Contract Testing
Consumer-Driven Contract Testing
 
Ultimate Guide to Microservice Architecture on Kubernetes
Ultimate Guide to Microservice Architecture on KubernetesUltimate Guide to Microservice Architecture on Kubernetes
Ultimate Guide to Microservice Architecture on Kubernetes
 
Code Contracts
Code ContractsCode Contracts
Code Contracts
 
Wcf best practice
Wcf best practiceWcf best practice
Wcf best practice
 
Workshop: .NET Code Contracts
Workshop: .NET Code ContractsWorkshop: .NET Code Contracts
Workshop: .NET Code Contracts
 
apidays Australia 2022 - Schemas are not contracts!, Matt Fellows, Pactflow
apidays Australia 2022 - Schemas are not contracts!, Matt Fellows, Pactflowapidays Australia 2022 - Schemas are not contracts!, Matt Fellows, Pactflow
apidays Australia 2022 - Schemas are not contracts!, Matt Fellows, Pactflow
 
Announcing: Native MQTT Integration with HiveMQ and InfluxDB Cloud
Announcing: Native MQTT Integration with HiveMQ and InfluxDB Cloud Announcing: Native MQTT Integration with HiveMQ and InfluxDB Cloud
Announcing: Native MQTT Integration with HiveMQ and InfluxDB Cloud
 
Contract Testing
Contract TestingContract Testing
Contract Testing
 
Coherent REST API design
Coherent REST API designCoherent REST API design
Coherent REST API design
 
Delivery Pipelines as a First Class Citizen @deliverAgile2019
Delivery Pipelines as a First Class Citizen @deliverAgile2019Delivery Pipelines as a First Class Citizen @deliverAgile2019
Delivery Pipelines as a First Class Citizen @deliverAgile2019
 
Software contracts - Global Enterprise Agile 2023.pdf
Software contracts - Global Enterprise Agile 2023.pdfSoftware contracts - Global Enterprise Agile 2023.pdf
Software contracts - Global Enterprise Agile 2023.pdf
 
How to Live in a Post-Spring-Cloud-Netflix World - Olga Maciaszek-Sharma & Ja...
How to Live in a Post-Spring-Cloud-Netflix World - Olga Maciaszek-Sharma & Ja...How to Live in a Post-Spring-Cloud-Netflix World - Olga Maciaszek-Sharma & Ja...
How to Live in a Post-Spring-Cloud-Netflix World - Olga Maciaszek-Sharma & Ja...
 
Consumer driven contract testing
Consumer driven contract testingConsumer driven contract testing
Consumer driven contract testing
 
Building and Scaling a WebSockets Pubsub System
Building and Scaling a WebSockets Pubsub SystemBuilding and Scaling a WebSockets Pubsub System
Building and Scaling a WebSockets Pubsub System
 

Recently uploaded

Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 

Recently uploaded (20)

Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 

Consumer driven contracts in java world

  • 2. About myself ● Application Architect @Thomas Cook ● Tech Lead - Platformers team ● +9 years Java
  • 3. Content ● Consumer Driven Contracts - general concepts; ● Consumer Driven Contracts - hygiene for APIs; ● Spring Cloud Contracts; ● Pact; ● Demo.
  • 8. They will be small and tidy
  • 10. And in a year we will see something like:
  • 13. Will your cool, modern microservices bring any value in terms of time-to-market...
  • 14. Will your cool, modern microservices bring any value in terms of time-to-market... ...without agreements/contracts between them?
  • 15. In terms of Quality Attributes: It’s never enough to comply with Runtime attributes when your Design attributes are not in order.
  • 16. We started with API Documentation at the time of decomposing our monolith
  • 17. But as time goes by we have come to realize it’s not enough...
  • 19. And our expectation was a system where services know about each other
  • 20. Standards are required for things like monitoring, logging, backward compatibility, and API contracts. API Contracts
  • 21. Lean principle: A service is of value to the business only to the extent it is consumed. https://martinfowler.com/articles/consumerDrivenContracts.html
  • 22. Make sure the Contracts can be imposed Set of rules and technologies that should be implemented and adopted on both sides.
  • 23. Do it as early as you can!
  • 24. Option 1: testing a chain of services without contracts
  • 25. You can go the hard way and deploy all microservices at once to test them together
  • 26. Drawbacks: ● Long cycle; ● Full Infrastructure required; ● Hard to debug; ● Very late feedback;
  • 27. Option 2: Mock other microservices in unit / integration tests
  • 28. Drawbacks: ● It simple has nothing to do with the reality if you do it in isolation.
  • 29. Postel's law “Be conservative in what you do, be liberal in what you accept from others (often reworded as "Be conservative in what you send, be liberal in what you accept").”
  • 30. Doesn’t Contract couple our services?
  • 32. Producer should have a contract for every specific Consumer
  • 33. It means that potentially there can be multiple contracts, a separate one for each Consumer,
  • 34. Contract visualisation: API: get: customers/{id} post: customers/ put: customers/{id} delete: customers/{id} get: customers/{id}/bookings/{id} post: customers/{id}/bookings/ delete: customers/{id}/bookings/{id}Producer
  • 35. API: get: customers/{id} post: customers/ put: customers/{id} delete: customers/{id} get: customers/{id}/bookings/{id} post: customers/{id}/bookings/ delete: customers/{id}/bookings/{id} Consumer 1 Producer get: customers/{id} post: customers/ put: customers/{id} Contract visualisation:
  • 36. API: get: customers/{id} post: customers/ put: customers/{id} delete: customers/{id} get: customers/{id}/bookings/{id} post: customers/{id}/bookings/ delete: customers/{id}/bookings/{id} Consumer 1 Producer Consumer 2 get: customers/{id} post: customers/ put: customers/{id} put: customers/{id} delete: customers/{id} Contract visualisation:
  • 37. API: get: customers/{id} post: customers/ put: customers/{id} delete: customers/{id} get: customers/{id}/bookings/{id} post: customers/{id}/bookings/ delete: customers/{id}/bookings/{id} Consumer 1 Producer Consumer 2 get: customers/{id} post: customers/ put: customers/{id} put: customers/{id} delete: customers/{id} Consumer 3 get: customers/ {id}/bookings/{id} post: customers/{id}/ bookings/ Contract visualisation:
  • 38. API: get: customers/{id} post: customers/ put: customers/{id} delete: customers/{id} get: customers/{id}/bookings/{id} post: customers/{id}/bookings/ delete: customers/{id}/bookings/{id} Consumer 1 Producer Consumer 2 get: customers/{id} post: customers/ put: customers/{id} put: customers/{id} delete: customers/{id} Consumer 3 get: customers/ {id}/bookings/{id} post: customers/{id}/ bookings/ Overlapping part of 2 contracts Contract visualisation:
  • 39. API: get: customers/{id} post: customers/ put: customers/{id} delete: customers/{id} get: customers/{id}/bookings/{id} post: customers/{id}/bookings/ Consumer 1 Producer Consumer 2 get: customers/{id} post: customers/ put: customers/{id} put: customers/{id} delete: customers/{id} Consumer 3 get: customers/ {id}/bookings/{id} post: customers/{id}/ bookings/ Overlapping part of 2 contracts Completely independent contract delete: customers/{id}/bookings/{id} Contract visualisation:
  • 40. API: get: customers/{id} post: customers/ put: customers/{id} delete: customers/{id} get: customers/{id}/bookings/{id} post: customers/{id}/bookings/ Consumer 1 Producer Consumer 2 get: customers/{id} post: customers/ put: customers/{id} put: customers/{id} delete: customers/{id} Consumer 3 get: customers/ {id}/bookings/{id} post: customers/{id}/ bookings/ Overlapping part of 2 contracts Completely independent contract delete: customers/{id}/bookings/{id} No in use Contract visualisation:
  • 41. Some parts of the APIs might not be used and will not impact anyone. They might either be in a state of active development or about to be decommissioned
  • 42. Demystification These tests are not component tests. They do not test the behaviour of the service deeply but that the inputs and outputs of service calls contain required attributes.
  • 43. Contract: specific scenario rather than schema Their main advantage is that they are fast and reliable.
  • 44. What tools are there for Java devs?
  • 45. There are 2 main options: 1. Spring Cloud Contracts; 2. Pact.
  • 46. Spring Cloud Contracts ● Spring Cloud Contract Verifier - the producer side; ● Spring Cloud Contract Stub Runner - the consumer side
  • 47. Spring Cloud Contracts: Contract Verifier Shipped with Contract Definition Language (DSL) written in Groovy or YAML.
  • 48. Spring Cloud Contract Verifier provides: ● Alignment between stubs and actual implementation; ● Acceptance test driven development; ● Immediate visibility of contracts on both sides; ● Autogenerated boilerplate tests.
  • 49. Spring Cloud Contracts: Contract Stub Runner ● Fetches the stubs of the producer; ● Runs tests against the stubs.
  • 50. Spring Cloud Contracts: Contract Definition Language
  • 51. There’s more if you read into specs: ● Groovy DSL ● YAML Spring Cloud Contracts: Contract Definition Language
  • 52. There’s more if you read into specs: ● Groovy DSL ● YAML ● Pact JSON ● Spring Rest Docs Spring Cloud Contracts: Contract Definition Language
  • 53. Spring Cloud Contracts - catchline “Spring Cloud Contract Verifier moves TDD to the level of software architecture.”
  • 54. SCC guarantees: ● Stubs reliability: generated on successful test execution. ● Stubs reusability: packed in a separate artifact with a stub classifier.
  • 55. Spring Cloud Contract: look inside
  • 56. Server / Producer side utilization: Maven/gradle plugin + verifier
  • 57. Server / Producer side: contract DSL Groovy YAML package contracts import org.springframework...t Contract.make { request { method 'PUT' url '/bookings/4' body(""" { "id": 4, "destination": "USA", …. } response { status 204 request: method: PUT url: /bookings/4 body: id: 4, destination: "USA" response: status: 204
  • 58. Server / Producer side: base class <baseClassForTests>....api.controllers.BookingControllerImplTest</...>
  • 59. Server / Producer side: base class The responsibility to initialising your MockMvc is on you: java.lang.IllegalStateException: You haven't configured a MockMVC instance. You can do this statically RestAssuredMockMvc.mockMvc(..) RestAssuredMockMvc.standaloneSetup(..); RestAssuredMockMvc.webAppContextSetup(..); or using the DSL: given(). mockMvc(..). ..
  • 60. Server / Producer side: producer stubs Spring Cloud Contract Verifier will convert the contracts into an HTTP server stub definitions(Wiremock mapping jsons).
  • 61. Demo time! ● Run Producer tests; ● Spring-cloud-contract-maven-plugin options; ● Run Consumer tests; ● Groovy DSL vs YAML; ● Add stateful behaviour(real MySql instance) for visibility; ● Non-binary storage(git); ● Explain stubrunner yaml property and how it impacts tests; ● Wiremock scenarios; ● produce(), consume() and matchers; ● execute() with custom utility methods; ● Integration with the rest of Spring Cloud.
  • 63. Pact: language support ● Ruby Pact ● JVM Pact and Scala-Pact ● .NET Pact ● JS Pact ● Go Pact (there is also a v1.1 native Pact Go) ● Swift / Objective-C Pact ● Python ● PHP
  • 64. Pact: objective The requirements of the consumer(s) are used to drive the features of the provider.
  • 65. Pact: diagram Consumer ● Specifies the contracts; ● Generates the pact files; ● Push them into Pact Broker
  • 66. Pact: diagram Consumer ● Specifies the contracts; ● Generates the pact files; ● Push them into Pact Broker Pact Broker ● Stores pact contracts; ● Integrates into CI tools.
  • 67. Pact: diagram Consumer ● Specifies the contracts; ● Generates the pact files; ● Push them into Pact Broker Pact Broker ● Stores pact contracts; ● Integrates into CI tools. Producer ● Implement a service according to Consumer specs.
  • 68. Demo time! ● Pact core concepts: State, Broker, Verification; ● Versioning; ● Pact extension for Spring Cloud Contracts;
  • 69. Useful links ● https://github.com/spring-cloud-samples/spring-cloud-contract-samples ● https://github.com/DiUS/pact-workshop-jvm ● https://github.com/DiUS/pact-jvm ● https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-maven
  • 70. Summary Use API Contracts ?: Adopt a design first approach ?: Document your APIs ?: You are hopeless
  • 71. Thank you! Mail: yura.v.nosenko@gmail.com Linkedin: https://www.linkedin.com/in/yuranos/ Github example: https://github.com/yuranos/raml-based-producer-application, https://github.com/yuranos/raml-based-consumer-application (consumer_driven_contracts branches)

Editor's Notes

  1. You can create very robust, scalable microservices with an expectation that it will get you closer to CD and bring true value to Business...
  2. CD Hotfixes. etc. You can create very robust, scalable microservices with an expectation that it will get you closer to CD and bring true value to Business... ...and completely screw it up by not adopting contracts between them, which will get you further from CD than you’ve ever been before.
  3. It has a lot to do with communitation. Runtime attributes(Scalability, Performance, Availability) Design attributes(Reusability, Maintanability, Integrity) If your ultimate goal is to build a distributed, microservice based system.
  4. and collaboration between services and people.
  5. As a result, providers expose a lean contract that is clearly aligned with the business goals that underpin their consumers.
  6. The Consumer-Driven Contract pattern is applicable in a single enterprise or a closed community.
  7. If you delay with API contracts, it will be a lot more challenging to get a buy-in from all development teams to adopt it when you are already running 10s/100s of services.
  8. Let’s say we don’t use any tools and decided to test our microservices together as an end to end flow
  9. Do you see a contraption like this very often in a real life?
  10. Contracts enable service independence; paradoxically, they can also couple service providers and consumers in undesirable ways. That’s why there are frameworks for that!
  11. and they can be quite different and only partially overlap or not overlap at all.
  12. As consumers of services, we need to define what exactly we want to achieve. We need to formulate our expectations. That is why we write contracts. Contract tests assert that the integration between the producer and the consumer meets the requirements defined in the contract.
  13. Spring Cloud Contract Verifier features: Ensure that HTTP / Messaging stubs (for client) are doing exactly what actual server-side implementation will do Promote acceptance test driven development method and Microservices architectural style Provide a way to publish changes in contracts that are immediately visible on both sides of the communication Autogenerated boilerplate test code used on the server side
  14. Very cool feature, because it allows you you use a webenvironment of your choosing.
  15. Wiremock output: logging by default Base classes: About Wiremock scenarios: TestRestTemplate is required. Otherwise 404 issue. About Wiremock scenarios: Content-type: very important to have. Otherwise, stubs will fail when called with RestTemplate. Disable FixedMethodOrder and show the results. https://github.com/spring-cloud-samples/spring-cloud-contract-nodejs https://spring.io/blog/2018/02/13/spring-cloud-contract-in-a-polyglot-world
  16. https://blog.risingstack.com/consumer-driven-contract-testing-with-pact/
  17. Something similar can be attempted by using Spring Cloud: In order to enable working with contracts while creating applications in non-JVM technologies, the springcloud/spring-cloud-contract Docker image has been created. It contains a project that automatically generates tests for HTTP contracts and executes them in EXPLICIT test mode. Then, if the tests pass, it generates Wiremock stubs and, optionally, publishes them to an artifact manager. In order to use the image, you can mount the contracts into the /contracts directory and set a few environment variables.
  18. Something similar can be attempted by using Spring Cloud: In order to enable working with contracts while creating applications in non-JVM technologies, the springcloud/spring-cloud-contract Docker image has been created. It contains a project that automatically generates tests for HTTP contracts and executes them in EXPLICIT test mode. Then, if the tests pass, it generates Wiremock stubs and, optionally, publishes them to an artifact manager. In order to use the image, you can mount the contracts into the /contracts directory and set a few environment variables.
  19. Something similar can be attempted by using Spring Cloud: In order to enable working with contracts while creating applications in non-JVM technologies, the springcloud/spring-cloud-contract Docker image has been created. It contains a project that automatically generates tests for HTTP contracts and executes them in EXPLICIT test mode. Then, if the tests pass, it generates Wiremock stubs and, optionally, publishes them to an artifact manager. In order to use the image, you can mount the contracts into the /contracts directory and set a few environment variables.
  20. Something similar can be attempted by using Spring Cloud: In order to enable working with contracts while creating applications in non-JVM technologies, the springcloud/spring-cloud-contract Docker image has been created. It contains a project that automatically generates tests for HTTP contracts and executes them in EXPLICIT test mode. Then, if the tests pass, it generates Wiremock stubs and, optionally, publishes them to an artifact manager. In order to use the image, you can mount the contracts into the /contracts directory and set a few environment variables.
  21. Something similar can be attempted by using Spring Cloud: In order to enable working with contracts while creating applications in non-JVM technologies, the springcloud/spring-cloud-contract Docker image has been created. It contains a project that automatically generates tests for HTTP contracts and executes them in EXPLICIT test mode. Then, if the tests pass, it generates Wiremock stubs and, optionally, publishes them to an artifact manager. In order to use the image, you can mount the contracts into the /contracts directory and set a few environment variables.
  22. Native Pact Broker only supports real HTTP transport. https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-junit
  23. https://unsplash.com/collections/239831/typography - slogans https://unsplash.com/collections/203782/in-transit - travelling, transportations, on the road https://unsplash.com/collections/261936/technology - mostly phones and laptops https://unsplash.com/collections/881815/au-naturel - desktop wallpapers https://unsplash.com/collections/345744/urban-architecture https://unsplash.com/collections/589374/textures - abstract patterns https://unsplash.com/collections/227/shapes-patterns-textures - abstract patterns https://unsplash.com/collections/416021/abstract - abstract patterns https://unsplash.com/collections/1240111/its-simple-but-very-complex - patterns(nature, architecture) https://unsplash.com/collections/420324/blurrrr - nice pics of different blurred sceneries https://unsplash.com/collections/770996/creative-spaces - office environment https://unsplash.com/collections/385548/work-and-collaboration https://unsplash.com/collections/361687/architectural-lines - architecture, buildings https://unsplash.com/collections/1439704/facade - building facades https://unsplash.com/collections/1136512/%E2%98%85-textures-colors - slogans and sceneries https://unsplash.com/collections/345761/sport - sports