Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Consumer Driven Contracts like TDD to the API - Olga Maciaszek-Sharma & Marcin Grzejszczak

166 views

Published on

Consumer Driven Contracts like TDD to the API by Olga Maciaszek-Sharma & Marcin Grzejszczak at SpringOne Tour 2019

Published in: Software
  • Be the first to comment

Consumer Driven Contracts like TDD to the API - Olga Maciaszek-Sharma & Marcin Grzejszczak

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

×