2

I need to match and validate my JSON response with that of downstream XML response. Here are sample responses for both.

Note that Json response parameters are is not in-order with XML response.

JSON RESPONSE

"Main": {
    "Cd": "ABC",
    "descriptionTxt": "Sample Main",
    "type": "A",
    "codeType": "P",
    "dt": "2018-10-15T00:00:00-05:00",
    "validity": "3",
    "segment": "Personal"
    },
  "testList": [
    {
      "code": "123",
      "descriptionTxt": "My Description",
      "categoryCd": "DUDU"
    },
    {
      "code": "675",
      "descriptionTxt": "His Description"
    },
    {
      "code": "345",
      "descriptionTxt": "Your Description",
      "categoryCd": "BH"
    }
]

XML RESPONSE

<S:Body>
<ns4:code>ABC </ns4:code>
    <ns5:description>Sample Main</ns5:description>
    <ns5:Date>2018-10-15</ns5:Date>
    <ns5:Type>A</ns5:Type>
    <ns5:codeType>P</ns5:codeType>
    <ns5:validity>3</ns5:validity >
    <ns5:Segment>PERSONAL  </ns5:Segment>
    <ns5:unwanted>Unwanted XML Parameter</ns5:unwanted>

    <ns4:Test>
      <ns5:code>123   </ns5:code>
      <ns5:description>My Description</ns5:description>
      <ns5:categoryCode>DUDU</ns5:categoryCode>
      <ns5:unwanted>Unwanted XML Parameter</ns5:unwanted>
    </ns4:Test>

    <ns4:Test>
      <ns5:code>345   </ns5:code>
      <ns5:description>Your Description</ns5:description>
      <ns5:categoryCode>BH</ns5:categoryCode>
    </ns4:Test>

    <ns4:Test>
      <ns5:code>675  </ns5:code>
      <ns5:description>His Description</ns5:description>
      <ns5:unwanted>Unwanted XML Parameter</ns5:unwanted>
    </ns4:Test>

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Nitheesh hunsur
  • 151
  • 2
  • 11

1 Answers1

2

It would have been nice if you took the time to post well-formed JSON and XML, but anyway. I'm focusing on the hard problem here, which is to map repeated XML elements to JSON, if you paste the below into a Scenario you can see it work:

* def json = 
"""
{
   "Main": {
      "Cd":"ABC",
      "descriptionTxt":"Sample Main",
      "type":"A",
      "codeType":"P",
      "dt":"2018-10-15T00:00:00-05:00",
      "validity":"3",
      "segment":"Personal"
   },
   "testList":[
      {
         "code":"123",
         "descriptionTxt":"My Description",
         "categoryCd":"DUDU"
      },
      {
         "code":"675",
         "descriptionTxt":"His Description"
      },
      {
         "code":"345",
         "descriptionTxt":"Your Description",
         "categoryCd":"BH"
      }
   ]
}
"""
* def xml = 
"""
<ns4:root xmlns:ns4="http://foo.com" xmlns:ns5="http://bar.com">
   <ns4:Test>
      <ns5:code>123</ns5:code>
      <ns5:description>My Description</ns5:description>
      <ns5:categoryCode>DUDU</ns5:categoryCode>
      <ns5:unwanted>Unwanted XML Parameter</ns5:unwanted>
   </ns4:Test>
   <ns4:Test>
      <ns5:code>345</ns5:code>
      <ns5:description>Your Description</ns5:description>
      <ns5:categoryCode>BH</ns5:categoryCode>
   </ns4:Test>
   <ns4:Test>
      <ns5:code>675</ns5:code>
      <ns5:description>His Description</ns5:description>
      <ns5:unwanted>Unwanted XML Parameter</ns5:unwanted>
   </ns4:Test>
</ns4:root>
"""
* def list = $xml/root/Test
* def xpath = function(x, p){ try { return karate.xmlPath(x, p) } catch (e) { return '#notpresent' } }
* def fun = function(x){ return { code: xpath(x, '/Test/code'), descriptionTxt: xpath(x, '/Test/description'), categoryCd: xpath(x, '/Test/categoryCode') } }
* def temp = karate.map(list, fun)
* print temp
* print json.testList
* match json.testList contains temp

Mapping the rest of the JSON is an exercise for you. Please refer to the docs. Also see this answer for more ideas: Karate - Match two dynamic responses

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thanks Peter; I have one more issue during implementation. '* def fun = function(x){ return{xml path traversing goes here} }' this part of the code throws 'at ✽.* def fun = function(x){return{xml path traversing goes here} }(Test/Features/Validation.feature:24) Caused by: javax.script.ScriptException: :1:38 Expected comma but found function(x){ return { xml path traversing goes here } }' error. – Nitheesh hunsur Nov 30 '18 at 07:34