1 of 48

Consumer Driven Contracts �like TDD to the API

Olga Maciaszek-Sharma, @olga_maciaszek

Marcin Grzejszczak, @mgrzejszczak

© 2019 Pivotal

1

2 of 48

About us

Spring Cloud developer at Pivotal

Working mostly on

  • Spring Cloud Contract
  • Spring Cloud Netflix
  • Spring Cloud Gateway

Twitter: @olga_maciaszek

2

3 of 48

About us

Spring Cloud developer at Pivotal

Working mostly on

  • Spring Cloud Sleuth
  • Spring Cloud Contract
  • CI / CD

Twitter: @mgrzejszczak

Blog: http://toomuchcoding.com

3

4 of 48

Agenda

Introduction

Demo

Summary

4

5 of 48

What we will NOT be talking about?

5

6 of 48

  • Schema
  • WSDL
  • ESB
  • XSD
  • XSLT

What we will NOT be talking about?

THE IDEA OF SPRING CLOUD CONTRACT IS NOT TO INTRODUCE UNNECESSARY COUPLING OR REPLICATE OLD MISTAKES

6

7 of 48

  • Producer
    • service that exposes an API / sends a message
  • Consumer
    • service that consumes the API of the producer / listens to a message from the producer
  • Contract
    • agreement between producer and consumer how the API / message will look like
  • Consumer Driven Contracts
    • approach where the consumer drives the changes of the API of the producer

Introduction

7

8 of 48

What problems are we trying to solve?

  • Stub validity & reusability in the integration tests
  • Nice API creation

Introduction

8

9 of 48

Typical situation

Introduction

HTTP

CONSUMER

PRODUCER

9

10 of 48

How to write a test for it?

Introduction

HTTP

CONSUMER

PRODUCER

10

11 of 48

How to write a test for it?

Introduction

HTTP

CONSUMER

PRODUCER

11

12 of 48

WireMock

http://wiremock.org/

12

13 of 48

A typical scenario

Introduction

WE’RE ON THE CONSUMER SIDE!!

CONSUMER OWNS THE STUB DEFINITIONS

13

14 of 48

Stub validity & reusability

Introduction

14

15 of 48

Let’s assume that we’re introducing a new endpoint...

15

Introduction

15

16 of 48

New endpoint

16

Introduction

16

17 of 48

So now what?

  • I write the missing code to work with the new URL
  • The unit and integration tests pass
  • Now we deploy to an environment �where real integrations take place...

Introduction

17

18 of 48

Ooops...

Introduction

18

19 of 48

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

20 of 48

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

21 of 48

Stub validity & reusability - typical problems

  • Stubs reside with the consumer
  • Consumer controls the stubbing process
  • How are you sure that the stubs are valid?
  • What if other teams want to reuse those stubs?
  • How are you sure you’re listening to the proper topic / queue?
  • Are you sure you’ll deserialize your message properly?

Introduction

21

22 of 48

Nice API creation

  • It’s the consumer that uses the API
  • Consumers should take part in the creation of the API of the producer
  • The producer’s API change should be driven by consumers

Introduction

22

23 of 48

Nice API creation - no cooperation results

Introduction

23

24 of 48

Nice API creation - no cooperation results

Introduction

24

25 of 48

Potential answer

Spring Cloud Contract

Demo

25

26 of 48

What are we going to code?

  • Consumer
    • service that gets beer requests
    • has to ask another service if the client can get the beer
  • Producer
    • service that checks if the client is old enough to buy beer
  • Feature
    • if the user is too young - the beer will not be sold
    • otherwise the beer will be granted

Demo

26

27 of 48

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

28 of 48

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

29 of 48

Who is who?

Demo

WHITE TERMINAL

WHITE IDE

CONSUMER

BLACK TERMINAL

BLACK IDE

PRODUCER

29

30 of 48

Demo

Consumer’s offline work

Consumer Phase 1

Consumer Phase 2

Consumer’s switching to online

Producer phase

Producer’s implementing the feature

30

31 of 48

Consumer flow 1

Demo

PRODUCER

CONSUMER CLONES PRODUCER

PRODUCER

CLONE

CONSUMER

31

32 of 48

Consumer flow 1

Demo

PRODUCER

CONSUMER

CONSUMER CLONES PRODUCER

PRODUCER

CLONE

INTERACTION

32

33 of 48

Consumer flow 1

Demo

PRODUCER

CONSUMER

PRODUCER

CLONE

INTERACTION

CONSUMER FILES �A PR

33

34 of 48

Demo

34

35 of 48

Producer flow

Demo

PRODUCER

TAKES OVER THE PR

35

36 of 48

Producer flow

Demo

PRODUCER

TAKES OVER THE PR

WRITES THE MISSING IMPLEMENTATION

36

37 of 48

Producer flow

Demo

PRODUCER

TAKES OVER THE PR

WRITES THE MISSING IMPLEMENTATION

MERGES TO MASTER

CI UPLOADS TO NEXUS / ARTIFACTORY

37

38 of 48

Demo

38

39 of 48

Consumer flow 2

Demo

CONSUMER

SWITCHES TO ONLINE MODE

DOWNLOADS STUBS OF COLLABORATORS DURING BUILD PHASE

39

40 of 48

Demo

40

41 of 48

I don’t want to write any scripts!

41

42 of 48

Don’t worry! You can use �Spring Cloud Contract with YML

42

Spring Cloud Contract with YML

42

43 of 48

43

Spring Cloud Contract with YML

43

44 of 48

If you must… then you can also use Swagger definitions

44

Spring Cloud Contract with Swagger

44

45 of 48

You can also use �Spring Cloud Contract with Rest Docs!!

45

Spring Cloud Contract with Rest Docs

45

46 of 48

With Spring Cloud Contract and Consumer Driven Contracts:

  • we’ve created an API that suits the consumer and the producer
  • expectations were defined by readable contracts
  • expectations were tested against the producer
  • producer stubs can be reused by consumers
  • starting and setting stubs is fully automated

Summary

46

47 of 48

Learn More. Stay Connected.

Twitter: twitter.com/springcentral

YouTube: spring.io/video

LinkedIn: spring.io/linkedin

Google Plus: spring.io/gplus

47

48 of 48

QUESTIONS

48