From d553ddf4d0da98b4999f609422e87fe9881148b1 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 21 May 2022 12:47:16 +0200 Subject: [PATCH] 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] --- spring-consumer/pom.xml | 65 ++++++++++ .../java/de/juplo/demos/pact/Application.java | 14 +++ .../de/juplo/demos/pact/ApplicationTests.java | 14 +++ .../de/juplo/demos/pact/ContractTest.java | 62 ++++++++++ .../de/juplo/demos/pact/Application.class | Bin 0 -> 727 bytes .../SpringConsumer-Siren Order Provider.json | 115 ++++++++++++++++++ 6 files changed, 270 insertions(+) create mode 100644 spring-consumer/pom.xml create mode 100644 spring-consumer/src/main/java/de/juplo/demos/pact/Application.java create mode 100644 spring-consumer/src/test/java/de/juplo/demos/pact/ApplicationTests.java create mode 100644 spring-consumer/src/test/java/de/juplo/demos/pact/ContractTest.java create mode 100644 spring-consumer/target/classes/de/juplo/demos/pact/Application.class create mode 100644 spring-consumer/target/pacts/SpringConsumer-Siren Order Provider.json 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 0000000000000000000000000000000000000000..2e934b582e34c0e74a97d33203ecf753fdcba81a GIT binary patch literal 727 zcmah{%Wl&^6g`tBb!q|)X(^P4PP~9USmQDDx4Hs# z!3XeBRqwc!;zbaae6Q~7oO@>G@6Gie0PpZBL>Es2?1gxWX94yZHjk86?wDa?d^l(5 zPR&9xY{yE=k9Cnq`&A@4Wk#_{MLri+`S?EBb?2&LIEWXLFY7Wld?5={@lvFYpOj^; zQsI=*5djxMX@ zQ~6%`<%TV;Hy)m0|4Xf%D&$;MiWWZ6+PG#Hnmvw<&3ILM@-q+l)7T517~{C8ok@*8 zQ(0{#pEWI~RA>=I1N5-T@cKWZ13Vw#1zys6E5)CZrA!^el%=+F!CUkA*~BpXn~$usi3$uF?0rram% z%b#x`Dxm&=x>p!|Ji<2RhxGRt1%8{y NjU8c!yr