SlideShare una empresa de Scribd logo
1 de 42
Descargar para leer sin conexión
Stick to the rules!
Consumer Driven Contracts
04.07.2015 - Confitura PL
Marcin Grzejszczak @mgrzejszczak
@confiturapl @mgrzejszczak / @4financeit
Swim for a dream
http://swimforadream.com/
Adam will swim 18.5 km from Hel to Gdynia
Money for 3 charity organizations
@confiturapl @mgrzejszczak / @4financeit
Marcin Grzejszczak
● Software Architect at 4financeIT
● Author of "Mockito Instant", "Mockito Cookbook"
● Co-author of "micro-infra-spring",
“spring-cloud-zookeeper”, “spring-cloud-sleuth”
Twitter: @MGrzejszczak
Blog: http://toomuchcoding.blogspot.com
Homepage: http://marcin.grzejszczak.pl
@confiturapl @mgrzejszczak / @4financeit
Agenda
● Short introduction
● Real life scenario with a hilarious joke
● How does it work?
● Live (hopefully) coding
● Summary
@confiturapl @mgrzejszczak / @4financeit
Consumer Driven Contracts
● TDD on architectural level
○ mistakes occur both in implementation and design
● Try to use API before implementing it
@confiturapl @mgrzejszczak / @4financeit
Real life scenario...
@confiturapl @mgrzejszczak / @4financeit
Real life scenario...
Hi! I’m team
X and we
have spent
weeks to
tweak our
code to use
your API!
@confiturapl @mgrzejszczak / @4financeit
Real life scenario...
That’s so
sweet… But
we’ve just
changed our
API because
well, it’s ours.
@confiturapl @mgrzejszczak / @4financeit
Real life scenario...
You’ve got to
be shitting
me! (however
it sounds)
@confiturapl @mgrzejszczak / @4financeit
Real life scenario...
@confiturapl @mgrzejszczak / @4financeit
CDC building blocks
● Server
● Consumer
● Contract
@confiturapl @mgrzejszczak / @4financeit
CDC building blocks
@confiturapl @mgrzejszczak / @4financeit
How to make life easier?
Use common contract
@confiturapl @mgrzejszczak / @4financeit
Contract definition
@confiturapl @mgrzejszczak / @4financeit
What is a contract?
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method "PUT"
url "/fraudcheck"
body(''' { “clientPesel":"12345678901", "loanAmount":123.123 } ''' )
headers { header("Content-Type", "application/fraud+json") }
}
response {
status 200
body( """{ "fraudCheckStatus": "OK"}""")
headers { header('Content-Type': 'application/fraud+json') }
}
}
@confiturapl @mgrzejszczak / @4financeit
What is a contract?
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method "PUT"
url "/fraudcheck"
body(''' { “clientPesel":"12345678901", "loanAmount":123.123 } ''' )
headers { header("Content-Type", "application/fraud+json") }
}
response {
status 200
body( """{ "fraudCheckStatus": "OK"}""")
headers { header('Content-Type': 'application/fraud+json') }
}
}
Groovy DSL
@confiturapl @mgrzejszczak / @4financeit
What is a contract?
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method "PUT"
url "/fraudcheck"
body(''' { “clientPesel":"12345678901", "loanAmount":123.123 } ''' )
headers { header("Content-Type", "application/fraud+json") }
}
response {
status 200
body( """{ "fraudCheckStatus": "OK"}""")
headers { header('Content-Type': 'application/fraud+json') }
}
}
FOR A GIVEN
REQUEST
@confiturapl @mgrzejszczak / @4financeit
What is a contract?
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method "PUT"
url "/fraudcheck"
body(''' { “clientPesel":"12345678901", "loanAmount":123.123 } ''' )
headers { header("Content-Type", "application/fraud+json") }
}
response {
status 200
body( """{ "fraudCheckStatus": "OK"}""")
headers { header('Content-Type': 'application/fraud+json') }
}
}
WE’LL HAVE SUCH A
RESPONSE
@confiturapl @mgrzejszczak / @4financeit
So how am I supposed to work?
@confiturapl @mgrzejszczak / @4financeit
Workflow - Consumer side
CLONE SERVER REPO
AND WORK OFFLINE
@confiturapl @mgrzejszczak / @4financeit
Workflow - Consumer side
1) ALTER THE CONTRACT LOCALLY
2) GENERATE AND COPY STUBS
3) USE STUBS IN CONSUMER TESTS
@confiturapl @mgrzejszczak / @4financeit
What is a stub?
● JSON representation of a Groovy DSL contract
● Understood by Wiremock Http Server Stub
@confiturapl @mgrzejszczak / @4financeit
Workflow - Consumer side
FILE A PULL REQUEST WITH
THE PROPOSED CONTRACT
@confiturapl @mgrzejszczak / @4financeit
Server tests - what happens there?
● server has DSLs with contracts
● tests are generated from the contracts
● stubs that consumers can use are
generated from the contracts
@confiturapl @mgrzejszczak / @4financeit
Workflow - Server side
● oh, I have a PR with the proposed contract
● I don’t have the implementation ready so
my generated tests are failing
● I’ll add the implementation, fix the tests
and merge the PR!
@confiturapl @mgrzejszczak / @4financeit
Workflow - Server side
IMPLEMENTATION IS READY
I’LL PUBLISH MY STUB!
@confiturapl @mgrzejszczak / @4financeit
Consumer Technology
Wiremock
http://wiremock.org
testCompile 'com.github.tomakehurst:wiremock:1.56'
{
"request": {
"method": "GET",
"url": "/hello"
},
"response": {
"status": 200,
"body": "Hello world!",
"headers": { "Content-Type": "text/plain" }
}
}
Configuration
Sample definition
@confiturapl @mgrzejszczak / @4financeit
Server Technology
Accurate REST
https://github.com/Codearte/accurest
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'io.codearte.accurest:accurest-gradle-plugin:0.7.0'
}
}
apply plugin: 'accurest'
@confiturapl @mgrzejszczak / @4financeit
Example
@confiturapl @mgrzejszczak / @4financeit
New feature
@confiturapl @mgrzejszczak / @4financeit
Let consumer drive the contract!
@confiturapl @mgrzejszczak / @4financeit
Live coding - consumer side
@confiturapl @mgrzejszczak / @4financeit
Consumer prepared the contract
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method "PUT"
url "/fraudcheck"
body(''' { “clientPesel":"1234567890", "loanAmount":99999 } ''' )
headers { header("Content-Type", "application/vnd.fraud.v1+json") }
}
response {
status 200
body( """{ "fraudCheckStatus": "REJECTED", "rejectionReason": "Amount too high" }""")
headers { header('Content-Type': 'application/vnd.fraud.v1+json') }
}
}
@confiturapl @mgrzejszczak / @4financeit
Time for server side implementation!
@confiturapl @mgrzejszczak / @4financeit
Live coding - server side
@confiturapl @mgrzejszczak / @4financeit
CDC benefits
@confiturapl @mgrzejszczak / @4financeit
CDC benefits
● Rapid API prototyping
● Non-blocking development
● Behaviour Driven Development
@confiturapl @mgrzejszczak / @4financeit
CDC benefits
● Client tells what he needs
● Quality - continuously checking if contract
is valid
@confiturapl @mgrzejszczak / @4financeit
CDC benefits
● if server breaks API compatibility
consumer tests fail
● if server adds new API I can reuse it
immediately in my tests
@confiturapl @mgrzejszczak / @4financeit
CDC benefits
● The server tests are generated for you
● Any typos / misunderstandings will be found
out immediately
@confiturapl @mgrzejszczak / @4financeit
Not so fast...
● Maintaining datasets
● What exactly to verify for server side?
● Breaking changes
● Accurest under development
○ I also have to sleep sometimes
@confiturapl @mgrzejszczak / @4financeit
Q&A
https://github.com/marcingrzejszczak/2015_confitura_cdc
Accurest: https://github.com/Codearte/accurest
Micro-Infra-Spring: https://github.com/4finance/micro-infra-spring
CDC: http://martinfowler.com/articles/consumerDrivenContracts.html

