1 package io.pactflow.example.sirenconsumer;
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 deletesTheFirstOrderUsingtheDeleteAction(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");
51 .uponReceiving("get all orders")
56 .headers(Map.of("Content-Type", "application/vnd.siren+json"))
57 .body(LambdaDsl.newJsonBody(body ->
59 body.array("class", classArray ->
61 classArray.stringValue("entity");
63 body.eachLike("entities", entities ->
65 entities.arrayContaining("actions", actionsArray->
67 actionsArray.object(object ->
69 object.stringValue("name","update");
70 object.stringValue("method", "PUT");
71 object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getMatcher());
73 actionsArray.object(object ->
75 object.stringValue("name","delete");
76 object.stringValue("method", "DELETE");
77 object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getMatcher());
80 entities.array("class", classArray ->
82 classArray.stringValue("entity");
84 entities.array("links", linksArray ->
86 linksArray.object(object->
88 object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getMatcher());
89 object.array("rel", relArray ->
91 relArray.stringValue("self");
95 entities.object("properties", object->
97 object.integerType("id", 1234);
99 entities.array("rel", relArray ->
101 relArray.stringValue("item");
104 body.array("links", linksArray ->
106 linksArray.object(object->
108 object.matchUrl2("href", "orders");
109 object.array("rel", relArray ->
111 relArray.stringValue("self");
116 .uponReceiving("delete order")
117 .matchPath("/orders/\\d+", "/orders/1234")
125 @PactTestFor(pactMethod = "deletesTheFirstOrderUsingtheDeleteAction")
126 public void testDeletesTheFirstOrderUsingtheDeleteAction(MockServer mockServer)
128 RestTemplate restTemplate =
129 new RestTemplateBuilder()
130 .rootUri(mockServer.getUrl())
134 restTemplate.getForEntity("/", String.class);
135 restTemplate.getForEntity("/orders", String.class);
136 restTemplate.delete("/orders/1234");
140 fail("Unexpected exception", e);