Fixed an issue with the matching of the URIs for `actions`
[demos/example-siren] / spring-consumer / src / test / java / de / juplo / demos / pact / ContractTest.java
1 package de.juplo.demos.pact;
2
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;
13
14 import java.util.Map;
15
16 import static org.assertj.core.api.Assertions.fail;
17
18
19 @ExtendWith(PactConsumerTestExt.class)
20 @PactTestFor(providerName = "SirenOrderProvider")
21 public class ContractTest
22 {
23   @Pact(consumer="SpringConsumer")
24   public RequestResponsePact deletesTheFirstOrderUsingtheDeleteAction(PactDslWithProvider builder)
25   {
26     return builder
27         .uponReceiving("get root")
28         .path("/")
29         .method("GET")
30         .willRespondWith()
31         .status(200)
32         .headers(Map.of("Content-Type", "application/vnd.siren+json"))
33         .body(LambdaDsl.newJsonBody(body ->
34         {
35           body.array("class", classArray ->
36           {
37             classArray.stringValue("representation");
38           });
39           body.array("links", linksArray ->
40           {
41             linksArray.object(object->
42             {
43               object.matchUrl2("href", "orders");
44               object.array("rel", relArray ->
45               {
46                 relArray.stringValue("orders");
47               });
48             });
49           });
50         }).build())
51         .uponReceiving("get all orders")
52         .path("/orders")
53         .method("GET")
54         .willRespondWith()
55         .status(200)
56         .headers(Map.of("Content-Type", "application/vnd.siren+json"))
57         .body(LambdaDsl.newJsonBody(body ->
58         {
59           body.array("class", classArray ->
60           {
61             classArray.stringValue("entity");
62           });
63           body.eachLike("entities", entities ->
64           {
65             entities.arrayContaining("actions", actionsArray->
66             {
67               actionsArray.object(object ->
68               {
69                 object.stringValue("name","update");
70                 object.stringValue("method", "PUT");
71                 object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getMatcher());
72               });
73               actionsArray.object(object ->
74               {
75                 object.stringValue("name","delete");
76                 object.stringValue("method", "DELETE");
77                 object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getMatcher());
78               });
79             });
80             entities.array("class", classArray ->
81             {
82               classArray.stringValue("entity");
83             });
84             entities.array("links", linksArray ->
85             {
86               linksArray.object(object->
87               {
88                 object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getMatcher());
89                 object.array("rel", relArray ->
90                 {
91                   relArray.stringValue("self");
92                 });
93               });
94             });
95             entities.object("properties", object->
96             {
97               object.integerType("id", 1234);
98             });
99             entities.array("rel", relArray ->
100             {
101               relArray.stringValue("item");
102             });
103           });
104           body.array("links", linksArray ->
105           {
106             linksArray.object(object->
107             {
108               object.matchUrl2("href", "orders");
109               object.array("rel", relArray ->
110               {
111                 relArray.stringValue("self");
112               });
113             });
114           });
115         }).build())
116         .uponReceiving("delete order")
117         .matchPath("/orders/\\d+", "/orders/1234")
118         .method("DELETE")
119         .willRespondWith()
120         .status(200)
121         .toPact();
122   }
123
124   @Test
125   @PactTestFor(pactMethod = "deletesTheFirstOrderUsingtheDeleteAction")
126   public void testDeletesTheFirstOrderUsingtheDeleteAction(MockServer mockServer)
127   {
128     RestTemplate restTemplate =
129         new RestTemplateBuilder()
130             .rootUri(mockServer.getUrl())
131             .build();
132     try
133     {
134       restTemplate.getForEntity("/", String.class);
135       restTemplate.getForEntity("/orders", String.class);
136       restTemplate.delete("/orders/1234");
137     }
138     catch (Exception e)
139     {
140       fail("Unexpected exception", e);
141     }
142   }
143 }