1

There are multiple keys present in x.details[0].user so how to compare in such condition. It works fine where there is only one key while when there are more than one key it fails with error as there are multiple keys for user.

Please guide

 * def array =
 """
  {
      "type": "1",
      "array": 2,
      "details": [
          {
              "path": "path",
              "user": {
                  "school": {
                      "name": [
                          {
                              "value": "this is school",
                              "codeable": {
                                  "details": [
                                      {
                                          "hello": "yty",
                                          "condition": "check1"
                                      }
                                  ],
                                  "text": "123S"
                              }
                          }
                      ]
                  },
                  "sample": "test1",
                  "id": "22222"
              },
              "data": {
                  "check": "abc"
              }
          },
          {
              "path": "path",
              "user": {
                  "school": {
                      "name": [
                          {
                              "value": "this is school",
                              "codeable": {
                                  "details": [
                                      {
                                          "hello": "def",
                                          "condition": "check2"
                                      }
                                  ],
                                  "text": "123O"
                              }
                          }
                      ]
                  },
                  "sample": "test",
                  "id": "11111"
              },
              "data": {
                  "check": "xyz"
              }
          }
      ]
  }
    """

    * def lookup = { 'check1': 'yty', 'check2': 'def' }
    * match each array.details contains { user: { school: { name[0]: { codeable: { details[0]: { hello: '#(lookup[_$.user.school.name[0].codeable.details[0].condition])' } } } } } }

I have tried multiple ways but not sure how to make it work when there are more than one keys getting returned.

Maddy
  • 674
  • 1
  • 7
  • 26

1 Answers1

1

First let me say this. Please, please do not try to be "too clever" in your tests. Read this please: https://stackoverflow.com/a/54126724/143475

Just have simple tests that test for specific, predictable responses and stop there. The maximum complexity should be match contains, that's it. What you are trying to do is in my opinion a waste of time, and will just cause grief for anyone who tries to maintain these tests in future.

That said, I'm giving you a completely different approach here. There are a few different ways, this is just one. Here we attack the problem piece by piece instead of trying to write one massive match.

* def allXyz = array.details.filter(x => x.data.check == 'xyz')
* match each allXyz..details == [{ hello: 'def', condition: 'check2' }]

You should be able to extend this to do all the weird assertions that you want. If you are wondering what allXyz..details does, you can print it like this:

* def temp = $allXyz..details
* print temp
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Good suggestion. Yes, I agree we are already comparing the response schema and there we have defined all enums. Thanks for sharing this Peter. – Maddy Nov 21 '21 at 21:32