Defined all contracts in a single pact, like in the original example
[demos/example-siren] / spring-consumer / src / test / java / de / juplo / demos / pact / ContractTest.java
index d7358d8..a148b60 100644 (file)
@@ -1,8 +1,7 @@
 package de.juplo.demos.pact;
 
 import au.com.dius.pact.consumer.MockServer;
-import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
-import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
+import au.com.dius.pact.consumer.dsl.*;
 import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
 import au.com.dius.pact.consumer.junit5.PactTestFor;
 import au.com.dius.pact.core.model.RequestResponsePact;
@@ -18,33 +17,113 @@ import static org.assertj.core.api.Assertions.fail;
 
 
 @ExtendWith(PactConsumerTestExt.class)
-@PactTestFor(providerName = "Siren Order Provider")
+@PactTestFor(providerName = "SirenOrderProvider")
 public class ContractTest
 {
   @Pact(consumer="SpringConsumer")
-  public RequestResponsePact getOrders(PactDslWithProvider builder)
+  public RequestResponsePact deletesTheFirstOrderUsingtheDeleteAction(PactDslWithProvider builder)
   {
-    PactDslJsonBody body = new PactDslJsonBody()
-        .stringType("name")
-        .booleanType("happy")
-        .hexValue("hexCode")
-        .id()
-        .ipAddress("localAddress")
-        .numberValue("age", 100);
     return builder
-          .uponReceiving("get all orders")
-            .path("/orders")
-            .method("GET")
-          .willRespondWith()
-            .status(200)
-            .headers(Map.of("Content-Type", "application/vnd.siren+json"))
-            .body(body)
+        .uponReceiving("get root")
+        .path("/")
+        .method("GET")
+        .willRespondWith()
+        .status(200)
+        .headers(Map.of("Content-Type", "application/vnd.siren+json"))
+        .body(LambdaDsl.newJsonBody(body ->
+        {
+          body.array("class", classArray ->
+          {
+            classArray.stringValue("representation");
+          });
+          body.array("links", linksArray ->
+          {
+            linksArray.object(object->
+            {
+              object.matchUrl2("href", "orders");
+              object.array("rel", relArray ->
+              {
+                relArray.stringValue("orders");
+              });
+            });
+          });
+        }).build())
+        .uponReceiving("get all orders")
+        .path("/orders")
+        .method("GET")
+        .willRespondWith()
+        .status(200)
+        .headers(Map.of("Content-Type", "application/vnd.siren+json"))
+        .body(LambdaDsl.newJsonBody(body ->
+        {
+          body.array("class", classArray ->
+          {
+            classArray.stringValue("entity");
+          });
+          body.eachLike("entities", entities ->
+          {
+            entities.arrayContaining("actions", actionsArray->
+            {
+              actionsArray.object(object ->
+              {
+                object.stringType("name","update");
+                object.stringType("method", "PUT");
+                object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getValue());
+              });
+              actionsArray.object(object ->
+              {
+                object.stringType("name","delete");
+                object.stringType("method", "DELETE");
+                object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getValue());
+              });
+            });
+            entities.array("class", classArray ->
+            {
+              classArray.stringValue("entity");
+            });
+            entities.array("links", linksArray ->
+            {
+              linksArray.object(object->
+              {
+                object.matchUrl2("href", "orders", Matchers.regexp("\\d+", "1234").getMatcher());
+                object.array("rel", relArray ->
+                {
+                  relArray.stringValue("self");
+                });
+              });
+            });
+            entities.object("properties", object->
+            {
+              object.integerType("id", 1234);
+            });
+            entities.array("rel", relArray ->
+            {
+              relArray.stringValue("item");
+            });
+          });
+          body.array("links", linksArray ->
+          {
+            linksArray.object(object->
+            {
+              object.matchUrl2("href", "orders");
+              object.array("rel", relArray ->
+              {
+                relArray.stringValue("self");
+              });
+            });
+          });
+        }).build())
+        .uponReceiving("delete order")
+        .matchPath("/orders/\\d+", "/orders/1234")
+        .method("DELETE")
+        .willRespondWith()
+        .status(200)
         .toPact();
   }
 
   @Test
-  @PactTestFor(pactMethod = "getOrders")
-  public void testGetExistingUserByEmail(MockServer mockServer)
+  @PactTestFor(pactMethod = "deletesTheFirstOrderUsingtheDeleteAction")
+  public void testDeletesTheFirstOrderUsingtheDeleteAction(MockServer mockServer)
   {
     RestTemplate restTemplate =
         new RestTemplateBuilder()
@@ -52,7 +131,9 @@ public class ContractTest
             .build();
     try
     {
+      restTemplate.getForEntity("/", String.class);
       restTemplate.getForEntity("/orders", String.class);
+      restTemplate.delete("/orders/1234");
     }
     catch (Exception e)
     {