Slide 1

Slide 1 text

Consumer Driven Contracts in der Praxis @agiledojo

Slide 2

Slide 2 text

Multichannel Ecommerce Platform Channel Capabilities Resources Bestellung Bezahlung Katalog Web B2B Mobile PIM Bestand

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Cascading Changes Bestellung Web B2B Mobile

Slide 5

Slide 5 text

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!

Slide 6

Slide 6 text

Consumer Driven Contracts Bestellung Web B2B Mobile

Slide 7

Slide 7 text

CDC Frameworks Consumer Provider Provider Stub Contract Verification Test Consumer Test

Slide 8

Slide 8 text

Spring Cloud Contract (SCC): Example Consumer Provider Request: http://providerhost/offers/ Response: { “status“: „open“, ... }

Slide 9

Slide 9 text

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" ]) } }

Slide 10

Slide 10 text

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"); } }

Slide 11

Slide 11 text

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); } }

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Feature starts with the Consumer. Bestellung Mobile Mobile Story Bestellung Story 1 2 3 4

Slide 15

Slide 15 text

Clients and Data Models Provider Contract Consumer Contract Client Client Generation Data Model TDD Data Model

Slide 16

Slide 16 text

Transitive Data Models Katalog Web PIM Katalog Web PIM specialOffers:[ {productId: 3456 productName: Tobi productPrice: 34.50€}, ... ] specialOffers:[ href: „http://pim/article/3456“, ... ]

Slide 17

Slide 17 text

Contract Tests are not Functional Tests - Optional Fields - Journeys - Validation Rules ü protocol syntax ü Specification by example ü Breaking Changes Covered Not Covered

Slide 18

Slide 18 text

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.