SlideShare a Scribd company logo
Consumer Driven Contracts
A Deep Dive
Ramya Authappan
Lead SDET, Freshworks
Few Things About Myself
www.facebook.com/at.ramya
@atramya
https://www.linkedin.com/in/ramy
aat/
ramya.authappan@freshworks.c
om
http://tiny.cc/ramya_talks
Lead SDET @ Freshworks
Passionate about
building/designing Test
Frameworks/Tools
Believes in building “Simple, yet
Powerful” tools
Loves to play with Sharon
About Freshworks
• SAAS Startup
• Believes in building software that’s ready to go, easy to setup and use, and
requires minimal customization.
• Global Presence:
• Investors:
• Products:
CDC - Consumer Driven Contract!
• Microservices @ Amazon
• Talks to each other via HTTP Requests
• How do I make sure that a simple change does not break
anything in the application? An integration nightmare!
• In such a system, how do I independently AND
confidently make changes?
• CDC to the RESCUE!
CDC - Consumer Driven Contract!
• Consumer-Driven Contracts is a pattern defined by Martin Fowler.
• In Consumer-Driven Contracts, each consumer captures their expectations of the provider in a separate contract
(pact).
• All of these contracts are shared with the provider so they gain insight into the obligations they must fulfil for each
individual client.
• Implementations:
• Pact - https://docs.pact.io/ - This is what we will use in this session.
• Pacto - https://github.com/thoughtworks/pacto
• Janus - https://github.com/gga/janus
• Spring Cloud Contract - https://cloud.spring.io/spring-cloud-contract/ - Found this implementation few days
back. - Java Implementation
CDC - Consumer Driven Contract!
• What is Pact? - An opensource library implementing CDC
• Available as ruby gems (gem: ‘pact’) and as npm packages
(‘pact’)
• Supports: Ruby, Java, .NET
• Beta: JS,Python, Swift, Go
• Its called “Asynchronous” Integration Tests - No extra setup
needed.
• As simple as unit tests but as powerful as E2E tests.
• Pact is run on both sides - On “Consumer” side and on
“Provider” side.
CDC - Consumer Side
Sends
response
for
assertions
Tests
Set
Expectations
Pact Mock Provider
Tests
invoke
code
Source Code
Sends
HTTP
Requests
Sends
expected
response
CDC - Pact File
DevPortal Activities
Request
Response
Consumer Provider
CDC - Pact File!
• Every HTTP Request and Response is
captured
• Standard way of representing interactions -
JSON file
• This is shared with the Provider.
• Explains everything that a consumer expects
from the Provider - the endpoints, query
params, header and the response objects.
CDC - Provider Side
Activities
Replay each HTTP Request
Get real HTTP Response
Provider
Pact File Compares real and
mock responses
CDC - Provider States
• Get activities of user with id=777
DevPortal Activities
Request
Response
Consumer Provider
Request
Response
?
CDC - Provider Side
state: cust_id=777 @ DB
• Get activities of user with id=777
DevPortal Activities
Request
Response
Consumer
Provider
Request
Response
state: cust_id=777 @ DB
state: cust_id=777 @ DB
CDC - Sharing Pact Files
Filesystem Cloud Pact BrokerCI Build
Artefact
CDC - Advantages!
• Eliminates Wrong Assumptions
• Enables communication
• Very less setup time!
• No extra infrastructure
• Need not start the dependent services to test!
• Fast in Execution
• Fails Fast - Identifies integration issues even before you actually
integrate!
• No flakiness!
• Easy to debug
Demo
Consumer Side : Setup Mock Server
Pact.service_consumer 'DevPortal' do
end
Consumer Side : Setup Mock Server
Pact.service_consumer 'DevPortal' do
has_pact_with "Freshapps Activities" do
end
end
Consumer Side : Setup Mock Server
Pact.service_consumer 'DevPortal' do
has_pact_with "Freshapps Activities" do
mock_service :freshapps_activities do
port 3005
end
end
end
Consumer Side : Setup Expectations
freshapps_activities.given(“all activities without role param")
Consumer Side : Setup Expectations
freshapps_activities.given(“all activities without role param")
.upon_receiving("a request for all activities")
.with(
method: :get,
path: '/all/activities.json',
query: {
page: '1',
account_id: '1' },
headers: {'Content-Type' => 'application/json', 'Accept' => 'application/json'} )
Consumer Side : Setup Expectations
freshapps_activities.given(“all activities without role param")
.upon_receiving("a request for all activities")
.with(
method: :get,
path: '/all/activities.json',
query: {
page: '1',
account_id: '1' },
headers: {'Content-Type' => 'application/json', 'Accept' => 'application/json'} )
.will_respond_with(
status: 422,
headers: {
'Content-Type' => 'application/json; charset=utf-8' },
body: { "error_msg" => "Required parameter missing" } )
Consumer Side : Make Request &
Assert
resp = FreshappsActivitiesClient.get_activities(path, query, header)
Consumer Side : Make Request &
Assert
resp = FreshappsActivitiesClient.get_activities(path, query, header)
expected_response = { "error_msg" => "Required parameter missing" }
expect(resp.parsed_response.inspect).to eq(expected_response.inspect)
Generated Pact File
Provider Side : Make Request & Assert
Pact.service_provider 'Freshapps Activities' do
end
Provider Side : Make Request & Assert
Pact.service_provider 'Freshapps Activities' do
honours_pact_with 'DevPortal' do
end
end
Provider Side : Make Request & Assert
Pact.service_provider 'Freshapps Activities' do
honours_pact_with 'DevPortal' do
pact_uri "../freshapps_devportal/spec/pacts/devportal-freshapps_activities.json"
end
end
end
Provider Side : Setup Test Data
Pact.provider_states_for "DevPortal" do
provider_state "all activities without role param" do
end
end
Remember:
Consumer Assumed:
freshapps_activities.given(“
all activities without role
param")
Provider Side : Setup Test Data
Remember:
Consumer Assumed:
freshapps_activities.given(“
all activities without role
param")
Pact.provider_states_for "DevPortal" do
provider_state "all activities without role param" do
set_up do
factory :activity do
account_id 1
user_id 1
extension_id 2
version_id 2
activity_type 'Extension'
visibility 1
activity { 'extension_id' => '2', 'name' => 'Sample Extension’}
end
end
end
end
Provider Side : Pact Verify Failure
Provider Side : Pact Verify Success
•My Upcoming Talks:
• WomenWhoCode August Meetup at Freshworks, Aug 19th - on “Docker and
CI”
Questions?
Thank you!

More Related Content

PDF
CDC Tests - Integration Tests cant be made simpler than this!
PPTX
GraphQL, Redux, and React
PPT
Using RESTFUL APIs in ANGULARJS
PDF
Deploy with Confidence using Pact Go!
PDF
The case for consumer-driven contracts
PDF
J2EE Difference
PPTX
Server Side Javascript : how to start ?
PDF
Performance optimisation with GraphQL
CDC Tests - Integration Tests cant be made simpler than this!
GraphQL, Redux, and React
Using RESTFUL APIs in ANGULARJS
Deploy with Confidence using Pact Go!
The case for consumer-driven contracts
J2EE Difference
Server Side Javascript : how to start ?
Performance optimisation with GraphQL

What's hot (18)

PPSX
React introduction
PPTX
I Know It Was MEAN, But I Cut the Cord to LAMP Anyway
PDF
React && React Native workshop
PDF
JOSA TechTalks - Better Web Apps with React and Redux
ODP
Consume Spring Data Rest with Angularjs
PDF
React js
PDF
Building Modern Web Applications using React and Redux
PDF
London React August - GraphQL at The Financial Times - Viktor Charypar
PPTX
Introduction to ReactJs & fundamentals
PPTX
A Brief Introduction to React.js
PDF
From ActiveRecord to EventSourcing
PPTX
I Know It Was MEAN, But I Cut the Cord to LAMP Anyway
PPTX
SignalR tutorial & best practices
PPTX
Entity framework db model的驗證機制 20130914
PPTX
PDF
Jumpstarting Testing In Your Organization with Selenium, Cucumber, & WebdriverIO
PDF
20150309 seven core principles of meteor
React introduction
I Know It Was MEAN, But I Cut the Cord to LAMP Anyway
React && React Native workshop
JOSA TechTalks - Better Web Apps with React and Redux
Consume Spring Data Rest with Angularjs
React js
Building Modern Web Applications using React and Redux
London React August - GraphQL at The Financial Times - Viktor Charypar
Introduction to ReactJs & fundamentals
A Brief Introduction to React.js
From ActiveRecord to EventSourcing
I Know It Was MEAN, But I Cut the Cord to LAMP Anyway
SignalR tutorial & best practices
Entity framework db model的驗證機制 20130914
Jumpstarting Testing In Your Organization with Selenium, Cucumber, & WebdriverIO
20150309 seven core principles of meteor
Ad

Similar to Consumer Driven Contracts - A Deep Dive (20)

PPTX
"Asynchronous" Integration Tests for Microservices - RootConf 2017
PPTX
ATAGTR2017 CDC Tests - Integration Tests cant be made simpler than this!
PDF
MongoDB.local Berlin: App development in a Serverless World
PPTX
Building Your First App with MongoDB
PDF
Cqrs api v2
PPTX
Service workers and their role in PWAs
PPTX
REST API 20.2 - Appworks Gateway Integration.pptx
PDF
InterConnect 2016 Java EE 7 Overview (PEJ-5296)
PPTX
Engineering Velocity @indeed eng presented on Sept 24 2014 at Beyond Agile
PDF
Creating Rich Server API’s for your Mobile Apps - Best Practices and Guidelines
PDF
Uncovering breaking changes behind UI on mobile applications
PPTX
Timings API: Performance Assertion during the functional testing
PPTX
REST API Best Practices & Implementing in Codeigniter
PDF
Pinterest like site using REST and Bottle
PPTX
Bdd with Cucumber and Mocha
PPTX
Consumer-driven contracts with Pact and PHP
PPT
APITalkMeetupSharable
PPT
Performance testing jmeter
PDF
Cqrs api
"Asynchronous" Integration Tests for Microservices - RootConf 2017
ATAGTR2017 CDC Tests - Integration Tests cant be made simpler than this!
MongoDB.local Berlin: App development in a Serverless World
Building Your First App with MongoDB
Cqrs api v2
Service workers and their role in PWAs
REST API 20.2 - Appworks Gateway Integration.pptx
InterConnect 2016 Java EE 7 Overview (PEJ-5296)
Engineering Velocity @indeed eng presented on Sept 24 2014 at Beyond Agile
Creating Rich Server API’s for your Mobile Apps - Best Practices and Guidelines
Uncovering breaking changes behind UI on mobile applications
Timings API: Performance Assertion during the functional testing
REST API Best Practices & Implementing in Codeigniter
Pinterest like site using REST and Bottle
Bdd with Cucumber and Mocha
Consumer-driven contracts with Pact and PHP
APITalkMeetupSharable
Performance testing jmeter
Cqrs api
Ad

Recently uploaded (20)

PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
additive manufacturing of ss316l using mig welding
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Sustainable Sites - Green Building Construction
PPTX
CH1 Production IntroductoryConcepts.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Geodesy 1.pptx...............................................
PPT
Project quality management in manufacturing
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPT
introduction to datamining and warehousing
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Current and future trends in Computer Vision.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
Operating System & Kernel Study Guide-1 - converted.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
bas. eng. economics group 4 presentation 1.pptx
Digital Logic Computer Design lecture notes
additive manufacturing of ss316l using mig welding
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Sustainable Sites - Green Building Construction
CH1 Production IntroductoryConcepts.pptx
573137875-Attendance-Management-System-original
CYBER-CRIMES AND SECURITY A guide to understanding
Geodesy 1.pptx...............................................
Project quality management in manufacturing
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
introduction to datamining and warehousing
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Current and future trends in Computer Vision.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
Internet of Things (IOT) - A guide to understanding

Consumer Driven Contracts - A Deep Dive

  • 1. Consumer Driven Contracts A Deep Dive Ramya Authappan Lead SDET, Freshworks
  • 2. Few Things About Myself www.facebook.com/at.ramya @atramya https://www.linkedin.com/in/ramy aat/ ramya.authappan@freshworks.c om http://tiny.cc/ramya_talks Lead SDET @ Freshworks Passionate about building/designing Test Frameworks/Tools Believes in building “Simple, yet Powerful” tools Loves to play with Sharon
  • 3. About Freshworks • SAAS Startup • Believes in building software that’s ready to go, easy to setup and use, and requires minimal customization. • Global Presence: • Investors: • Products:
  • 4. CDC - Consumer Driven Contract! • Microservices @ Amazon • Talks to each other via HTTP Requests • How do I make sure that a simple change does not break anything in the application? An integration nightmare! • In such a system, how do I independently AND confidently make changes? • CDC to the RESCUE!
  • 5. CDC - Consumer Driven Contract! • Consumer-Driven Contracts is a pattern defined by Martin Fowler. • In Consumer-Driven Contracts, each consumer captures their expectations of the provider in a separate contract (pact). • All of these contracts are shared with the provider so they gain insight into the obligations they must fulfil for each individual client. • Implementations: • Pact - https://docs.pact.io/ - This is what we will use in this session. • Pacto - https://github.com/thoughtworks/pacto • Janus - https://github.com/gga/janus • Spring Cloud Contract - https://cloud.spring.io/spring-cloud-contract/ - Found this implementation few days back. - Java Implementation
  • 6. CDC - Consumer Driven Contract! • What is Pact? - An opensource library implementing CDC • Available as ruby gems (gem: ‘pact’) and as npm packages (‘pact’) • Supports: Ruby, Java, .NET • Beta: JS,Python, Swift, Go • Its called “Asynchronous” Integration Tests - No extra setup needed. • As simple as unit tests but as powerful as E2E tests. • Pact is run on both sides - On “Consumer” side and on “Provider” side.
  • 7. CDC - Consumer Side Sends response for assertions Tests Set Expectations Pact Mock Provider Tests invoke code Source Code Sends HTTP Requests Sends expected response
  • 8. CDC - Pact File DevPortal Activities Request Response Consumer Provider
  • 9. CDC - Pact File! • Every HTTP Request and Response is captured • Standard way of representing interactions - JSON file • This is shared with the Provider. • Explains everything that a consumer expects from the Provider - the endpoints, query params, header and the response objects.
  • 10. CDC - Provider Side Activities Replay each HTTP Request Get real HTTP Response Provider Pact File Compares real and mock responses
  • 11. CDC - Provider States • Get activities of user with id=777 DevPortal Activities Request Response Consumer Provider Request Response ?
  • 12. CDC - Provider Side state: cust_id=777 @ DB • Get activities of user with id=777 DevPortal Activities Request Response Consumer Provider Request Response state: cust_id=777 @ DB state: cust_id=777 @ DB
  • 13. CDC - Sharing Pact Files Filesystem Cloud Pact BrokerCI Build Artefact
  • 14. CDC - Advantages! • Eliminates Wrong Assumptions • Enables communication • Very less setup time! • No extra infrastructure • Need not start the dependent services to test! • Fast in Execution • Fails Fast - Identifies integration issues even before you actually integrate! • No flakiness! • Easy to debug
  • 15. Demo
  • 16. Consumer Side : Setup Mock Server Pact.service_consumer 'DevPortal' do end
  • 17. Consumer Side : Setup Mock Server Pact.service_consumer 'DevPortal' do has_pact_with "Freshapps Activities" do end end
  • 18. Consumer Side : Setup Mock Server Pact.service_consumer 'DevPortal' do has_pact_with "Freshapps Activities" do mock_service :freshapps_activities do port 3005 end end end
  • 19. Consumer Side : Setup Expectations freshapps_activities.given(“all activities without role param")
  • 20. Consumer Side : Setup Expectations freshapps_activities.given(“all activities without role param") .upon_receiving("a request for all activities") .with( method: :get, path: '/all/activities.json', query: { page: '1', account_id: '1' }, headers: {'Content-Type' => 'application/json', 'Accept' => 'application/json'} )
  • 21. Consumer Side : Setup Expectations freshapps_activities.given(“all activities without role param") .upon_receiving("a request for all activities") .with( method: :get, path: '/all/activities.json', query: { page: '1', account_id: '1' }, headers: {'Content-Type' => 'application/json', 'Accept' => 'application/json'} ) .will_respond_with( status: 422, headers: { 'Content-Type' => 'application/json; charset=utf-8' }, body: { "error_msg" => "Required parameter missing" } )
  • 22. Consumer Side : Make Request & Assert resp = FreshappsActivitiesClient.get_activities(path, query, header)
  • 23. Consumer Side : Make Request & Assert resp = FreshappsActivitiesClient.get_activities(path, query, header) expected_response = { "error_msg" => "Required parameter missing" } expect(resp.parsed_response.inspect).to eq(expected_response.inspect)
  • 25. Provider Side : Make Request & Assert Pact.service_provider 'Freshapps Activities' do end
  • 26. Provider Side : Make Request & Assert Pact.service_provider 'Freshapps Activities' do honours_pact_with 'DevPortal' do end end
  • 27. Provider Side : Make Request & Assert Pact.service_provider 'Freshapps Activities' do honours_pact_with 'DevPortal' do pact_uri "../freshapps_devportal/spec/pacts/devportal-freshapps_activities.json" end end end
  • 28. Provider Side : Setup Test Data Pact.provider_states_for "DevPortal" do provider_state "all activities without role param" do end end Remember: Consumer Assumed: freshapps_activities.given(“ all activities without role param")
  • 29. Provider Side : Setup Test Data Remember: Consumer Assumed: freshapps_activities.given(“ all activities without role param") Pact.provider_states_for "DevPortal" do provider_state "all activities without role param" do set_up do factory :activity do account_id 1 user_id 1 extension_id 2 version_id 2 activity_type 'Extension' visibility 1 activity { 'extension_id' => '2', 'name' => 'Sample Extension’} end end end end
  • 30. Provider Side : Pact Verify Failure
  • 31. Provider Side : Pact Verify Success
  • 32. •My Upcoming Talks: • WomenWhoCode August Meetup at Freshworks, Aug 19th - on “Docker and CI”