5

I have to compare my WebService response with its downstream service. But, the IDs in my response and downstream response are not identical. I am giving sample responses below. And again, one is a REST service and another SOAP service, however i can do typeconversion (Thats not an issue)

MyWebService Response:

"myWebServiceResponse": {
"webServiceSummary": {
  "service": {
    "serviceCd": "ABCD",
    "serviceDescription": "Checking Main Service",
    "hypotheticalInd": "100.0",
    "realInd": "200.0"
  },
  "includeServicesList": [
  {
    "serviceCd": "XYZ",
    "serviceDescription": "Checking AddOn Service",
    "hypotheticalInd": "50.0",
    "realInd": "60.0"
 },
 {
    "serviceCd": "PQRS",
    "serviceDescription": "Checking SecondAddOn Service",
    "hypotheticalInd": "100.0",
    "realInd": "200.0"
 }
  ]
    }

Now, below is downstream service response. I cannot use 'match contains' because IDs in myWebServiceResponse and DownstreamService are different and also there are many extra parameters. You can see below.

DownstreamServiceResponse:

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

Now, How am i suppose to match these two responses? Also, you can see that few parameters are random and cannot be compared by moving line by line. Only identical parameters values assigned to CDs/Indicators. And also, I want to know how to extract and match parameters based on one main value. For example, i want to take "serviceCd" : "ABCD" and compare all parametes related to ABCD with that of downstream service.

sn1693
  • 271
  • 2
  • 21

1 Answers1

4

For a simpler example that can give you a better understanding of the concept, especially karate.map() which can even be used on nested JSON structures, see here: https://stackoverflow.com/a/65036047/143475

And also read the docs: https://github.com/intuit/karate#json-transforms

* def response = 
"""
{
   "webServiceSummary":{
      "service":{
         "serviceCd":"ABCD",
         "serviceDescription":"Checking Main Service",
         "hypotheticalInd":"100.0",
         "realInd":"200.0"
      },
      "includeServicesList":[
         {
            "serviceCd":"XYZ",
            "serviceDescription":"Checking AddOn Service",
            "hypotheticalInd":"50.0",
            "realInd":"60.0"
         },
         {
            "serviceCd":"PQRS",
            "serviceDescription":"Checking SecondAddOn Service",
            "hypotheticalInd":"100.0",
            "realInd":"200.0"
         }
      ]
   }
}
"""
* def source =
"""
{
   "webServiceDetail":{
      "feature":{
         "featureCd":"ABCD",
         "featureName":"Checking Main Service",
         "imaginaryInd":"100.0",
         "actualInd":"200.0",
         "extraInd1":"someRandomValue1"
      },
      "includefeatureList":[
         {
            "featureCd":"PQRS",
            "featureName":"Checking SecondAddOn Service",
            "imaginaryInd":"100.0",
            "actualInd":"200.0",
            "extraInd1":"someRandomValue1",
            "extraInd2":"someRandomValue1"
         },
         {
            "featureCd":"XYZ",
            "featureName":"Checking AddOn Service",
            "imaginaryInd":"50.0",
            "actualInd":"60.0",
            "extraInd1":"someRandomValue1",
            "extraInd2":"someRandomValue1"
         }
      ]
   }
}
"""
* def feature = source.webServiceDetail.feature
* set expected.webServiceSummary.service
| path               | value                |
| serviceCd          | feature.featureCd    |
| serviceDescription | feature.featureName  |
| hypotheticalInd    | feature.imaginaryInd |
| realInd            | feature.actualInd    |

* def mapper = function(x){ return { serviceCd: x.featureCd, serviceDescription: x.featureName, hypotheticalInd: x.imaginaryInd, realInd: x.actualInd } }
* def expectedList = karate.map(source.webServiceDetail.includefeatureList, mapper)
* set expected.webServiceSummary.includeServicesList = '#(^expectedList)'
* print expected
* match response == expected
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thanks Peter. Sorry to ask again; can you please direct me to related topic in GitHub for the second part where you used function(x) and karate.map? I kinda understood what you did here; but want to read more about the concept. I was not able to find related topic in Git. Thanks a lot. – sn1693 Nov 05 '18 at 11:33
  • 1
    @sandeephegde here's a tip - in the github page just do CTRL + F and type "karate.map" – Peter Thomas Nov 05 '18 at 12:31
  • @sandeephegde Can you please confirm if the above worked for you? – JCK Feb 05 '20 at 14:49