From: Kai Moritz Date: Sat, 21 May 2022 10:47:16 +0000 (+0200) Subject: Spring-Consumer, um den Kontrakt mit der Lambda-DSL nachzubauen X-Git-Tag: wip-umbau-array-contains~15 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=d553ddf4d0da98b4999f609422e87fe9881148b1;p=demos%2Fexample-siren Spring-Consumer, um den Kontrakt mit der Lambda-DSL nachzubauen * Macht noch was ganz anderes... * Geht von dem Beispiel der PactDslJsonBody DSL aus * Siehe: https://docs.pact.io/implementation_guides/jvm/consumer#building-json-bodies-with-pactdsljsonbody-dsl[Beispiel] --- diff --git a/spring-consumer/pom.xml b/spring-consumer/pom.xml new file mode 100644 index 0000000..23844a2 --- /dev/null +++ b/spring-consumer/pom.xml @@ -0,0 +1,65 @@ + + + + + org.springframework.boot + spring-boot-starter-parent + 2.7.0 + + + + 4.0.0 + de.juplo.demos.pact + siren-consumer + 1.0.0-SNAPSHOT + + + 11 + 4.2.2 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + au.com.dius.pact.consumer + junit5 + ${pact.version} + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + maven-failsafe-plugin + + + + + diff --git a/spring-consumer/src/main/java/de/juplo/demos/pact/Application.java b/spring-consumer/src/main/java/de/juplo/demos/pact/Application.java new file mode 100644 index 0000000..5dccaa0 --- /dev/null +++ b/spring-consumer/src/main/java/de/juplo/demos/pact/Application.java @@ -0,0 +1,14 @@ +package de.juplo.demos.pact; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application +{ + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-consumer/src/test/java/de/juplo/demos/pact/ApplicationTests.java b/spring-consumer/src/test/java/de/juplo/demos/pact/ApplicationTests.java new file mode 100644 index 0000000..7c96248 --- /dev/null +++ b/spring-consumer/src/test/java/de/juplo/demos/pact/ApplicationTests.java @@ -0,0 +1,14 @@ +package de.juplo.demos.pact; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ApplicationTests +{ + + @Test + void contextLoads() { + } + +} diff --git a/spring-consumer/src/test/java/de/juplo/demos/pact/ContractTest.java b/spring-consumer/src/test/java/de/juplo/demos/pact/ContractTest.java new file mode 100644 index 0000000..d7358d8 --- /dev/null +++ b/spring-consumer/src/test/java/de/juplo/demos/pact/ContractTest.java @@ -0,0 +1,62 @@ +package de.juplo.demos.pact; + +import au.com.dius.pact.consumer.MockServer; +import au.com.dius.pact.consumer.dsl.PactDslJsonBody; +import au.com.dius.pact.consumer.dsl.PactDslWithProvider; +import au.com.dius.pact.consumer.junit5.PactConsumerTestExt; +import au.com.dius.pact.consumer.junit5.PactTestFor; +import au.com.dius.pact.core.model.RequestResponsePact; +import au.com.dius.pact.core.model.annotations.Pact; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.web.client.RestTemplate; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.fail; + + +@ExtendWith(PactConsumerTestExt.class) +@PactTestFor(providerName = "Siren Order Provider") +public class ContractTest +{ + @Pact(consumer="SpringConsumer") + public RequestResponsePact getOrders(PactDslWithProvider builder) + { + PactDslJsonBody body = new PactDslJsonBody() + .stringType("name") + .booleanType("happy") + .hexValue("hexCode") + .id() + .ipAddress("localAddress") + .numberValue("age", 100); + return builder + .uponReceiving("get all orders") + .path("/orders") + .method("GET") + .willRespondWith() + .status(200) + .headers(Map.of("Content-Type", "application/vnd.siren+json")) + .body(body) + .toPact(); + } + + @Test + @PactTestFor(pactMethod = "getOrders") + public void testGetExistingUserByEmail(MockServer mockServer) + { + RestTemplate restTemplate = + new RestTemplateBuilder() + .rootUri(mockServer.getUrl()) + .build(); + try + { + restTemplate.getForEntity("/orders", String.class); + } + catch (Exception e) + { + fail("Unexpected exception", e); + } + } +} diff --git a/spring-consumer/target/classes/de/juplo/demos/pact/Application.class b/spring-consumer/target/classes/de/juplo/demos/pact/Application.class new file mode 100644 index 0000000..2e934b5 Binary files /dev/null and b/spring-consumer/target/classes/de/juplo/demos/pact/Application.class differ diff --git a/spring-consumer/target/pacts/SpringConsumer-Siren Order Provider.json b/spring-consumer/target/pacts/SpringConsumer-Siren Order Provider.json new file mode 100644 index 0000000..953d4e6 --- /dev/null +++ b/spring-consumer/target/pacts/SpringConsumer-Siren Order Provider.json @@ -0,0 +1,115 @@ +{ + "consumer": { + "name": "SpringConsumer" + }, + "interactions": [ + { + "description": "get all orders", + "request": { + "method": "GET", + "path": "/orders" + }, + "response": { + "body": { + "age": 100, + "happy": true, + "hexCode": "1234a", + "id": 1234567890, + "localAddress": "127.0.0.1", + "name": "string" + }, + "generators": { + "body": { + "$.hexCode": { + "digits": 10, + "type": "RandomHexadecimal" + }, + "$.id": { + "max": 2147483647, + "min": 0, + "type": "RandomInt" + }, + "$.name": { + "size": 20, + "type": "RandomString" + } + } + }, + "headers": { + "Content-Type": "application/vnd.siren+json" + }, + "matchingRules": { + "body": { + "$.happy": { + "combine": "AND", + "matchers": [ + { + "match": "type" + } + ] + }, + "$.hexCode": { + "combine": "AND", + "matchers": [ + { + "match": "regex", + "regex": "[0-9a-fA-F]+" + } + ] + }, + "$.id": { + "combine": "AND", + "matchers": [ + { + "match": "type" + } + ] + }, + "$.localAddress": { + "combine": "AND", + "matchers": [ + { + "match": "regex", + "regex": "(\\d{1,3}\\.)+\\d{1,3}" + } + ] + }, + "$.name": { + "combine": "AND", + "matchers": [ + { + "match": "type" + } + ] + } + } + }, + "status": 200 + } + }, + { + "description": "get root", + "request": { + "method": "GET", + "path": "/" + }, + "response": { + "headers": { + "Content-Type": "application/vnd.siren+json" + }, + "status": 200 + } + } + ], + "metadata": { + "pact-jvm": { + "version": "4.2.2" + }, + "pactSpecification": { + "version": "3.0.0" + } + }, + "provider": { + "name": "Siren Order Provider" + } +}