Más contenido relacionado

Similar a Stick to the rules - Consumer Driven Contracts. 2015.07 Confitura

CSU33012-I-microservices.pdf
CSU33012-I-microservices.pdfCSU33012-I-microservices.pdf
CSU33012-I-microservices.pdfRicky Garg
 
Semantic Web & TYPO3
Semantic Web & TYPO3Semantic Web & TYPO3
Semantic Web & TYPO3André Wuttig
 
Introduction to the IBM Watson Data Platform
Introduction to the IBM Watson Data PlatformIntroduction to the IBM Watson Data Platform
Introduction to the IBM Watson Data PlatformMargriet Groenendijk
 
BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
BDD to the Bone: Using Behave and Selenium to Test-Drive Web ApplicationsBDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
BDD to the Bone: Using Behave and Selenium to Test-Drive Web ApplicationsPatrick Viafore
 
REST with Eve and Python
REST with Eve and PythonREST with Eve and Python
REST with Eve and PythonPiXeL16
 
Web Components for Microservices and IoT
Web Components for Microservices and IoTWeb Components for Microservices and IoT
Web Components for Microservices and IoTChris Lorenzo
 
Expanding APIs beyond the Web
Expanding APIs beyond the WebExpanding APIs beyond the Web
Expanding APIs beyond the WebTim Messerschmidt
 
