From 842f6c421190e7654640b7ec7d65ae976684264c Mon Sep 17 00:00:00 2001 From: buildmaster Date: Fri, 18 Aug 2017 18:44:06 +0000 Subject: [PATCH] Sync docs from master to gh-pages --- spring-cloud-contract.html | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/spring-cloud-contract.html b/spring-cloud-contract.html index 3af9353bc2..1c07ad2596 100644 --- a/spring-cloud-contract.html +++ b/spring-cloud-contract.html @@ -7511,6 +7511,75 @@ assertions and the one from matchers with an and section):

''' +
+ + + + + +
+ + +If you use a matcher then the part of the request / response that the matcher is addressing +via the JSON Path will get removed from assertion. In case of verifying a collection you have to create +matchers for all elements of the collection. +
+
+
+

Let’s look at the following example:

+
+
+
+
Contract.make {
+    request {
+        method 'GET'
+        url("/foo")
+    }
+    response {
+        status 200
+        body(events: [[
+                                 operation          : 'EXPORT',
+                                 eventId            : '16f1ed75-0bcc-4f0d-a04d-3121798faf99',
+                                 status             : 'OK'
+                         ], [
+                                 operation          : 'INPUT_PROCESSING',
+                                 eventId            : '3bb4ac82-6652-462f-b6d1-75e424a0024a',
+                                 status             : 'OK'
+                         ]
+                ]
+        )
+        testMatchers {
+            jsonPath('$.events[0].operation', byRegex('.+'))
+            jsonPath('$.events[0].eventId', byRegex('^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})$'))
+            jsonPath('$.events[0].status', byRegex('.+'))
+        }
+    }
+}
+
+
+
+

This will lead in creating the following test (showing just the assertion section)

+
+
+
+
and:
+	DocumentContext parsedJson = JsonPath.parse(response.body.asString())
+	assertThatJson(parsedJson).array("['events']").contains("['eventId']").isEqualTo("16f1ed75-0bcc-4f0d-a04d-3121798faf99")
+	assertThatJson(parsedJson).array("['events']").contains("['operation']").isEqualTo("EXPORT")
+	assertThatJson(parsedJson).array("['events']").contains("['operation']").isEqualTo("INPUT_PROCESSING")
+	assertThatJson(parsedJson).array("['events']").contains("['eventId']").isEqualTo("3bb4ac82-6652-462f-b6d1-75e424a0024a")
+	assertThatJson(parsedJson).array("['events']").contains("['status']").isEqualTo("OK")
+and:
+	assertThat(parsedJson.read("\$.events[0].operation", String.class)).matches(".+")
+	assertThat(parsedJson.read("\$.events[0].eventId", String.class)).matches("^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})\$")
+	assertThat(parsedJson.read("\$.events[0].status", String.class)).matches(".+")
+
+
+
+

As you can see the assertion is malformed. That’s because only the first element of the array got asserted. +In order to fix this it’s best to apply the assertion to the whole $.events collection and assert it +via the byCommand(…​) method.

+