I am working on spring cloud contract and have a use-case where I want the response to be BAD_REQUEST when a few query parameters are missing and OK when all the mandatory query parameter are present. In order to achieve this, I have different contracts(groovy files) to cater each of such request/responses. But when I test from the consumer(with/without query parameters) all of these match to only 1 contract whether or the query parameter is present.
Contracts listed below,
Both the query parameters are not present
Contract.make {
description("Test to see if authorisation works")
request {
method 'GET'
urlPath("/check/validate")
headers {
contentType('application/json')
}
}
response {
status(400)
"body" "shouldReturnBadRequest400BothFieldsNotPresent"
headers {
contentType('application/json')
}
}
}
Query parameters B is not present
Contract.make {
description("Test to see if authorisation works")
request {
method 'GET'
urlPath("/check/validate") {
queryParameters {
parameter 'a' : value(regex(nonBlank()))
}
}
headers {
contentType('application/json')
}
}
response {
status(400)
"body" "shouldReturnBadRequest400BNotPresent"
headers {
contentType('application/json')
}
}
}
Query Parameters are present and response is OK
Contract.make {
description("Test to see if authorisation works")
request {
method 'GET'
urlPath('/check/validate') {
queryParameters {
parameter 'a' : equalTo("AUTHORISED")
parameter 'b' : value(regex(nonBlank()))
}
}
headers {
contentType('application/json')
}
}
response {
status(200)
"body" "shouldReturn200OkValidRequest"
headers {
contentType('application/json')
}
}
}
Questions:
All of my testcases match to the first case Both the query parameters are not present rather than matching to specifics. Please let me know what I am missing.
Can someone please point to the spring-cloud-contract documentation on query parameters which cater the above requirement. Found this(http://cloud.spring.io/spring-cloud-contract/1.0.x/#_passing_optional_parameters) but I need some more information on my above requirement.
Your contracts are extending one another. Each is more concrete than the other. You have to use the priority()
method to say which one is more concrete than the other. E.g. the least concrete should have the highest value priority - priority(100)
. The most - priority(1)
.
Both the query parameters are not present
Contract.make {
priority(100)
description("Test to see if authorisation works")
request {
method 'GET'
urlPath("/check/validate")
headers {
contentType('application/json')
}
}
response {
status(400)
"body" "shouldReturnBadRequest400BothFieldsNotPresent"
headers {
contentType('application/json')
}
}
}
Query parameters B is not present
Contract.make {
priority(50)
description("Test to see if authorisation works")
request {
method 'GET'
urlPath("/check/validate") {
queryParameters {
parameter 'a' : value(regex(nonBlank()))
}
}
headers {
contentType('application/json')
}
}
response {
status(400)
"body" "shouldReturnBadRequest400BNotPresent"
headers {
contentType('application/json')
}
}
}
Query Parameters are present and response is OK
Contract.make {
priority(10)
description("Test to see if authorisation works")
request {
method 'GET'
urlPath('/check/validate') {
queryParameters {
parameter 'a' : equalTo("AUTHORISED")
parameter 'b' : value(regex(nonBlank()))
}
}
headers {
contentType('application/json')
}
}
response {
status(200)
"body" "shouldReturn200OkValidRequest"
headers {
contentType('application/json')
}
}
}