Slide 1

Slide 1 text

Olga Maciaszek-Sharma Oleg Šelajev May 2023 Build resilient systems with Spring Cloud Contract and Testcontainers Copyright © 2023 VMware, Inc. or its affiliates.

Slide 2

Slide 2 text

What is going to happen?

Slide 3

Slide 3 text

About Olga Senior Software Engineer in Spring Cloud Team ● Spring Cloud LoadBalancer (Spring Cloud Commons) ● Spring Interface Clients ● Spring Cloud Contract ● Spring Cloud OpenFeign ● Spring Cloud Netflix ● Spring Native support for Spring Cloud projects ● RSocket HTTP Bridge https://github.com/OlgaMaciaszek https://twitter.com/olga_maciaszek

Slide 4

Slide 4 text

Developer advocate at AtomicJar ● Testcontainers for Java https://github.com/shelajev https://twitter.com/shelajev About Oleg

Slide 5

Slide 5 text

Our project

Slide 6

Slide 6 text

A new message topic: servings body: { "beverages": [ { "uuid": "16b439a4-6ace-4901-8fb6-728588c12da8", "coffee": { "name": "Latte", "coffeeContent": 60, "steamedMilkContent": 180, "milkFoamContent": 5 } } // … ] } headers: “contentType”: “application/json” “testKey1”: “testValue1”

Slide 7

Slide 7 text

A new message

Slide 8

Slide 8 text

We go to production! Production

Slide 9

Slide 9 text

A new message topic: servings body: { "beverages": [ { "uuid": "16b439a4-6ace-4901-8fb6-728588c12da8", "coffee": { "name": "Latte", "coffeeContent": 60, "steamedMilkContent": 180, "milkFoamContent": 5 } } // … ] } headers: “contentType”: “application/json” “testKey1”: “testValue1”

Slide 10

Slide 10 text

A new message topic: serving body: { "beverages": [ { "uuid": "16b439a4-6ace-4901-8fb6-728588c12da8", "coffee": { "name": "Latte", "coffeeContent": 60, "steamedMilkContent": 180, "milkFoamContent": 5 } } // … ] } headers: “contentType”: “application/json” “testKey1”: “testValue1”

Slide 11

Slide 11 text

A new message topic: servings body: { "beverages": [ { "uuid": "16b439a4-6ace-4901-8fb6-728588c12da8", "coffee": { "name": "Latte", "coffeeContent": 60, "steamedMilkContent": 180, "milkFoamContent": 5 } } // … ] } headers: “contentType”: “application/json” “testKey1”: “testValue1”

Slide 12

Slide 12 text

A new message topic: servings body: { "beverages": [ { "id": "16b439a4-6ace-4901-8fb6-728588c12da8", "coffee": { "name": "Latte", "coffeeContent": 60, "steamedMilkContent": 180, "milkFoamContent": 5 } } // … ] } headers: “contentType”: “application/json” “testKey1”: “testValue1”

Slide 13

Slide 13 text

A new message topic: servings body: { "beverages": [ { "uuid": "16b439a4-6ace-4901-8fb6-728588c12da8", "coffee": { "name": "Latte", "coffeeContent": 60, "steamedMilkContent": 180, "milkFoamContent": 5 } } // … ] } headers: “contentType”: “application/json” “testKey1”: “testValue1”

Slide 14

Slide 14 text

A new message topic: servings body: { "beverages": [ { "uuid": "16b439a4-6ace-4901-8fb6-728588c12da8", "coffee": { "name": "Latte", "coffeeContent": 60, "steamedMilkContent": 180, "milkFoamContent": 5 } } // … ] } headers: “contentType”: “application/json” “testKey1”: “testValue1”

Slide 15

Slide 15 text

Stubs alone don’t guarantee ANYTHING

Slide 16

Slide 16 text

The Answer: Consumer-Driven Contracts

Slide 17

Slide 17 text

Spring Cloud Contract

Slide 18

Slide 18 text

No free stubs! Build tool plugin Contract definitions Generated tests Stubs Test setup configuration

Slide 19

Slide 19 text

Producer Build Setup org.springframework.cloud spring-cloud-contract-maven-plugin 4.0.3-SNAPSHOT true JUNIT5 io.xxx.BaseTestClass org.springframework.boot spring-boot-maven-plugin

Slide 20

Slide 20 text

● Setup the environment ● Create inputs ● Produce the outputs (with production code) Producer test setup

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

● TC slide

Slide 23

Slide 23 text

● TC slide

Slide 24

Slide 24 text

No free stubs! Build tool plugin Contract definitions Generated tests Stubs Test setup configuration

Slide 25

Slide 25 text

Contract Contract.make { label("serving") input { triggeredBy("triggerServing()") } / ... }

Slide 26

Slide 26 text

Contract Contract.make { / ... outputMessage { sentTo("servings") body([beverages: [[uuid : $(anyUuid()), coffee: [ name : "Latte", coffeeContent : "60", steamedMilkContent: "180", milkFoamContent : "5" ]]]]) / ... }

Slide 27

Slide 27 text

Contract Contract.make { / ... headers { messagingContentType(applicationJson()) header 'testKey1', 'testValue1' } }

Slide 28

Slide 28 text

No free stubs! Build tool plugin Contract definitions Generated tests Stubs Test setup configuration

Slide 29

Slide 29 text

Generated Contract Test @Test public void validate_shouldSendServing() throws Exception { // when: triggerServing(); // then: ContractVerifierMessage response = contractVerifierMessaging.receive("servings", contract(this, "shouldSendServing.yml")); assertThat(response).isNotNull(); // and: assertThat(response.getHeader("contentType")).isNotNull(); assertThat(response.getHeader("contentType").toString()).isEqualTo("application/json"); assertThat(response.getHeader("testKey1")).isNotNull(); assertThat(response.getHeader("testKey1").toString()).isEqualTo("testValue1"); // ...

Slide 30

Slide 30 text

Generated Contract Test @Test public void validate_shouldSendServing() throws Exception { // ... DocumentContext parsedJson = JsonPath.parse(contractVerifierObjectMapper.writeValueAsString(response.getPayload())); assertThatJson(parsedJson).array("['beverages']").contains("['uuid']") .matches("[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}") assertThatJson(parsedJson).array("['beverages']").field("['coffee']").field("['name']") .isEqualTo("Latte"); assertThatJson(parsedJson).array("['beverages']").field("['coffee']").field("['coffeeContent'] ") .isEqualTo("60"); assertThatJson(parsedJson).array("['beverages']").field("['coffee']").field("['steamedMilkCont ent']") .isEqualTo("180"); assertThatJson(parsedJson).array("['beverages']").field("['coffee']").field("['milkFoamContent ']") .isEqualTo("5");

Slide 31

Slide 31 text

No free stubs! Build tool plugin Contract definitions Generated tests Stubs Test setup configuration

Slide 32

Slide 32 text

Contracts vs Schemas

Slide 33

Slide 33 text

Consumer integration tests using stubs from Contract and TestContainers

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Local Development Environment

Slide 36

Slide 36 text

Teamwork - different approaches Provider Contracts ● Contracts with the producer ● The producer has no knowledge of how consumers use their API ● The producer can impose contract changes on consumers Consumer Contracts ● Contracts with the consumers ● Possible issues with conflict resolution in case of multiple consumers ● In some implementations, the consumers can impose contract changes on the producer Consumer-Driven Contracts ● Contracts with the producer ○ If security issues, an external repo ● Consumers as key participants in the creation of the contracts ● Convenient handling of multiple consumers

Slide 37

Slide 37 text

Spring Cloud Contract - Consumer-Driven Contract Flow Consumer ● Checks out Producer code repo and works locally on a contract ● Creates a pull request with the new Contract ● Collaborates with the Producer on the Contract PR ● Uses stubs published by the producer along the app for integration tests Producer ● Reviews the Contract PR ● Runs build on the PR branch (FAILURE) ● Adds required endpoints ● Runs build on the PR branch (PASSING) ● Code Deployed along with generated stubs jar

Slide 38

Slide 38 text

Spring Cloud Contract - Consumer-Driven Contract Flow Consumer ● Checks out Producer code repo and works locally on a contract ● Creates a pull request with the new Contract ● Collaborates with the Producer on the Contract PR ● Uses stubs published by the producer along the app for integration tests Producer ● Reviews the Contract PR ● Runs build on the PR branch (FAILURE) ● Adds required endpoints ● Runs build on the PR branch (PASSING) ● Code Deployed along with generated stubs jar

Slide 39

Slide 39 text

Spring Cloud Contract - Consumer-Driven Contract Flow Consumer ● Checks out Producer code repo and works locally on a contract ● Creates a pull request with the new Contract ● Collaborates with the Producer on the Contract PR ● Uses stubs published by the producer along the app for integration tests Producer ● Reviews the Contract PR ● Runs build on the PR branch (FAILURE) ● Adds required endpoints ● Runs build on the PR branch (PASSING) ● Code Deployed along with generated stubs jar

Slide 40

Slide 40 text

Spring Cloud Contract - Consumer-Driven Contract Flow Consumer ● Checks out Producer code repo and works locally on a contract ● Creates a pull request with the new Contract ● Collaborates with the Producer on the Contract PR ● Uses stubs published by the producer along the app for integration tests Producer ● Reviews the Contract PR ● Runs build on the PR branch (FAILURE) ● Adds required endpoints ● Runs build on the PR branch (PASSING) ● Code Deployed along with generated stubs jar

Slide 41

Slide 41 text

Spring Cloud Contract - Consumer-Driven Contract Flow Consumer ● Checks out Producer code repo and works locally on a contract ● Creates a pull request with the new Contract ● Collaborates with the Producer on the Contract PR ● Uses stubs published by the producer along the app for integration tests Producer ● Reviews the Contract PR ● Runs build on the PR branch (FAILURE) ● Adds required endpoints ● Runs build on the PR branch (PASSING) ● Code Deployed along with generated stubs jar

Slide 42

Slide 42 text

Spring Cloud Contract - Consumer-Driven Contract Flow Consumer ● Checks out Producer code repo and works locally on a contract ● Creates a pull request with the new Contract ● Collaborates with the Producer on the Contract PR ● Uses stubs published by the producer along the app for integration tests Producer ● Reviews the Contract PR ● Runs build on the PR branch (FAILURE) ● Adds required endpoints ● Runs build on the PR branch (PASSING) ● Code Deployed along with generated stubs jar

Slide 43

Slide 43 text

Spring Cloud Contract - Consumer-Driven Contract Flow Consumer ● Checks out Producer code repo and works locally on a contract ● Creates a pull request with the new Contract ● Collaborates with the Producer on the Contract PR ● Uses stubs published by the producer along the app for integration tests Producer ● Reviews the Contract PR ● Runs build on the PR branch (FAILURE) ● Adds required endpoints ● Runs build on the PR branch (PASSING) ● Code Deployed along with generated stubs jar

Slide 44

Slide 44 text

Spring Cloud Contract - Consumer-Driven Contract Flow Consumer ● Checks out Producer code repo and works locally on a contract ● Creates a pull request with the new Contract ● Collaborates with the Producer on the Contract PR ● Uses stubs published by the producer along the app for integration tests Producer ● Reviews the Contract PR ● Runs build on the PR branch (FAILURE) ● Adds required endpoints ● Runs build on the PR branch (PASSING) ● Code Deployed along with generated stubs jar

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Backward Compatibility Checks in Pipelines New API version Test with PROD contracts Deploy Test with new contracts

Slide 47

Slide 47 text

Producer Build Setup - Compatibility Tests with Profiles apicompatibility org.springframework.cloud spring-cloud-contract-maven-plugin 4.0.3-SNAPSHOT ${repo.with.binaries} REMOTE ${project.groupId} ${project.artifactId} stubs ${latest.production.version}

Slide 48

Slide 48 text

Evolving the system safely Deployment Contract fields Producer fields 0 { "uuid": "xxx" } { "uuid": "xxx" } 1 { "uuid": "xxx", "id": 5 } { "uuid": “xxx”, "id": 5 } 2 { "id": 5 } { "uuid": “xxx”, "id": 5 } 3 { "id": 5 } { "id": 5 }

Slide 49

Slide 49 text

What’s next? github.com/OlgaMaciaszek/coffee testcontainers.com https://docs.spring.io/spring-cloud-contract/docs/current/reference/html/