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 = "Siren Order Provider")
21 public class ContractTest
23 @Pact(consumer="SpringConsumer")
24 public RequestResponsePact getOrders(PactDslWithProvider builder)
27 .uponReceiving("get all orders")
32 .headers(Map.of("Content-Type", "application/vnd.siren+json"))
33 .body(LambdaDsl.newJsonBody(body ->
35 body.array("class", classArray ->
37 classArray.stringValue("entity");
39 body.array("entities", entities ->
41 entities.eachLike(entity ->
43 entity.arrayContaining("actions", actionsArray->
45 actionsArray.object(object ->
47 object.stringType("name","update");
48 object.stringType("method", "PUT");
49 object.matchUrl2("href", Matchers.regexp("\\d+", "1234").getValue());
51 actionsArray.object(object ->
53 object.stringType("name","delete");
54 object.stringType("method", "DELETE");
55 object.matchUrl2("href", Matchers.regexp("\\d+", "1234").getValue());
58 entity.array("class", classArray ->
60 classArray.stringValue("entity");
62 entity.array("links", linksArray ->
64 linksArray.object(object->
66 object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234"));
67 object.array("rel", relArray ->
69 relArray.stringValue("self");
73 entity.object("properties", object->
75 object.stringType("id", "1234");
76 object.array("rel", relArray ->
78 relArray.stringValue("item");
83 body.array("links", linksArray ->
85 linksArray.object(object->
87 object.matchUrl2("href", "orders");
88 object.array("rel", relArray ->
90 relArray.stringValue("self");
99 @PactTestFor(pactMethod = "getOrders")
100 public void testGetExistingUserByEmail(MockServer mockServer)
102 RestTemplate restTemplate =
103 new RestTemplateBuilder()
104 .rootUri(mockServer.getUrl())
108 restTemplate.getForEntity("/orders", String.class);
112 fail("Unexpected exception", e);