Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

Karate - Match two dynamic responses

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...