From 20ff01b051ebb4c382a7ce792701bfbb53ac0b5f Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 21 May 2022 21:19:47 +0200 Subject: [PATCH] Defined all contracts in a single pact, like in the original example * Looking back, I have to adhere, that this move is missleading. * For a newbie (like me) this suggests, that all commands will be executed in order on the provider, so that the expected state will be created automatically - but that is not true! --- ...en Order Provider-Siren Order Service.json | 2 +- .../de/juplo/demos/pact/ContractTest.java | 170 +++++++----------- 2 files changed, 62 insertions(+), 110 deletions(-) diff --git a/consumer/pacts/Siren Order Provider-Siren Order Service.json b/consumer/pacts/Siren Order Provider-Siren Order Service.json index 138ef78..c22f0ba 100644 --- a/consumer/pacts/Siren Order Provider-Siren Order Service.json +++ b/consumer/pacts/Siren Order Provider-Siren Order Service.json @@ -253,4 +253,4 @@ "provider": { "name": "Siren Order Service" } -} \ No newline at end of file +} 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 index b863b23..a148b60 100644 --- a/spring-consumer/src/test/java/de/juplo/demos/pact/ContractTest.java +++ b/spring-consumer/src/test/java/de/juplo/demos/pact/ContractTest.java @@ -21,7 +21,7 @@ import static org.assertj.core.api.Assertions.fail; public class ContractTest { @Pact(consumer="SpringConsumer") - public RequestResponsePact getRoot(PactDslWithProvider builder) + public RequestResponsePact deletesTheFirstOrderUsingtheDeleteAction(PactDslWithProvider builder) { return builder .uponReceiving("get root") @@ -48,121 +48,71 @@ public class ContractTest }); }); }).build()) - .toPact(); - } - - @Test - @PactTestFor(pactMethod = "getRoot") - public void testGetRoot(MockServer mockServer) - { - RestTemplate restTemplate = - new RestTemplateBuilder() - .rootUri(mockServer.getUrl()) - .build(); - try - { - restTemplate.getForEntity("/", String.class); - } - catch (Exception e) - { - fail("Unexpected exception", e); - } - } - - @Pact(consumer="SpringConsumer") - public RequestResponsePact getAllOrders(PactDslWithProvider builder) - { - return builder - .uponReceiving("get all orders") - .path("/orders") - .method("GET") - .willRespondWith() - .status(200) - .headers(Map.of("Content-Type", "application/vnd.siren+json")) - .body(LambdaDsl.newJsonBody(body -> + .uponReceiving("get all orders") + .path("/orders") + .method("GET") + .willRespondWith() + .status(200) + .headers(Map.of("Content-Type", "application/vnd.siren+json")) + .body(LambdaDsl.newJsonBody(body -> + { + body.array("class", classArray -> + { + classArray.stringValue("entity"); + }); + body.eachLike("entities", entities -> + { + entities.arrayContaining("actions", actionsArray-> { - body.array("class", classArray -> + actionsArray.object(object -> { - classArray.stringValue("entity"); + object.stringType("name","update"); + object.stringType("method", "PUT"); + object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getValue()); }); - body.eachLike("entities", entities -> + actionsArray.object(object -> { - entities.arrayContaining("actions", actionsArray-> - { - actionsArray.object(object -> - { - object.stringType("name","update"); - object.stringType("method", "PUT"); - object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getValue()); - }); - actionsArray.object(object -> - { - object.stringType("name","delete"); - object.stringType("method", "DELETE"); - object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getValue()); - }); - }); - entities.array("class", classArray -> - { - classArray.stringValue("entity"); - }); - entities.array("links", linksArray -> - { - linksArray.object(object-> - { - object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getMatcher()); - object.array("rel", relArray -> - { - relArray.stringValue("self"); - }); - }); - }); - entities.object("properties", object-> - { - object.integerType("id", 1234); - }); - entities.array("rel", relArray -> - { - relArray.stringValue("item"); - }); + object.stringType("name","delete"); + object.stringType("method", "DELETE"); + object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getValue()); }); - body.array("links", linksArray -> + }); + entities.array("class", classArray -> + { + classArray.stringValue("entity"); + }); + entities.array("links", linksArray -> + { + linksArray.object(object-> { - linksArray.object(object-> + object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getMatcher()); + object.array("rel", relArray -> { - object.matchUrl2("href", "orders"); - object.array("rel", relArray -> - { - relArray.stringValue("self"); - }); + relArray.stringValue("self"); }); }); - }).build()) - .toPact(); - } - - @Test - @PactTestFor(pactMethod = "getAllOrders") - 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); - } - } - - @Pact(consumer="SpringConsumer") - public RequestResponsePact deleteOrder(PactDslWithProvider builder) - { - return builder + }); + entities.object("properties", object-> + { + object.integerType("id", 1234); + }); + entities.array("rel", relArray -> + { + relArray.stringValue("item"); + }); + }); + body.array("links", linksArray -> + { + linksArray.object(object-> + { + object.matchUrl2("href", "orders"); + object.array("rel", relArray -> + { + relArray.stringValue("self"); + }); + }); + }); + }).build()) .uponReceiving("delete order") .matchPath("/orders/\\d+", "/orders/1234") .method("DELETE") @@ -172,8 +122,8 @@ public class ContractTest } @Test - @PactTestFor(pactMethod = "deleteOrder") - public void testDeleteOrder(MockServer mockServer) + @PactTestFor(pactMethod = "deletesTheFirstOrderUsingtheDeleteAction") + public void testDeletesTheFirstOrderUsingtheDeleteAction(MockServer mockServer) { RestTemplate restTemplate = new RestTemplateBuilder() @@ -181,6 +131,8 @@ public class ContractTest .build(); try { + restTemplate.getForEntity("/", String.class); + restTemplate.getForEntity("/orders", String.class); restTemplate.delete("/orders/1234"); } catch (Exception e) -- 2.20.1