WIP
[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 = "Siren Order Provider")
21 public class ContractTest
22 {
23   @Pact(consumer="SpringConsumer")
24   public RequestResponsePact getOrders(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.array("entities", entities ->
40               {
41                 entities.eachLike(entity ->
42                 {
43                   entity.arrayContaining("actions", actionsArray->
44                   {
45                     actionsArray.object(object ->
46                     {
47                       object.stringType("name","update");
48                       object.stringType("method", "PUT");
49                       object.matchUrl2("href", Matchers.regexp("\\d+", "1234").getValue());
50                     });
51                     actionsArray.object(object ->
52                     {
53                       object.stringType("name","delete");
54                       object.stringType("method", "DELETE");
55                       object.matchUrl2("href", Matchers.regexp("\\d+", "1234").getValue());
56                     });
57                   });
58                   entity.array("class", classArray ->
59                   {
60                     classArray.stringValue("entity");
61                   });
62                   entity.array("links", linksArray ->
63                   {
64                     linksArray.object(object->
65                     {
66                       object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234"));
67                       object.array("rel", relArray ->
68                       {
69                         relArray.stringValue("self");
70                       });
71                     });
72                   });
73                   entity.object("properties", object->
74                   {
75                     object.stringType("id", "1234");
76                     object.array("rel", relArray ->
77                     {
78                       relArray.stringValue("item");
79                     });
80                   });
81                 });
82               });
83               body.array("links", linksArray ->
84               {
85                 linksArray.object(object->
86                 {
87                   object.matchUrl2("href", "orders");
88                   object.array("rel", relArray ->
89                   {
90                     relArray.stringValue("self");
91                   });
92                 });
93               });
94             }).build())
95         .toPact();
96   }
97
98   @Test
99   @PactTestFor(pactMethod = "getOrders")
100   public void testGetExistingUserByEmail(MockServer mockServer)
101   {
102     RestTemplate restTemplate =
103         new RestTemplateBuilder()
104             .rootUri(mockServer.getUrl())
105             .build();
106     try
107     {
108       restTemplate.getForEntity("/orders", String.class);
109     }
110     catch (Exception e)
111     {
112       fail("Unexpected exception", e);
113     }
114   }
115 }