I'm using PACT and Java for contract tests and my issue is that I have an api where the items may come up like this:
[
{
"programCode": "ELA_NGL_G7_TX",
"contentResources": [
{
"tocPosition": 1827,
"contentIdentifier": "l_6bf0783e-8499-4f6c-9f9b-c8fbdc8dcf6b_e5f25016-e2fa-4223-8969-2004c644917d"
},
{
"tocPosition": 1828,
"contentIdentifier": "l_192af774-54b9-4280-87e9-71f2b86a7d4d_e5f25016-e2fa-4223-8969-2004c644917d",
"skills": [
{
"skillId": "ae836bd9-4758-4665-b3f8-8339313363e3",
"spineId": "63c2b7d0-cd69-4e8a-9761-c90623104b8c"
}
]
}
]
So as you can see, sometimes the inner skills array appears others it won't and not sure how to go about incorporating this scenario on my consumer tests. I mean, if the response had or no skills array depending on specific params, I could have two different tests and it might be fine, but here they come from the same call. So I guess what I need is something like an if else, that if the skills array is present then I would assert its inner children, otherwise just ignore it instead.
This is my consumer:
@ExtendWith(PactConsumerTestExt.class)
public class PublishContractWithTocGetSummaryTest {
Map<String, String> headers = new HashMap<>();
String getRecommendations = "/toc/getsummary/ELA_NGL_G7_TX";
@Pact(provider = "CRS-METADATA-FILTERING-SERVICE", consumer = "CRS-TOC-RECOMMENDER")
public RequestResponsePact createPact(PactDslWithProvider builder) throws IOException {
headers.put("Content-Type", "application/json");
DslPart body = new PactDslJsonBody()
.stringValue("programCode", "ELA_NGL_G7_TX")
.eachLike("contentResources")
.integerType("tocPosition", 0)
.stringType("contentIdentifier", "l_9d23cb4f-69dc-4032-bb53-73501234dc14_e5f25016-e2fa-4223-8969-2004c644917d")
.closeArray();
return builder
.given("get TOC Summary")
.uponReceiving("get TOC Summary")
.path(getRecommendations)
.method("GET")
.headers(headers)
.willRespondWith()
.status(200)
.body(body)
.toPact();
}
Many thanks.