Re-staged the contract for `get root`
authorKai Moritz <kai@juplo.de>
Sat, 21 May 2022 18:54:42 +0000 (20:54 +0200)
committerKai Moritz <kai@juplo.de>
Fri, 3 Jun 2022 11:44:53 +0000 (13:44 +0200)
spring-consumer/src/test/java/de/juplo/demos/pact/ContractTest.java
spring-consumer/target/pacts/SpringConsumer-SirenOrderProvider.json

index 16c6815..6262b15 100644 (file)
@@ -20,6 +20,55 @@ import static org.assertj.core.api.Assertions.fail;
 @PactTestFor(providerName = "SirenOrderProvider")
 public class ContractTest
 {
+  @Pact(consumer="SpringConsumer")
+  public RequestResponsePact getRoot(PactDslWithProvider builder)
+  {
+    return builder
+        .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())
+        .toPact();
+  }
+
+  @Test
+  @PactTestFor(pactMethod = "getRoot")
+  public void testGetRoot(MockServer mockServer)
+  {
+    RestTemplate restTemplate =
+        new RestTemplateBuilder()
+            .rootUri(mockServer.getUrl())
+            .build();
+    try
+    {
+      restTemplate.getForEntity("/", String.class);
+    }
+    catch (Exception e)
+    {
+      fail("Unexpected exception", e);
+    }
+  }
+
   @Pact(consumer="SpringConsumer")
   public RequestResponsePact getAllOrders(PactDslWithProvider builder)
   {
index 1d1fbd3..d4dbfff 100644 (file)
         },
         "status": 200
       }
+    },
+    {
+      "description": "get root",
+      "request": {
+        "method": "GET",
+        "path": "/"
+      },
+      "response": {
+        "body": {
+          "class": [
+            "representation"
+          ],
+          "links": [
+            {
+              "href": "http://localhost:8080/orders",
+              "rel": [
+                "orders"
+              ]
+            }
+          ]
+        },
+        "generators": {
+          "body": {
+            "$.links[0].href": {
+              "example": "http://localhost:8080/orders",
+              "regex": ".*\\/(\\Qorders\\E)$",
+              "type": "MockServerURL"
+            }
+          }
+        },
+        "headers": {
+          "Content-Type": "application/vnd.siren+json"
+        },
+        "matchingRules": {
+          "body": {
+            "$.links[0].href": {
+              "combine": "AND",
+              "matchers": [
+                {
+                  "match": "regex",
+                  "regex": ".*\\/(\\Qorders\\E)$"
+                }
+              ]
+            }
+          }
+        },
+        "status": 200
+      }
     }
   ],
   "metadata": {