Reputation: 129
I have a scenario where a portion of the Parent API response is from a child API. If the child API response(which is dynamic) has only one array element then no need to match that in parent API response, if array size > 1
then I need to match from index 1 on wards with parent API response.
* def child = {"array1":[{"mbr1":{"id":"A1"}},{"mbr2":{"id":"A2"}}]}
There is no specific order for the child api response and array1
can have "n"
number of array elements(mbr1,mbr2,mbr3, etc)
If the child API response is like above then the parent will look like below:
* def parent = {"parent":{"muid":"1234"},"elg":[{"EID":"E123"},{"members":[{"mbr2":{"id":"A2"}}]}]}
So in parent API response towards the end child api response is populated, only if the above mentioned conditions are satisfied. If child API returns only one element then the parent API response will look like the below:
* def parent = {"parent":{"muid":"1234"},"elg":[{"EID":"E123"}]}
So how do I do a match to see whether the child is present in parent if child returns 200 OK and the child array length > 1? So I am looking for a solution for the below scenario:
if (child responseStatus == 200){
if (child.array1.length > 1){
for (i = 1;i <= child.array1.length; i++){
parent.elg[1].members[i] contains child.arrays1[i]
}
}
}
Upvotes: 1
Views: 411
Reputation: 58058
Here you go:
* copy last = child.array1
* remove last[0]
* def multi = {"parent":{"muid":"1234"},"elg":[{"EID":"E123"},{"members":"#(last)"}]}
* def expected = child.array1.length == 1 ? {"parent":{"muid":"1234"},"elg":[{"EID":"E123"}]} : multi
* match parent == expected
Now I'm quite sure you (or anyone else in your team) will have a really hard time understanding how on earth that works. Which brings me to the advice I have for you.
Most teams don't do this kind of dynamic complex gymnastics. That is not how you should approach tests.
You have 2 scenarios. One case where your have 1 element in child. And other cases.
Please write two separate Scenario
-s and make sure the data you pass to whatever service is guaranteed to return a specific shape. If you can't do this, it probably means it is so dynamic, there is no point in you testing it - maybe it should not be the focus of your validation in the first place.
So your test becomes simple.
Scenario: first case
# send request1, get child and parent
# match parent == {"parent":{"muid":"1234"},"elg":[{"EID":"E123"}]}
Scenario: second case
# send request2, get child and parent
# match parent == {"parent":{"muid":"1234"},"elg":[{"EID":"E123"},{"members":[{"mbr2":{"id":"A2"}}]}]}
For another example of how to simplify bad tests, see this: https://stackoverflow.com/a/54126724/143475
Upvotes: 2