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                 body.arrayContaining("actions", actionsArray ->
42                 {
43                   actionsArray.object(object ->
44                   {
45                     object.stringType("name","update");
46                     object.stringType("method", "PUT");
47                     object.matchUrl2("href", 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", Matchers.regexp("\\d+", "1234").getValue());
54                   });
55                 });
56                 body.array("links", linksArray ->
57                 {
58                   linksArray.object(object->
59                   {
60                     object.matchUrl2("href", "orders");
61                     object.array("rel", relArray ->
62                     {
63                       relArray.stringValue("self");
64                     });
65                   });
66                 });
67               });
68             }).build())
69         .toPact();
70   }
71
72   @Test
73   @PactTestFor(pactMethod = "getOrders")
74   public void testGetExistingUserByEmail(MockServer mockServer)
75   {
76     RestTemplate restTemplate =
77         new RestTemplateBuilder()
78             .rootUri(mockServer.getUrl())
79             .build();
80     try
81     {
82       restTemplate.getForEntity("/orders", String.class);
83     }
84     catch (Exception e)
85     {
86       fail("Unexpected exception", e);
87     }
88   }
89 }