Re-staged the contract for `get all orders`
authorKai Moritz <kai@juplo.de>
Fri, 3 Jun 2022 11:37:38 +0000 (13:37 +0200)
committerKai Moritz <kai@juplo.de>
Fri, 3 Jun 2022 11:40:33 +0000 (13:40 +0200)
spring-consumer/src/test/java/de/juplo/demos/pact/ContractTest.java
spring-consumer/target/pacts/SpringConsumer-Siren Order Provider.json [deleted file]
spring-consumer/target/pacts/SpringConsumer-SirenOrderProvider.json [new file with mode: 0644]

index 8db0dbc..16c6815 100644 (file)
@@ -1,10 +1,7 @@
 package de.juplo.demos.pact;
 
 import au.com.dius.pact.consumer.MockServer;
-import au.com.dius.pact.consumer.dsl.LambdaDsl;
-import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
-import au.com.dius.pact.consumer.dsl.PactDslJsonRootValue;
-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;
@@ -20,11 +17,11 @@ 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 getAllOrders(PactDslWithProvider builder)
   {
     return builder
           .uponReceiving("get all orders")
@@ -35,18 +32,68 @@ public class ContractTest
             .headers(Map.of("Content-Type", "application/vnd.siren+json"))
             .body(LambdaDsl.newJsonBody(body ->
             {
-              body.stringType("name");
-              body.booleanType("happy");
-              // body.hexValue("hexCode");
-              body.id();
-              body.ipV4Address("localAddress");
-              body.numberValue("age", 100);
+              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())
         .toPact();
   }
 
   @Test
-  @PactTestFor(pactMethod = "getOrders")
+  @PactTestFor(pactMethod = "getAllOrders")
   public void testGetExistingUserByEmail(MockServer mockServer)
   {
     RestTemplate restTemplate =
diff --git a/spring-consumer/target/pacts/SpringConsumer-Siren Order Provider.json b/spring-consumer/target/pacts/SpringConsumer-Siren Order Provider.json
deleted file mode 100644 (file)
index 7b55f1e..0000000
+++ /dev/null
@@ -1,88 +0,0 @@
-{
-  "consumer": {
-    "name": "SpringConsumer"
-  },
-  "interactions": [
-    {
-      "description": "get all orders",
-      "request": {
-        "method": "GET",
-        "path": "/orders"
-      },
-      "response": {
-        "body": {
-          "age": 100,
-          "happy": true,
-          "id": 1234567890,
-          "localAddress": "127.0.0.1",
-          "name": "string"
-        },
-        "generators": {
-          "body": {
-            "$.id": {
-              "max": 2147483647,
-              "min": 0,
-              "type": "RandomInt"
-            },
-            "$.name": {
-              "size": 20,
-              "type": "RandomString"
-            }
-          }
-        },
-        "headers": {
-          "Content-Type": "application/vnd.siren+json"
-        },
-        "matchingRules": {
-          "body": {
-            "$.happy": {
-              "combine": "AND",
-              "matchers": [
-                {
-                  "match": "type"
-                }
-              ]
-            },
-            "$.id": {
-              "combine": "AND",
-              "matchers": [
-                {
-                  "match": "type"
-                }
-              ]
-            },
-            "$.localAddress": {
-              "combine": "AND",
-              "matchers": [
-                {
-                  "match": "regex",
-                  "regex": "(\\d{1,3}\\.)+\\d{1,3}"
-                }
-              ]
-            },
-            "$.name": {
-              "combine": "AND",
-              "matchers": [
-                {
-                  "match": "type"
-                }
-              ]
-            }
-          }
-        },
-        "status": 200
-      }
-    }
-  ],
-  "metadata": {
-    "pact-jvm": {
-      "version": "4.2.2"
-    },
-    "pactSpecification": {
-      "version": "3.0.0"
-    }
-  },
-  "provider": {
-    "name": "Siren Order Provider"
-  }
-}
diff --git a/spring-consumer/target/pacts/SpringConsumer-SirenOrderProvider.json b/spring-consumer/target/pacts/SpringConsumer-SirenOrderProvider.json
new file mode 100644 (file)
index 0000000..1d1fbd3
--- /dev/null
@@ -0,0 +1,223 @@
+{
+  "consumer": {
+    "name": "SpringConsumer"
+  },
+  "interactions": [
+    {
+      "description": "get all orders",
+      "request": {
+        "method": "GET",
+        "path": "/orders"
+      },
+      "response": {
+        "body": {
+          "class": [
+            "entity"
+          ],
+          "entities": [
+            {
+              "actions": [
+                {
+                  "href": "http://localhost:8080/orders/1234",
+                  "method": "PUT",
+                  "name": "update"
+                },
+                {
+                  "href": "http://localhost:8080/orders/1234",
+                  "method": "DELETE",
+                  "name": "delete"
+                }
+              ],
+              "class": [
+                "entity"
+              ],
+              "links": [
+                {
+                  "href": "http://localhost:8080/orders/1234",
+                  "rel": [
+                    "self"
+                  ]
+                }
+              ],
+              "properties": {
+                "id": 1234
+              },
+              "rel": [
+                "item"
+              ]
+            }
+          ],
+          "links": [
+            {
+              "href": "http://localhost:8080/orders",
+              "rel": [
+                "self"
+              ]
+            }
+          ]
+        },
+        "generators": {
+          "body": {
+            "$.entities[*].links[0].href": {
+              "example": "http://localhost:8080/orders/1234",
+              "regex": ".*\\/(\\Qorders\\E\\/\\d+)$",
+              "type": "MockServerURL"
+            },
+            "$.entities[*][0].href": {
+              "example": "http://localhost:8080/orders/1234",
+              "regex": ".*\\/(\\Qorders\\E\\/\\Q1234\\E)$",
+              "type": "MockServerURL"
+            },
+            "$.entities[*][1].href": {
+              "example": "http://localhost:8080/orders/1234",
+              "regex": ".*\\/(\\Qorders\\E\\/\\Q1234\\E)$",
+              "type": "MockServerURL"
+            },
+            "$.links[0].href": {
+              "example": "http://localhost:8080/orders",
+              "regex": ".*\\/(\\Qorders\\E)$",
+              "type": "MockServerURL"
+            }
+          }
+        },
+        "headers": {
+          "Content-Type": "application/vnd.siren+json"
+        },
+        "matchingRules": {
+          "body": {
+            "$.entities": {
+              "combine": "AND",
+              "matchers": [
+                {
+                  "match": "type"
+                }
+              ]
+            },
+            "$.entities[*].actions": {
+              "combine": "AND",
+              "matchers": [
+                {
+                  "match": "arrayContains",
+                  "variants": [
+                    {
+                      "generators": {
+                        "$.href": {
+                          "example": "http://localhost:8080/orders/1234",
+                          "regex": ".*\\/(\\Qorders\\E\\/\\Q1234\\E)$",
+                          "type": "MockServerURL"
+                        }
+                      },
+                      "index": 0,
+                      "rules": {
+                        "$.href": {
+                          "combine": "AND",
+                          "matchers": [
+                            {
+                              "match": "regex",
+                              "regex": ".*\\/(\\Qorders\\E\\/\\Q1234\\E)$"
+                            }
+                          ]
+                        },
+                        "$.method": {
+                          "combine": "AND",
+                          "matchers": [
+                            {
+                              "match": "type"
+                            }
+                          ]
+                        },
+                        "$.name": {
+                          "combine": "AND",
+                          "matchers": [
+                            {
+                              "match": "type"
+                            }
+                          ]
+                        }
+                      }
+                    },
+                    {
+                      "generators": {
+                        "$.href": {
+                          "example": "http://localhost:8080/orders/1234",
+                          "regex": ".*\\/(\\Qorders\\E\\/\\Q1234\\E)$",
+                          "type": "MockServerURL"
+                        }
+                      },
+                      "index": 1,
+                      "rules": {
+                        "$.href": {
+                          "combine": "AND",
+                          "matchers": [
+                            {
+                              "match": "regex",
+                              "regex": ".*\\/(\\Qorders\\E\\/\\Q1234\\E)$"
+                            }
+                          ]
+                        },
+                        "$.method": {
+                          "combine": "AND",
+                          "matchers": [
+                            {
+                              "match": "type"
+                            }
+                          ]
+                        },
+                        "$.name": {
+                          "combine": "AND",
+                          "matchers": [
+                            {
+                              "match": "type"
+                            }
+                          ]
+                        }
+                      }
+                    }
+                  ]
+                }
+              ]
+            },
+            "$.entities[*].links[0].href": {
+              "combine": "AND",
+              "matchers": [
+                {
+                  "match": "regex",
+                  "regex": ".*\\/(\\Qorders\\E\\/\\d+)$"
+                }
+              ]
+            },
+            "$.entities[*].properties.id": {
+              "combine": "AND",
+              "matchers": [
+                {
+                  "match": "integer"
+                }
+              ]
+            },
+            "$.links[0].href": {
+              "combine": "AND",
+              "matchers": [
+                {
+                  "match": "regex",
+                  "regex": ".*\\/(\\Qorders\\E)$"
+                }
+              ]
+            }
+          }
+        },
+        "status": 200
+      }
+    }
+  ],
+  "metadata": {
+    "pact-jvm": {
+      "version": "4.2.2"
+    },
+    "pactSpecification": {
+      "version": "3.0.0"
+    }
+  },
+  "provider": {
+    "name": "SirenOrderProvider"
+  }
+}