Client-side JavaScript Vulnerabilities
Client-side JavaScript VulnerabilitiesClient-side JavaScript Vulnerabilities
Client-side JavaScript VulnerabilitiesOry Segal
 
Ed presents JSF 2.2 and WebSocket to Gameduell.
Ed presents JSF 2.2 and WebSocket to Gameduell.Ed presents JSF 2.2 and WebSocket to Gameduell.
Ed presents JSF 2.2 and WebSocket to Gameduell.Edward Burns
 
Composing re-useable ETL on Hadoop
Composing re-useable ETL on HadoopComposing re-useable ETL on Hadoop
Composing re-useable ETL on HadoopPaul Lam
 
Compositional UIs With Hosted Views and Hypermedia
Compositional UIs With Hosted Views and HypermediaCompositional UIs With Hosted Views and Hypermedia
Compositional UIs With Hosted Views and HypermediaNordic APIs
 
Strigil - lightning talks
Strigil - lightning talksStrigil - lightning talks
Strigil - lightning talkszviri
 
H2O 3 REST API Overview
H2O 3 REST API OverviewH2O 3 REST API Overview
H2O 3 REST API OverviewRaymond Peck
 
H2O 3 REST API Overview
H2O 3 REST API OverviewH2O 3 REST API Overview
H2O 3 REST API OverviewSri Ambati
 
Perchè potresti aver bisogno di un database NoSQL anche se non sei Google o F...
Perchè potresti aver bisogno di un database NoSQL anche se non sei Google o F...Perchè potresti aver bisogno di un database NoSQL anche se non sei Google o F...
Perchè potresti aver bisogno di un database NoSQL anche se non sei Google o F...Codemotion
 
Nuts and Bolts of WebSocket Devoxx 2014
Nuts and Bolts of WebSocket Devoxx 2014Nuts and Bolts of WebSocket Devoxx 2014
Nuts and Bolts of WebSocket Devoxx 2014Arun Gupta
 
Introduction to back-end
Introduction to back-endIntroduction to back-end
Introduction to back-endMosaab Ehab
 
how to use openstack api
how to use openstack apihow to use openstack api
how to use openstack apiLiang Bo
 
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008IE 8 et les standards du Web - Chris Wilson - Paris Web 2008
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008Association Paris-Web
 

Similar a Stick to the rules - Consumer Driven Contracts. 2015.07 Confitura (20)

CSU33012-I-microservices.pdf
CSU33012-I-microservices.pdfCSU33012-I-microservices.pdf
CSU33012-I-microservices.pdf
 
Semantic Web & TYPO3
Semantic Web & TYPO3Semantic Web & TYPO3
Semantic Web & TYPO3
 
Introduction to the IBM Watson Data Platform
Introduction to the IBM Watson Data PlatformIntroduction to the IBM Watson Data Platform
Introduction to the IBM Watson Data Platform
 
BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
BDD to the Bone: Using Behave and Selenium to Test-Drive Web ApplicationsBDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
BDD to the Bone: Using Behave and Selenium to Test-Drive Web Applications
 
CouchDB Day NYC 2017: MapReduce Views
CouchDB Day NYC 2017: MapReduce ViewsCouchDB Day NYC 2017: MapReduce Views
CouchDB Day NYC 2017: MapReduce Views
 
REST with Eve and Python
REST with Eve and PythonREST with Eve and Python
REST with Eve and Python
 
Web Components for Microservices and IoT
Web Components for Microservices and IoTWeb Components for Microservices and IoT
Web Components for Microservices and IoT
 
