1 package de.juplo.demos.pact;
3 import au.com.dius.pact.consumer.MockServer;
4 import au.com.dius.pact.consumer.dsl.*;
5 import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
6 import au.com.dius.pact.consumer.junit5.PactTestFor;
7 import au.com.dius.pact.core.model.RequestResponsePact;
8 import au.com.dius.pact.core.model.annotations.Pact;
9 import org.junit.jupiter.api.Test;
10 import org.junit.jupiter.api.extension.ExtendWith;
11 import org.springframework.boot.web.client.RestTemplateBuilder;
12 import org.springframework.web.client.RestTemplate;
16 import static org.assertj.core.api.Assertions.fail;
19 @ExtendWith(PactConsumerTestExt.class)
20 @PactTestFor(providerName = "SirenOrderProvider")
21 public class ContractTest
23 @Pact(consumer="SpringConsumer")
24 public RequestResponsePact getRoot(PactDslWithProvider builder)
27 .uponReceiving("get root")
32 .headers(Map.of("Content-Type", "application/vnd.siren+json"))
33 .body(LambdaDsl.newJsonBody(body ->
35 body.array("class", classArray ->
37 classArray.stringValue("representation");
39 body.array("links", linksArray ->
41 linksArray.object(object->
43 object.matchUrl2("href", "orders");
44 object.array("rel", relArray ->
46 relArray.stringValue("orders");
55 @PactTestFor(pactMethod = "getRoot")
56 public void testGetRoot(MockServer mockServer)
58 RestTemplate restTemplate =
59 new RestTemplateBuilder()
60 .rootUri(mockServer.getUrl())
64 restTemplate.getForEntity("/", String.class);
68 fail("Unexpected exception", e);
72 @Pact(consumer="SpringConsumer")
73 public RequestResponsePact getAllOrders(PactDslWithProvider builder)
76 .uponReceiving("get all orders")
81 .headers(Map.of("Content-Type", "application/vnd.siren+json"))
82 .body(LambdaDsl.newJsonBody(body ->
84 body.array("class", classArray ->
86 classArray.stringValue("entity");
88 body.eachLike("entities", entities ->
90 entities.arrayContaining("actions", actionsArray->
92 actionsArray.object(object ->
94 object.stringType("name","update");
95 object.stringType("method", "PUT");
96 object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getValue());
98 actionsArray.object(object ->
100 object.stringType("name","delete");
101 object.stringType("method", "DELETE");
102 object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getValue());
105 entities.array("class", classArray ->
107 classArray.stringValue("entity");
109 entities.array("links", linksArray ->
111 linksArray.object(object->
113 object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getMatcher());
114 object.array("rel", relArray ->
116 relArray.stringValue("self");
120 entities.object("properties", object->
122 object.integerType("id", 1234);
124 entities.array("rel", relArray ->
126 relArray.stringValue("item");
129 body.array("links", linksArray ->
131 linksArray.object(object->
133 object.matchUrl2("href", "orders");
134 object.array("rel", relArray ->
136 relArray.stringValue("self");
145 @PactTestFor(pactMethod = "getAllOrders")
146 public void testGetExistingUserByEmail(MockServer mockServer)
148 RestTemplate restTemplate =
149 new RestTemplateBuilder()
150 .rootUri(mockServer.getUrl())
154 restTemplate.getForEntity("/orders", String.class);
158 fail("Unexpected exception", e);
162 @Pact(consumer="SpringConsumer")
163 public RequestResponsePact deleteOrder(PactDslWithProvider builder)
166 .uponReceiving("delete order")
167 .matchPath("/orders/\\d+", "/orders/1234")
175 @PactTestFor(pactMethod = "deleteOrder")
176 public void testDeleteOrder(MockServer mockServer)
178 RestTemplate restTemplate =
179 new RestTemplateBuilder()
180 .rootUri(mockServer.getUrl())
184 restTemplate.delete("/orders/1234");
188 fail("Unexpected exception", e);