Consumer Driven Contracts �like TDD to the API
Olga Maciaszek-Sharma, @olga_maciaszek
Marcin Grzejszczak, @mgrzejszczak
© 2019 Pivotal
1
About us
Spring Cloud developer at Pivotal
Working mostly on
Twitter: @olga_maciaszek
2
About us
Spring Cloud developer at Pivotal
Working mostly on
Twitter: @mgrzejszczak
Blog: http://toomuchcoding.com
3
Agenda
Introduction
Demo
Summary
4
What we will NOT be talking about?
5
What we will NOT be talking about?
THE IDEA OF SPRING CLOUD CONTRACT IS NOT TO INTRODUCE UNNECESSARY COUPLING OR REPLICATE OLD MISTAKES
6
Introduction
7
What problems are we trying to solve?
Introduction
8
Typical situation
Introduction
HTTP
CONSUMER
PRODUCER
9
How to write a test for it?
Introduction
HTTP
CONSUMER
PRODUCER
10
How to write a test for it?
Introduction
HTTP
CONSUMER
PRODUCER
11
WireMock
http://wiremock.org/
12
A typical scenario
Introduction
WE’RE ON THE CONSUMER SIDE!!
CONSUMER OWNS THE STUB DEFINITIONS
13
Stub validity & reusability
Introduction
14
Let’s assume that we’re introducing a new endpoint...
15
Introduction
15
New endpoint
16
Introduction
16
So now what?
Introduction
17
Ooops...
Introduction
18
What went wrong?
Stubs that were used in the build phase have nothing to do with the real API!!
Introduction
HTTP
CONSUMER
PRODUCER
TEST
OK
HTTP
CONSUMER
PRODUCER
REALITY
WAT?!
19
Another example with messaging
Stubs that were used in the build phase have nothing to do with the real communication!!
Introduction
CONSUMER
PRODUCER
TEST
PRODUCER
CONSUMER
REALITY
topic: bar
topic: foo
topic: bar
20
Stub validity & reusability - typical problems
Introduction
21
Nice API creation
Introduction
22
Nice API creation - no cooperation results
Introduction
23
Nice API creation - no cooperation results
Introduction
24
Potential answer
Spring Cloud Contract
Demo
25
What are we going to code?
Demo
26
What are we going to code?
Introduction
I’m 22 give me a beer
CONSUMER
PRODUCER
THERE YOU GO
Can the beer be sold?
status: OK, name: marcin
27
What are we going to code?
Introduction
I’m 17 give me a beer
CONSUMER
PRODUCER
GET LOST
Can the beer be sold?
status: NOT_OK, name: marcin
28
Who is who?
Demo
WHITE TERMINAL
WHITE IDE
CONSUMER
BLACK TERMINAL
BLACK IDE
PRODUCER
29
Demo
Consumer’s offline work
Consumer Phase 1
Consumer Phase 2
Consumer’s switching to online
Producer phase
Producer’s implementing the feature
30
Consumer flow 1
Demo
PRODUCER
CONSUMER CLONES PRODUCER
PRODUCER
CLONE
CONSUMER
31
Consumer flow 1
Demo
PRODUCER
CONSUMER
CONSUMER CLONES PRODUCER
PRODUCER
CLONE
INTERACTION
32
Consumer flow 1
Demo
PRODUCER
CONSUMER
PRODUCER
CLONE
INTERACTION
CONSUMER FILES �A PR
33
Demo
34
Producer flow
Demo
PRODUCER
TAKES OVER THE PR
35
Producer flow
Demo
PRODUCER
TAKES OVER THE PR
WRITES THE MISSING IMPLEMENTATION
36
Producer flow
Demo
PRODUCER
TAKES OVER THE PR
WRITES THE MISSING IMPLEMENTATION
MERGES TO MASTER
CI UPLOADS TO NEXUS / ARTIFACTORY
37
Demo
38
Consumer flow 2
Demo
CONSUMER
SWITCHES TO ONLINE MODE
DOWNLOADS STUBS OF COLLABORATORS DURING BUILD PHASE
39
Demo
40
I don’t want to write any scripts!
41
Don’t worry! You can use �Spring Cloud Contract with YML
42
Spring Cloud Contract with YML
42
43
Spring Cloud Contract with YML
43
If you must… then you can also use Swagger definitions
44
Spring Cloud Contract with Swagger
44
You can also use �Spring Cloud Contract with Rest Docs!!
45
Spring Cloud Contract with Rest Docs
45
With Spring Cloud Contract and Consumer Driven Contracts:
Summary
46
Learn More. Stay Connected.
Twitter: twitter.com/springcentral
YouTube: spring.io/video
LinkedIn: spring.io/linkedin
Google Plus: spring.io/gplus
47
QUESTIONS
48