Expanding APIs beyond the Web
Expanding APIs beyond the WebExpanding APIs beyond the Web
Expanding APIs beyond the Web
 
Client-side JavaScript Vulnerabilities
Client-side JavaScript VulnerabilitiesClient-side JavaScript Vulnerabilities
Client-side JavaScript Vulnerabilities
 
Ed presents JSF 2.2 and WebSocket to Gameduell.
Ed presents JSF 2.2 and WebSocket to Gameduell.Ed presents JSF 2.2 and WebSocket to Gameduell.
Ed presents JSF 2.2 and WebSocket to Gameduell.
 
Composing re-useable ETL on Hadoop
Composing re-useable ETL on HadoopComposing re-useable ETL on Hadoop
Composing re-useable ETL on Hadoop
 
Compositional UIs With Hosted Views and Hypermedia
Compositional UIs With Hosted Views and HypermediaCompositional UIs With Hosted Views and Hypermedia
Compositional UIs With Hosted Views and Hypermedia
 
Strigil - lightning talks
Strigil - lightning talksStrigil - lightning talks
Strigil - lightning talks
 
H2O 3 REST API Overview
H2O 3 REST API OverviewH2O 3 REST API Overview
H2O 3 REST API Overview
 
H2O 3 REST API Overview
H2O 3 REST API OverviewH2O 3 REST API Overview
H2O 3 REST API Overview
 
Perchè potresti aver bisogno di un database NoSQL anche se non sei Google o F...
Perchè potresti aver bisogno di un database NoSQL anche se non sei Google o F...Perchè potresti aver bisogno di un database NoSQL anche se non sei Google o F...
Perchè potresti aver bisogno di un database NoSQL anche se non sei Google o F...
 
Nuts and Bolts of WebSocket Devoxx 2014
Nuts and Bolts of WebSocket Devoxx 2014Nuts and Bolts of WebSocket Devoxx 2014
Nuts and Bolts of WebSocket Devoxx 2014
 
Introduction to back-end
Introduction to back-endIntroduction to back-end
Introduction to back-end
 
how to use openstack api
how to use openstack apihow to use openstack api
how to use openstack api
 
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008IE 8 et les standards du Web - Chris Wilson - Paris Web 2008
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008
 

Más de Marcin Grzejszczak

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
 
Continuous Deployment of your Application @jSession#5
Continuous Deployment of your Application @jSession#5Continuous Deployment of your Application @jSession#5
Continuous Deployment of your Application @jSession#5Marcin Grzejszczak
 
Continuous Deployment of your Application @JUGtoberfest
Continuous Deployment of your Application @JUGtoberfestContinuous Deployment of your Application @JUGtoberfest
Continuous Deployment of your Application @JUGtoberfestMarcin Grzejszczak
 
Continuous Deployment To The Cloud @DevoxxPL 2017
Continuous Deployment To The Cloud @DevoxxPL 2017 Continuous Deployment To The Cloud @DevoxxPL 2017
Continuous Deployment To The Cloud @DevoxxPL 2017 Marcin Grzejszczak
 
Continuous Deployment To The Cloud
Continuous Deployment To The CloudContinuous Deployment To The Cloud
Continuous Deployment To The CloudMarcin Grzejszczak
 
Consumer Driven Contracts To Enable API Evolution @Geecon
Consumer Driven Contracts To Enable API Evolution @GeeconConsumer Driven Contracts To Enable API Evolution @Geecon
Consumer Driven Contracts To Enable API Evolution @GeeconMarcin Grzejszczak
 
Continuous Deployment To The Cloud With Spring Cloud Pipelines @WarsawCloudNa...
Continuous Deployment To The Cloud With Spring Cloud Pipelines @WarsawCloudNa...Continuous Deployment To The Cloud With Spring Cloud Pipelines @WarsawCloudNa...
Continuous Deployment To The Cloud With Spring Cloud Pipelines @WarsawCloudNa...Marcin Grzejszczak
 
Microservices Tracing With Spring Cloud and Zipkin @Szczecin JUG
Microservices Tracing With Spring Cloud and Zipkin @Szczecin JUGMicroservices Tracing With Spring Cloud and Zipkin @Szczecin JUG
Microservices Tracing With Spring Cloud and Zipkin @Szczecin JUGMarcin Grzejszczak
 
