Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Consumer Driven Contracts in Action

Consumer Driven Contracts in Action

How can Consumer Driven Contracts help you with the technical challenges of your Microservice Architecture? This talk shows experiences, tools and learnings from a Multi-Team Ecommerce project.

7379c719bba185bb01143b61b84c373d?s=128

Christian Fischer

September 20, 2018
Tweet

Transcript

  1. Consumer Driven Contracts in der Praxis @agiledojo

  2. Multichannel Ecommerce Platform Channel Capabilities Resources Bestellung Bezahlung Katalog Web

    B2B Mobile PIM Bestand
  3. None
  4. Cascading Changes Bestellung Web B2B Mobile

  5. Why not Release Trains? Welcome to Waterfall!!! Solutions? Parallel Versions?

    Complexity Hell!!! I heard about this new Thing called CDC! OK! Let‘s give it a try!
  6. Consumer Driven Contracts Bestellung Web B2B Mobile

  7. CDC Frameworks Consumer Provider Provider Stub Contract Verification Test Consumer

    Test
  8. Spring Cloud Contract (SCC): Example Consumer Provider Request: http://providerhost/offers/<offerId> Response:

    { “status“: „open“, ... }
  9. SCC: Contract ... Contract.make { description "should get order status"

    request{ method GET() url $(consumer(~/\/orders\/[0-9]+/), producer('/orders/1234')) } response { status OK() body([ status: "open" ]) } }
  10. SCC: Contract Verification Test public class ContractVerifierTest extends ConsumerBase {

    @Test public void validate_providerContract() throws Exception { // given: MockMvcRequestSpecification request = given(); // when: ResponseOptions response = given().spec(request) .get("/orders/1234"); // then: assertThat(response.statusCode()).isEqualTo(200); // and: DocumentContext parsedJson = JsonPath.parse(response.getBody().asString()); assertThatJson(parsedJson).field("['status']").isEqualTo("open"); } }
  11. SCC: Consumer Test with Stub ... @AutoConfigureStubRunner(ids = {"de.agiledojo:cdcprovider:+:stubs:6565"}, stubsMode

    = StubRunnerProperties.StubsMode.LOCAL) @DirtiesContext public class ProviderAdapterTest { @Autowired RestTemplateBuilder builder; private ProviderAdapter adapter; @Before public void setUp(){ adapter = new ProviderAdapter(builder.build(),"http://localhost:6565/orders"); } @Test public void should_get_status() { String orderStatus = adapter.getStatus("123"); Assert.assertEquals("open",orderStatus); } }
  12. CC Workflow Implement Consumer Feature 1. 1. Develop Consumer on

    new Feature Branch using TDD 2. Fork Provider Repository and create Feature Branch 3. Extend Consumer Contract until Stub satisfies Tests 4. Send Pull Request Implement & Release Provider 2. 1. Pull Feature Branch 2. Implement Provider using Contract Verification Test 3. Merge Feature Branch 4. Release Provider & Contract + Stub Merge & Release Consumer 3. 1. Switch to released Stub 2. Merge Feature Branch 3. Publish Consumer
  13. 2. 1. Avoid Breaking Changes { productId: 3456 productName: „Tobi“

    category: „General“ ... } { productId: 3456 productName: „Tobi“ category: „General“ categories: [ „General“, „Furniture“, „Lifestyle“ ... } { productId: 3456 productName: „Tobi“ categories: [ „General“, „Furniture“, „Lifestyle“ ... } Expand Contract
  14. Feature starts with the Consumer. Bestellung Mobile Mobile Story Bestellung

    Story 1 2 3 4
  15. Clients and Data Models Provider Contract Consumer Contract Client Client

    Generation Data Model TDD Data Model
  16. Transitive Data Models Katalog Web PIM Katalog Web PIM specialOffers:[

    {productId: 3456 productName: Tobi productPrice: 34.50€}, ... ] specialOffers:[ href: „http://pim/article/3456“, ... ]
  17. Contract Tests are not Functional Tests - Optional Fields -

    Journeys - Validation Rules ü protocol syntax ü Specification by example ü Breaking Changes Covered Not Covered
  18. Take Away 1. CDCs help to avoid Release Trains and

    Interface Versioning. 2. Spring Cloud Contracts provide a Test Framework and Workflow for CDCs. 3. Avoid breaking Changes with Expand & Contract Pattern. 4. Feature Implementation should start with the Consumer. 5. Implement Consumer test driven. 6. Avoid indirect Contracts. 7. Don‘t forget functional Tests.