Re-staged the contract for `get all orders`
[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 getAllOrders(PactDslWithProvider builder)
25   {
26     return builder
27           .uponReceiving("get all orders")
28             .path("/orders")
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("entity");
38               });
39               body.eachLike("entities", entities ->
40               {
41                 entities.arrayContaining("actions", actionsArray->
42                 {
43                   actionsArray.object(object ->
44                   {
45                     object.stringType("name","update");
46                     object.stringType("method", "PUT");
47                     object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getValue());
48                   });
49                   actionsArray.object(object ->
50                   {
51                     object.stringType("name","delete");
52                     object.stringType("method", "DELETE");
53                     object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getValue());
54                   });
55                 });
56                 entities.array("class", classArray ->
57                 {
58                   classArray.stringValue("entity");
59                 });
60                 entities.array("links", linksArray ->
61                 {
62                   linksArray.object(object->
63                   {
64                     object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getMatcher());
65                     object.array("rel", relArray ->
66                     {
67                       relArray.stringValue("self");
68                     });
69                   });
70                 });
71                 entities.object("properties", object->
72                 {
73                   object.integerType("id", 1234);
74                 });
75                 entities.array("rel", relArray ->
76                 {
77                   relArray.stringValue("item");
78                 });
79               });
80               body.array("links", linksArray ->
81               {
82                 linksArray.object(object->
83                 {
84                   object.matchUrl2("href", "orders");
85                   object.array("rel", relArray ->
86                   {
87                     relArray.stringValue("self");
88                   });
89                 });
90               });
91             }).build())
92         .toPact();
93   }
94
95   @Test
96   @PactTestFor(pactMethod = "getAllOrders")
97   public void testGetExistingUserByEmail(MockServer mockServer)
98   {
99     RestTemplate restTemplate =
100         new RestTemplateBuilder()
101             .rootUri(mockServer.getUrl())
102             .build();
103     try
104     {
105       restTemplate.getForEntity("/orders", String.class);
106     }
107     catch (Exception e)
108     {
109       fail("Unexpected exception", e);
110     }
111   }
112 }