Consumer Driven Contracts and Your Microservice Architecture @ Warsaw JUG
Consumer Driven Contracts and Your Microservice Architecture @ Warsaw JUGConsumer Driven Contracts and Your Microservice Architecture @ Warsaw JUG
Consumer Driven Contracts and Your Microservice Architecture @ Warsaw JUGMarcin Grzejszczak
 
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
 
Spring Cloud Contract And Your Microservice Architecture
Spring Cloud Contract And Your Microservice ArchitectureSpring Cloud Contract And Your Microservice Architecture
Spring Cloud Contract And Your Microservice ArchitectureMarcin Grzejszczak
 
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
 
Microservices - enough with theory, let's do some code @Geecon Prague 2015
Microservices - enough with theory, let's do some code @Geecon Prague 2015Microservices - enough with theory, let's do some code @Geecon Prague 2015
Microservices - enough with theory, let's do some code @Geecon Prague 2015Marcin Grzejszczak
 
Do you think you're doing microservice architecture? What about infrastructur...
Do you think you're doing microservice architecture? What about infrastructur...Do you think you're doing microservice architecture? What about infrastructur...
Do you think you're doing microservice architecture? What about infrastructur...Marcin Grzejszczak
 
Introduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transformsIntroduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transformsMarcin Grzejszczak
 

Más de Marcin Grzejszczak (15)

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
 
Continuous Deployment of your Application @jSession#5
Continuous Deployment of your Application @jSession#5Continuous Deployment of your Application @jSession#5
Continuous Deployment of your Application @jSession#5
 
Continuous Deployment of your Application @JUGtoberfest
Continuous Deployment of your Application @JUGtoberfestContinuous Deployment of your Application @JUGtoberfest
Continuous Deployment of your Application @JUGtoberfest
 
Continuous Deployment To The Cloud @DevoxxPL 2017
Continuous Deployment To The Cloud @DevoxxPL 2017 Continuous Deployment To The Cloud @DevoxxPL 2017
Continuous Deployment To The Cloud @DevoxxPL 2017
 
Continuous Deployment To The Cloud
Continuous Deployment To The CloudContinuous Deployment To The Cloud
Continuous Deployment To The Cloud
 
Consumer Driven Contracts To Enable API Evolution @Geecon
Consumer Driven Contracts To Enable API Evolution @GeeconConsumer Driven Contracts To Enable API Evolution @Geecon
Consumer Driven Contracts To Enable API Evolution @Geecon
 
Continuous Deployment To The Cloud With Spring Cloud Pipelines @WarsawCloudNa...
Continuous Deployment To The Cloud With Spring Cloud Pipelines @WarsawCloudNa...Continuous Deployment To The Cloud With Spring Cloud Pipelines @WarsawCloudNa...
Continuous Deployment To The Cloud With Spring Cloud Pipelines @WarsawCloudNa...
 
Microservices Tracing With Spring Cloud and Zipkin @Szczecin JUG
Microservices Tracing With Spring Cloud and Zipkin @Szczecin JUGMicroservices Tracing With Spring Cloud and Zipkin @Szczecin JUG
Microservices Tracing With Spring Cloud and Zipkin @Szczecin JUG
 
Consumer Driven Contracts and Your Microservice Architecture @ Warsaw JUG
Consumer Driven Contracts and Your Microservice Architecture @ Warsaw JUGConsumer Driven Contracts and Your Microservice Architecture @ Warsaw JUG
Consumer Driven Contracts and Your Microservice Architecture @ Warsaw JUG
 
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
 
Spring Cloud Contract And Your Microservice Architecture
Spring Cloud Contract And Your Microservice ArchitectureSpring Cloud Contract And Your Microservice Architecture
Spring Cloud Contract And Your Microservice Architecture
 
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
 
Microservices - enough with theory, let's do some code @Geecon Prague 2015
Microservices - enough with theory, let's do some code @Geecon Prague 2015Microservices - enough with theory, let's do some code @Geecon Prague 2015
Microservices - enough with theory, let's do some code @Geecon Prague 2015
 
Do you think you're doing microservice architecture? What about infrastructur...
Do you think you're doing microservice architecture? What about infrastructur...Do you think you're doing microservice architecture? What about infrastructur...
Do you think you're doing microservice architecture? What about infrastructur...
 
Introduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transformsIntroduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transforms
 

