Karate - Match two complex shuffled json

2019-07-12 08:56发布

问题:

Below question is very similar to this: Karate - Validate json responses stored in different files I went through suggested contains-shortcuts and could not figure out the answer.

I need to compare two json files but using contains keyword. Why only contains? Because in some cases i need to match only some of the selected fields in json files. Below are the samples and codes.

Json File 1: Test.Json

{
   "webServiceDetail":{
      "feature":{
         "featureCd":"ABCD",
         "imaginaryInd":"100.0",
         "extraInd1":"someRandomValue1"
      },
      "includefeatureList":[
         {
            "featureCd":"PQRS",
            "featureName":"Checking SecondAddOn Service",
            "extraInd1":"someRandomValue1",
            "extraInd2":"someRandomValue1"
         },
         {
            "featureCd":"XYZ",
            "featureName":"Checking AddOn Service",
            "imaginaryInd":"50.0"
         }
      ]
   }
}

Json File 2: Test1.json

{
   "webServiceSummary":{
      "service":{
         "serviceCd":"ABCD"
      },
      "includeServicesList":[
         {
            "serviceCd":"XYZ",
            "serviceDescription": "Checking AddOn Service"
         },
         {
            "serviceDescription":"Checking SecondAddOn Service",
            "serviceCd":"PQRS",
            "randon":"FGDD"
         }
      ]
   }
}

My Code:

* def Test = read('classpath:PP1/data/test.json')
* def Test1 = read('classpath:PP1/data/Test1.json')
* def feature = Test.webServiceDetail.feature
* set expected.webServiceSummary.service
| path               | value                |
| serviceCd          | feature.featureCd    |

* def mapper = function(x){ return { serviceCd: x.featureCd, serviceDescription: x.featureName} }
* def expectedList = karate.map(Test.webServiceDetail.includefeatureList, mapper)
* set expected.webServiceSummary.includeServicesList = '#(^*expectedList)'
* match Test1.webServiceSummary.includeServicesList == expected.webServiceSummary.includeServicesList

Now, above code, perfectly works and i get success response as well. But my concern is I am matching with contains any here. I should verify with contains keyword. Because i need to ensure all the parameters in expected.webServiceSummary.includeServicesList are present in Test1.webServiceSummary.includeServicesList; not any or some of them. I tried using #(^expectedList) -- for contains; but didn't worked out. I know that these series of questions look silly, but i can't figure out the behavior!

回答1:

This will always check that a value contains only all the array elements in expectedList.

'#(^^expectedList)' 

Read the docs: https://github.com/intuit/karate#contains-short-cuts



标签: karate