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.

Christian Fischer

September 20, 2018
Tweet

More Decks by Christian Fischer

Other Decks in Technology

Transcript

  1. 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!
  2. 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" ]) } }
  3. 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"); } }
  4. 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); } }
  5. 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
  6. 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
  7. Transitive Data Models Katalog Web PIM Katalog Web PIM specialOffers:[

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

    Journeys - Validation Rules ü protocol syntax ü Specification by example ü Breaking Changes Covered Not Covered
  9. 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.