1

I am going to work on an api testing framework where I need to create payload and assert dynamically based on a template, which I need to create both positive and negative payloads on the fly and test. Let me explain it further. I have an endpoint that gives rules for another endpoint. Lets call it /api/v1/rules/india

{
  "name": "india",
  "attributes": [
    {
      "fieldName": "name",
      "isRequired": true,
      "minLength":3,
      "maxLength":10,
      "type":"string",
      "regex":"emailRegEx"
    },
    {
      "fieldName": "pinCode",
      "isRequired": false,
      "minLength":3,
      "maxLength":10,
      "type":"string",
      "regex":"emailRegEx"
    }
  ]
}

Lets call it /api/v1/rules/USA
{
  "name": "india",
  "attributes": [
    {
      "fieldName": "name",
      "isRequired": true,
      "minLength":3,
      "maxLength":10,
      "type":"string",
      "regex":"emailRegEx"
    },
    {
      "fieldName": "zipCode",
      "isRequired": false,
      "minLength":3,
      "maxLength":10,
      "type":"string",
      "regex":"emailRegEx"
    }
  ]
}

if you look at the parameter, fields are differnt for the same webservice like when you pass india then one of the field in payload is pincode andwhen you pass USA it changes to Zipcode ( this is just for eferenc ein my case payloads will change significantly for same endpoint) These are the rules for creating a payload for another endpoint. Values for these fields can be changed by the business team say whatever is required True can be changed to Required False tomorrow. So basically I cannot hardcode my payload/test data and also I cannot hardcode my expected condition as it should be derived from that rulebook.

Can you guy suggest to me some approach to solve this problem using Java Restassued or karate or just plain java?

Deepu Nair
  • 165
  • 5
  • 15
  • besides the linked answer (which IMO directly addresses your question with some more reference links), I personally discourage these kinds of "clever" tests because in the long term this leads to unmaintainable tests: https://stackoverflow.com/a/54126724/143475 – Peter Thomas May 04 '22 at 17:53
  • 1
    here's my honest opinion. as the creator of Karate, I don't recommend this approach. when teams have this kind of conflict, even if there are champions like yourself, the others who have some preconceived notions will reluctantly use Karate but all the time complain about it. and as I showed in the link above, the test cases will be worse than if you had used REST-assured or just Java code. finally, everyone in the project will blame it on Karate and tell others not to use it. I've seen this happen a few times. so my advice is - in this case, please use something else – Peter Thomas May 05 '22 at 04:40
  • 1
    I have solved this problem using karate itself and the code is not that messy. Thanks Peter Thomas – Deepu Nair May 12 '22 at 12:58

0 Answers0