6262b15193ccd7f9d2d21fa60db5062efc4cb1dc
[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 getRoot(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         .toPact();
52   }
53
54   @Test
55   @PactTestFor(pactMethod = "getRoot")
56   public void testGetRoot(MockServer mockServer)
57   {
58     RestTemplate restTemplate =
59         new RestTemplateBuilder()
60             .rootUri(mockServer.getUrl())
61             .build();
62     try
63     {
64       restTemplate.getForEntity("/", String.class);
65     }
66     catch (Exception e)
67     {
68       fail("Unexpected exception", e);
69     }
70   }
71
72   @Pact(consumer="SpringConsumer")
73   public RequestResponsePact getAllOrders(PactDslWithProvider builder)
74   {
75     return builder
76           .uponReceiving("get all orders")
77             .path("/orders")
78             .method("GET")
79           .willRespondWith()
80             .status(200)
81             .headers(Map.of("Content-Type", "application/vnd.siren+json"))
82             .body(LambdaDsl.newJsonBody(body ->
83             {
84               body.array("class", classArray ->
85               {
86                 classArray.stringValue("entity");
87               });
88               body.eachLike("entities", entities ->
89               {
90                 entities.arrayContaining("actions", actionsArray->
91                 {
92                   actionsArray.object(object ->
93                   {
94                     object.stringType("name","update");
95                     object.stringType("method", "PUT");
96                     object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getValue());
97                   });
98                   actionsArray.object(object ->
99                   {
100                     object.stringType("name","delete");
101                     object.stringType("method", "DELETE");
102                     object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getValue());
103                   });
104                 });
105                 entities.array("class", classArray ->
106                 {
107                   classArray.stringValue("entity");
108                 });
109                 entities.array("links", linksArray ->
110                 {
111                   linksArray.object(object->
112                   {
113                     object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getMatcher());
114                     object.array("rel", relArray ->
115                     {
116                       relArray.stringValue("self");
117                     });
118                   });
119                 });
120                 entities.object("properties", object->
121                 {
122                   object.integerType("id", 1234);
123                 });
124                 entities.array("rel", relArray ->
125                 {
126                   relArray.stringValue("item");
127                 });
128               });
129               body.array("links", linksArray ->
130               {
131                 linksArray.object(object->
132                 {
133                   object.matchUrl2("href", "orders");
134                   object.array("rel", relArray ->
135                   {
136                     relArray.stringValue("self");
137                   });
138                 });
139               });
140             }).build())
141         .toPact();
142   }
143
144   @Test
145   @PactTestFor(pactMethod = "getAllOrders")
146   public void testGetExistingUserByEmail(MockServer mockServer)
147   {
148     RestTemplate restTemplate =
149         new RestTemplateBuilder()
150             .rootUri(mockServer.getUrl())
151             .build();
152     try
153     {
154       restTemplate.getForEntity("/orders", String.class);
155     }
156     catch (Exception e)
157     {
158       fail("Unexpected exception", e);
159     }
160   }
161 }