Stick to the rules - Consumer Driven Contracts. 2015.07 Confitura

  • 1. Stick to the rules! Consumer Driven Contracts 04.07.2015 - Confitura PL Marcin Grzejszczak @mgrzejszczak
  • 2. @confiturapl @mgrzejszczak / @4financeit Swim for a dream http://swimforadream.com/ Adam will swim 18.5 km from Hel to Gdynia Money for 3 charity organizations
  • 3. @confiturapl @mgrzejszczak / @4financeit Marcin Grzejszczak ● Software Architect at 4financeIT ● Author of "Mockito Instant", "Mockito Cookbook" ● Co-author of "micro-infra-spring", “spring-cloud-zookeeper”, “spring-cloud-sleuth” Twitter: @MGrzejszczak Blog: http://toomuchcoding.blogspot.com Homepage: http://marcin.grzejszczak.pl
  • 4. @confiturapl @mgrzejszczak / @4financeit Agenda ● Short introduction ● Real life scenario with a hilarious joke ● How does it work? ● Live (hopefully) coding ● Summary
  • 5. @confiturapl @mgrzejszczak / @4financeit Consumer Driven Contracts ● TDD on architectural level ○ mistakes occur both in implementation and design ● Try to use API before implementing it
  • 6. @confiturapl @mgrzejszczak / @4financeit Real life scenario...
  • 7. @confiturapl @mgrzejszczak / @4financeit Real life scenario... Hi! I’m team X and we have spent weeks to tweak our code to use your API!
  • 8. @confiturapl @mgrzejszczak / @4financeit Real life scenario... That’s so sweet… But we’ve just changed our API because well, it’s ours.
  • 9. @confiturapl @mgrzejszczak / @4financeit Real life scenario... You’ve got to be shitting me! (however it sounds)
  • 10. @confiturapl @mgrzejszczak / @4financeit Real life scenario...
  • 11. @confiturapl @mgrzejszczak / @4financeit CDC building blocks ● Server ● Consumer ● Contract
  • 12. @confiturapl @mgrzejszczak / @4financeit CDC building blocks
  • 13. @confiturapl @mgrzejszczak / @4financeit How to make life easier? Use common contract
  • 14. @confiturapl @mgrzejszczak / @4financeit Contract definition
  • 15. @confiturapl @mgrzejszczak / @4financeit What is a contract? io.codearte.accurest.dsl.GroovyDsl.make { request { method "PUT" url "/fraudcheck" body(''' { “clientPesel":"12345678901", "loanAmount":123.123 } ''' ) headers { header("Content-Type", "application/fraud+json") } } response { status 200 body( """{ "fraudCheckStatus": "OK"}""") headers { header('Content-Type': 'application/fraud+json') } } }
  • 16. @confiturapl @mgrzejszczak / @4financeit What is a contract? io.codearte.accurest.dsl.GroovyDsl.make { request { method "PUT" url "/fraudcheck" body(''' { “clientPesel":"12345678901", "loanAmount":123.123 } ''' ) headers { header("Content-Type", "application/fraud+json") } } response { status 200 body( """{ "fraudCheckStatus": "OK"}""") headers { header('Content-Type': 'application/fraud+json') } } } Groovy DSL
  • 17. @confiturapl @mgrzejszczak / @4financeit What is a contract? io.codearte.accurest.dsl.GroovyDsl.make { request { method "PUT" url "/fraudcheck" body(''' { “clientPesel":"12345678901", "loanAmount":123.123 } ''' ) headers { header("Content-Type", "application/fraud+json") } } response { status 200 body( """{ "fraudCheckStatus": "OK"}""") headers { header('Content-Type': 'application/fraud+json') } } } FOR A GIVEN REQUEST
  • 18. @confiturapl @mgrzejszczak / @4financeit What is a contract? io.codearte.accurest.dsl.GroovyDsl.make { request { method "PUT" url "/fraudcheck" body(''' { “clientPesel":"12345678901", "loanAmount":123.123 } ''' ) headers { header("Content-Type", "application/fraud+json") } } response { status 200 body( """{ "fraudCheckStatus": "OK"}""") headers { header('Content-Type': 'application/fraud+json') } } } WE’LL HAVE SUCH A RESPONSE
  • 19. @confiturapl @mgrzejszczak / @4financeit So how am I supposed to work?
  • 20. @confiturapl @mgrzejszczak / @4financeit Workflow - Consumer side CLONE SERVER REPO AND WORK OFFLINE
  • 21. @confiturapl @mgrzejszczak / @4financeit Workflow - Consumer side 1) ALTER THE CONTRACT LOCALLY 2) GENERATE AND COPY STUBS 3) USE STUBS IN CONSUMER TESTS
  • 22. @confiturapl @mgrzejszczak / @4financeit What is a stub? ● JSON representation of a Groovy DSL contract ● Understood by Wiremock Http Server Stub
  • 23. @confiturapl @mgrzejszczak / @4financeit Workflow - Consumer side FILE A PULL REQUEST WITH THE PROPOSED CONTRACT
  • 24. @confiturapl @mgrzejszczak / @4financeit Server tests - what happens there? ● server has DSLs with contracts ● tests are generated from the contracts ● stubs that consumers can use are generated from the contracts
  • 25. @confiturapl @mgrzejszczak / @4financeit Workflow - Server side ● oh, I have a PR with the proposed contract ● I don’t have the implementation ready so my generated tests are failing ● I’ll add the implementation, fix the tests and merge the PR!
  • 26. @confiturapl @mgrzejszczak / @4financeit Workflow - Server side IMPLEMENTATION IS READY I’LL PUBLISH MY STUB!
  • 27. @confiturapl @mgrzejszczak / @4financeit Consumer Technology Wiremock http://wiremock.org testCompile 'com.github.tomakehurst:wiremock:1.56' { "request": { "method": "GET", "url": "/hello" }, "response": { "status": 200, "body": "Hello world!", "headers": { "Content-Type": "text/plain" } } } Configuration Sample definition
  • 28. @confiturapl @mgrzejszczak / @4financeit Server Technology Accurate REST https://github.com/Codearte/accurest buildscript { repositories { mavenCentral() } dependencies { classpath 'io.codearte.accurest:accurest-gradle-plugin:0.7.0' } } apply plugin: 'accurest'
  • 29. @confiturapl @mgrzejszczak / @4financeit Example
  • 30. @confiturapl @mgrzejszczak / @4financeit New feature
  • 31. @confiturapl @mgrzejszczak / @4financeit Let consumer drive the contract!
  • 32. @confiturapl @mgrzejszczak / @4financeit Live coding - consumer side
  • 33. @confiturapl @mgrzejszczak / @4financeit Consumer prepared the contract io.codearte.accurest.dsl.GroovyDsl.make { request { method "PUT" url "/fraudcheck" body(''' { “clientPesel":"1234567890", "loanAmount":99999 } ''' ) headers { header("Content-Type", "application/vnd.fraud.v1+json") } } response { status 200 body( """{ "fraudCheckStatus": "REJECTED", "rejectionReason": "Amount too high" }""") headers { header('Content-Type': 'application/vnd.fraud.v1+json') } } }
  • 34. @confiturapl @mgrzejszczak / @4financeit Time for server side implementation!
  • 35. @confiturapl @mgrzejszczak / @4financeit Live coding - server side
  • 36. @confiturapl @mgrzejszczak / @4financeit CDC benefits
  • 37. @confiturapl @mgrzejszczak / @4financeit CDC benefits ● Rapid API prototyping ● Non-blocking development ● Behaviour Driven Development
  • 38. @confiturapl @mgrzejszczak / @4financeit CDC benefits ● Client tells what he needs ● Quality - continuously checking if contract is valid
  • 39. @confiturapl @mgrzejszczak / @4financeit CDC benefits ● if server breaks API compatibility consumer tests fail ● if server adds new API I can reuse it immediately in my tests
  • 40. @confiturapl @mgrzejszczak / @4financeit CDC benefits ● The server tests are generated for you ● Any typos / misunderstandings will be found out immediately
  • 41. @confiturapl @mgrzejszczak / @4financeit Not so fast... ● Maintaining datasets ● What exactly to verify for server side? ● Breaking changes ● Accurest under development ○ I also have to sleep sometimes
  • 42. @confiturapl @mgrzejszczak / @4financeit Q&A https://github.com/marcingrzejszczak/2015_confitura_cdc Accurest: https://github.com/Codearte/accurest Micro-Infra-Spring: https://github.com/4finance/micro-infra-spring CDC: http://martinfowler.com/articles/consumerDrivenContracts.html