-
Unresolved directive in verifier/introduction.adoc - include::https://raw.githubusercontent.com/spring-cloud/spring-cloud-contract/master/samples/standalone/dsl/http-server/src/test/resources/contracts/shouldMarkClientAsFraud.groovy[]
+package contracts
-The Contract is written using a statically typed Groovy DSL. You might be wondering what are those
-`value(client(...), server(...))` parts. By using this notation Spring Cloud Contract allows you to
-define parts of a JSON / URL / etc. which are dynamic. In case of an identifier or a timestamp you
-don't want to hardcode a value. You want to allow some different ranges of values. That's why for
-the consumer side you can set regular expressions matching those values. You can provide the body
-either by means of a map notation or String with interpolations.
-https://cloud.spring.io/spring-cloud-contract/spring-cloud-contract.html#_contract_dsl[Consult the docs
-for more information.] We highly recommend using the map notation!
+org.springframework.cloud.contract.spec.Contract.make {
+ request { // (1)
+ method 'PUT' // (2)
+ url '/fraudcheck' // (3)
+ body([ // (4)
+ clientId: value(consumer(regex('[0-9]{10}'))),
+ loanAmount: 99999
+ ])
+ headers { // (5)
+ header('Content-Type', 'application/vnd.fraud.v1+json')
+ }
+ }
+ response { // (6)
+ status 200 // (7)
+ body([ // (8)
+ fraudCheckStatus: "FRAUD",
+ rejectionReason: "Amount too high"
+ ])
+ headers { // (9)
+ header('Content-Type': value(
+ producer(regex('application/vnd.fraud.v1.json.*')),
+ consumer('application/vnd.fraud.v1+json'))
+ )
+ }
+ }
+}
-TIP: It's really important that you understand the map notation to set up contracts. Please read the
-http://groovy-lang.org/json.html[Groovy docs regarding JSON]
+/*
+Since we don't want to force on the user to hardcode values of fields that are dynamic
+(timestamps, database ids etc.), one can provide parametrize those entries by using the
+`value(consumer(...), producer(...))` method. That way what's present in the `consumer`
+section will end up in the produced stub. What's there in the `producer` will end up in the
+autogenerated test. If you provide only the regular expression side without the concrete
+value then Spring Cloud Contract will generate one for you.
-The aforementioned contract is an agreement between two sides that:
+From the Consumer perspective, when shooting a request in the integration test:
-- if an HTTP request is sent with
-** a method `PUT` on an endpoint `/fraudcheck`
-** JSON body with `clientId` matching the regular expression `[0-9]{10}` and `loanAmount` equal to `99999`
-** and with a header `Content-Type` equal to `application/vnd.fraud.v1+json`
-- then an HTTP response would be sent to the consumer that
-** has status `200`
-** contains JSON body with the `fraudCheckStatus` field containing a value `FRAUD` and the `rejectionReason` field having value `Amount too high`
-** and a `Content-Type` header with a value of `application/vnd.fraud.v1+json`
+(1) - If the consumer sends a request
+(2) - With the "PUT" method
+(3) - to the URL "/fraudcheck"
+(4) - with the JSON body that
+ * has a field `clientId` that matches a regular expression `[0-9]{10}`
+ * has a field `loanAmount` that is equal to `99999`
+(5) - with header `Content-Type` equal to `application/vnd.fraud.v1+json`
+(6) - then the response will be sent with
+(7) - status equal `200`
+(8) - and JSON body equal to
+ { "fraudCheckStatus": "FRAUD", "rejectionReason": "Amount too high" }
+(9) - with header `Content-Type` equal to `application/vnd.fraud.v1+json`
-Once we're ready to check the API in practice in the integration tests we need to just install the stubs locally
+From the Producer perspective, in the autogenerated producer-side test:
-*add the Spring Cloud Contract Verifier plugin*
-
-We can add either Maven or Gradle plugin - in this example we'll show how to add Maven. First we need to add the `Spring Cloud Contract` BOM.
-
-[source,xml,indent=0]
+(1) - A request will be sent to the producer
+(2) - With the "PUT" method
+(3) - to the URL "/fraudcheck"
+(4) - with the JSON body that
+ * has a field `clientId` that will have a generated value that matches a regular expression `[0-9]{10}`
+ * has a field `loanAmount` that is equal to `99999`
+(5) - with header `Content-Type` equal to `application/vnd.fraud.v1+json`
+(6) - then the test will assert if the response has been sent with
+(7) - status equal `200`
+(8) - and JSON body equal to
+ { "fraudCheckStatus": "FRAUD", "rejectionReason": "Amount too high" }
+(9) - with header `Content-Type` matching `application/vnd.fraud.v1+json.*`
+ */